module Blueprinter::BaseHelpers::SingletonMethods
Private Instance Methods
associations(view_name = :default)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 116 def associations(view_name = :default) view_collection.fields_for(view_name).select { |f| f.options[:association] } end
current_view()
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 108 def current_view @current_view ||= view_collection[:default] end
dynamic_blueprint?(blueprint)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 66 def dynamic_blueprint?(blueprint) blueprint.is_a?(Proc) end
inherited(subclass)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 40 def inherited(subclass) subclass.send(:view_collection).inherit(view_collection) end
jsonify(blob)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 104 def jsonify(blob) Blueprinter.configuration.jsonify(blob) end
object_to_hash(object, view_name:, local_options:)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 44 def object_to_hash(object, view_name:, local_options:) result_hash = view_collection.fields_for(view_name).each_with_object({}) do |field, hash| next if field.skip?(field.name, object, local_options) hash[field.name] = field.extract(object, local_options) end view_collection.transformers(view_name).each do |transformer| transformer.transform(result_hash, object, local_options) end result_hash end
prepare_data(object, view_name, local_options)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 20 def prepare_data(object, view_name, local_options) if array_like?(object) object.map do |obj| object_to_hash(obj, view_name: view_name, local_options: local_options) end else object_to_hash(object, view_name: view_name, local_options: local_options) end end
prepare_for_render(object, options)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 12 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) prepare(object, view_name: view_name, local_options: options, root: root, meta: meta) end
prepend_root_and_meta(data, root, meta)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 34 def prepend_root_and_meta(data, root, meta) return data unless root ret = {root => data} meta ? ret.merge!(meta: meta) : ret end
validate_blueprint!(blueprint, method)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 70 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) end end
validate_blueprint_has_ancestors!(blueprint, association_name)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 82 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 # be raised. unless blueprint.respond_to?(:ancestors) raise BlueprinterError, "Blueprint provided for #{association_name} "\ 'association is not valid.' end end
validate_blueprint_has_blueprinter_base_ancestor!(blueprint, association_name)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 93 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 # Raise error describing what's wrong. raise BlueprinterError, "Class #{blueprint.name} does not inherit from "\ 'Blueprinter::Base and is not a valid Blueprinter '\ "for #{association_name} association." end
validate_presence_of_blueprint!(blueprint)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 78 def validate_presence_of_blueprint!(blueprint) raise BlueprinterError, 'Blueprint required' unless blueprint end
validate_root_and_meta!(root, meta)
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 55 def validate_root_and_meta!(root, meta) case root when String, Symbol # no-op when NilClass raise BlueprinterError, "meta requires a root to be passed" if meta else raise BlueprinterError, "root should be one of String, Symbol, NilClass" end end
view_collection()
click to toggle source
# File lib/blueprinter/helpers/base_helpers.rb, line 112 def view_collection @view_collection ||= ViewCollection.new end