Class: Yaks::CollectionMapper

Inherits:
Mapper
  • Object
show all
Defined in:
lib/yaks/collection_mapper.rb

Instance Attribute Summary

Attributes inherited from Mapper

#context, #object

Attributes included from Configurable

#config

Instance Method Summary collapse

Methods inherited from Mapper

#env, #expand_uri, #expand_value, #initialize, #load_attribute, mapper_name, #mapper_name, #mapper_stack, #policy

Methods included from Configurable

#def_add, #def_forward, #def_set, extended, #inherited

Methods included from Util

#Resolve, #camelize, #extract_options, #reject_keys, #slice_hash, #symbolize_keys, #underscore

Methods included from FP::Callable

#to_proc

Constructor Details

This class inherits a constructor from Yaks::Mapper

Instance Method Details

#call(collection, _env = nil) ⇒ Array

Parameters:

  • collection (Array)

Returns:

  • (Array)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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