Sha256: 2468465379f340a94da1a3cd42b0e86ff27048f3b4ea69ac3921bc4bd509e2b7

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

# -*- coding: utf-8 -*-

module Yaks
  class CollectionMapper < Mapper
    alias collection object

    # @param [Array] collection
    # @return [Array]
    def call(collection, env = {})
      @object = collection

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

      if context[:mapper_stack].empty?
        attrs[:rels] = [collection_rel]
      end

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

    private

    def collection_rel
      if collection_type
        policy.expand_rel( pluralize( collection_type ) )
      else
        'collection'
      end
    end

    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

    def mapper_for_model(model)
      context.fetch(:item_mapper) do
        policy.derive_mapper_from_object(model)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
yaks-0.7.7 lib/yaks/collection_mapper.rb
yaks-0.7.6 lib/yaks/collection_mapper.rb
yaks-0.7.5 lib/yaks/collection_mapper.rb
yaks-0.7.4 lib/yaks/collection_mapper.rb
yaks-0.7.3 lib/yaks/collection_mapper.rb
yaks-0.7.2 lib/yaks/collection_mapper.rb
yaks-0.7.1 lib/yaks/collection_mapper.rb