Skip to content

Commit

Permalink
Add bang to methods that might throw errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
caws committed Aug 11, 2020
1 parent 5f48b5c commit dab36ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/blueprinter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def self.field(method, options = {}, &block)
#
# @return [Field] A Field object
def self.association(method, options = {}, &block)
validate_blueprint(options[:blueprint], method)
validate_blueprint!(options[:blueprint], method)

field(
method,
Expand Down
18 changes: 9 additions & 9 deletions lib/blueprinter/helpers/base_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def prepare_for_render(object, options)
view_name = options.delete(:view) || :default
root = options.delete(:root)
meta = options.delete(:meta)
validate_root_and_meta(root, meta)
validate_root_and_meta!(root, meta)
prepare(object, view_name: view_name, local_options: options, root: root, meta: meta)
end

Expand Down Expand Up @@ -52,7 +52,7 @@ def object_to_hash(object, view_name:, local_options:)
result_hash
end

def validate_root_and_meta(root, meta)
def validate_root_and_meta!(root, meta)
case root
when String, Symbol
# no-op
Expand All @@ -67,19 +67,19 @@ def dynamic_blueprint?(blueprint)
blueprint.is_a?(Proc)
end

def validate_blueprint(blueprint, method)
validate_presence_of_blueprint(blueprint)
def validate_blueprint!(blueprint, method)
validate_presence_of_blueprint!(blueprint)
unless dynamic_blueprint?(blueprint)
validate_blueprint_has_ancestors(blueprint, method)
validate_blueprint_has_blueprinter_base_ancestor(blueprint, method)
validate_blueprint_has_ancestors!(blueprint, method)
validate_blueprint_has_blueprinter_base_ancestor!(blueprint, method)
end
end

def validate_presence_of_blueprint(blueprint)
def validate_presence_of_blueprint!(blueprint)
raise BlueprinterError, 'Blueprint required' unless blueprint
end

def validate_blueprint_has_ancestors(blueprint, association_name)
def validate_blueprint_has_ancestors!(blueprint, association_name)
# If the class passed as a blueprint does not respond to ancestors
# it means it, at the very least, does not have Blueprinter::Base as
# one of its ancestor classes (e.g: Hash) and thus an error should
Expand All @@ -90,7 +90,7 @@ def validate_blueprint_has_ancestors(blueprint, association_name)
end
end

def validate_blueprint_has_blueprinter_base_ancestor(blueprint, association_name)
def validate_blueprint_has_blueprinter_base_ancestor!(blueprint, association_name)
# Guard clause in case Blueprinter::Base is present in the ancestor list
# for the blueprint class provided.
return if blueprint.ancestors.include? Blueprinter::Base
Expand Down

0 comments on commit dab36ce

Please sign in to comment.