Sha256: 6484a6cdf6a33e0fec01b970fd4970fddae8e819c290a5db3766c8c0cda494ee

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

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

module Yaks
  class CollectionMapper < Mapper
    alias collection object

    def initialize(context)
      super(context)
    end

    def member_mapper
      context.fetch(:member_mapper) do
        if collection.first
          mapper_for_model(collection.first)
        end
      end
    end

    def call(collection)
      @object = collection

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

      attrs[ :members_rel ] = members_rel if members_rel

      CollectionResource.new(attrs)
    end

    private

    def members_rel
      policy.expand_rel( 'collection', pluralize( collection_type ) ) if collection_type
    end

    def collection_type
      return unless member_mapper
      member_mapper.config.type || policy.derive_type_from_mapper_class(member_mapper)
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yaks-0.4.2 lib/yaks/collection_mapper.rb