Sha256: 890c661232bd2cd85624b756fd760174909ea85fdef9a5b85a3df81b3e2f04c5

Contents?: true

Size: 1.06 KB

Versions: 8

Compression:

Stored size: 1.06 KB

Contents

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

module Yaks
  class CollectionMapper < Mapper
    alias collection object

    # @param [Array] collection
    # @return [Array]
    def call(collection)
      @object = collection

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

      attrs[ :collection_rel ] = collection_rel

      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

8 entries across 8 versions & 2 rubygems

Version Path
yaks-0.6.2 lib/yaks/collection_mapper.rb
yaks-0.6.1 lib/yaks/collection_mapper.rb
yaks-0.6.0 lib/yaks/collection_mapper.rb
yaks-0.6.0.alpha.1 lib/yaks/collection_mapper.rb
yaks-html-0.6.0.alpha yaks/lib/yaks/collection_mapper.rb
yaks-0.6.0.alpha yaks/lib/yaks/collection_mapper.rb
yaks-0.5.0 lib/yaks/collection_mapper.rb
yaks-0.4.4 lib/yaks/collection_mapper.rb