class Yaks::CollectionMapper

Public Instance Methods

call(collection, _env = nil) click to toggle source

@param [Array] collection @return [Array]

# File lib/yaks/collection_mapper.rb, line 7
def call(collection, _env = nil)
  @object = collection

  attrs = {
    type: collection_type,
    members: collection().map do |obj|
      mapper_for_model(obj).new(context).call(obj)
    end
  }

  # For collections from associations the rel will be based on the
  # association. At the top level there's no association, so we
  # use a generic rel. This matters especially for HAL, where a
  # top-level collection is rendered as an object with the
  # collection as a subresource.
  attrs[:rels] = [collection_rel] if context[:mapper_stack].empty?

  map_attributes(
    map_links(
      CollectionResource.new(attrs)
    )
  )
end

Private Instance Methods

collection_rel() click to toggle source
# File lib/yaks/collection_mapper.rb, line 33
def collection_rel
  if collection_type
    policy.expand_rel(pluralize(collection_type))
  else
    'collection'
  end
end
collection_type() click to toggle source
# File lib/yaks/collection_mapper.rb, line 41
def collection_type
  if item_mapper = context[:item_mapper]
    item_mapper.config.type || policy.derive_type_from_mapper_class(item_mapper)
  else
    policy.derive_type_from_collection(collection)
  end
end
mapper_for_model(model) click to toggle source
# File lib/yaks/collection_mapper.rb, line 49
def mapper_for_model(model)
  context.fetch(:item_mapper) do
    policy.derive_mapper_from_object(model)
  end
end