class Blueprinter::ViewCollection

@api private

Attributes

sort_by_definition[R]
views[R]

Public Class Methods

new() click to toggle source
# File lib/blueprinter/view_collection.rb, line 5
def initialize
  @views = {
    identifier: View.new(:identifier),
    default: View.new(:default)
  }
  @sort_by_definition = Blueprinter.configuration.sort_fields_by.eql?(:definition)
end

Public Instance Methods

[](view_name) click to toggle source
# File lib/blueprinter/view_collection.rb, line 36
def [](view_name)
  @views[view_name] ||= View.new(view_name)
end
fields_for(view_name) click to toggle source
# File lib/blueprinter/view_collection.rb, line 23
def fields_for(view_name)
  return identifier_fields if view_name == :identifier

  fields, excluded_fields = sortable_fields(view_name)
  sorted_fields = sort_by_definition ? sort_by_def(view_name, fields) : fields.values.sort_by(&:name)

  (identifier_fields + sorted_fields).reject { |field| excluded_fields.include?(field.name) }
end
has_view?(view_name) click to toggle source
# File lib/blueprinter/view_collection.rb, line 19
def has_view?(view_name)
  views.has_key? view_name
end
inherit(view_collection) click to toggle source
# File lib/blueprinter/view_collection.rb, line 13
def inherit(view_collection)
  view_collection.views.each do |view_name, view|
    self[view_name].inherit(view)
  end
end
transformers(view_name) click to toggle source
# File lib/blueprinter/view_collection.rb, line 32
def transformers(view_name)
  views[view_name].transformers
end

Private Instance Methods

add_to_ordered_fields(ordered_fields, definition, fields, view_name_filter = nil) click to toggle source

view_name_filter allows to follow definition order all the way down starting from the view_name given to sort_by_def() but include no others at the top-level

# File lib/blueprinter/view_collection.rb, line 74
def add_to_ordered_fields(ordered_fields, definition, fields, view_name_filter = nil)
  if definition.view?
    if view_name_filter.nil? || view_name_filter == definition.name
      views[definition.name].definition_order.each { |_definition| add_to_ordered_fields(ordered_fields, _definition, fields) }
    end
  else
    ordered_fields[definition.name] = fields[definition.name]
  end
end
identifier_fields() click to toggle source
# File lib/blueprinter/view_collection.rb, line 42
def identifier_fields
  views[:identifier].fields.values
end
merge_fields(source_fields, included_fields) click to toggle source
# File lib/blueprinter/view_collection.rb, line 84
def merge_fields(source_fields, included_fields)
  source_fields.merge included_fields
end
sort_by_def(view_name, fields) click to toggle source

select and order members of fields according to traversal of the definition_orders

# File lib/blueprinter/view_collection.rb, line 66
def sort_by_def(view_name, fields)
  ordered_fields = {}
  views[:default].definition_order.each { |definition| add_to_ordered_fields(ordered_fields, definition, fields, view_name)  }
  ordered_fields.values
end
sortable_fields(view_name) click to toggle source

@param [String] view_name @return [Array<(Hash, Hash<String, NilClass>)>] fields, excluded_fields

# File lib/blueprinter/view_collection.rb, line 48
def sortable_fields(view_name)
  excluded_fields = {}
  fields = views[:default].fields
  views[view_name].included_view_names.each do |included_view_name|
    next if view_name == included_view_name

    view_fields, view_excluded_fields = sortable_fields(included_view_name)
    fields = merge_fields(fields, view_fields)
    excluded_fields.merge!(view_excluded_fields)
  end
  fields = merge_fields(fields, views[view_name].fields)

  views[view_name].excluded_field_names.each { |name| excluded_fields[name] = nil }

  [fields, excluded_fields]
end