Sha256: 21ce3875f7b471e781305997e3477f21f18fd4f8ebdb5474a4784982c6627038

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

module Yaks
  class Mapper
    class AssociationMapper
      attr_reader :parent_mapper, :context, :rel, :association

      def initialize(parent_mapper, association, context)
        @parent_mapper = parent_mapper
        @association   = association
        @context       = context.merge(
          mapper_stack: context[:mapper_stack] + [parent_mapper]
        )
        @rel           = association.map_rel(policy)
      end

      def policy
        context.fetch(:policy)
      end

      def call(resource)
        if association.render_as_link?(parent_mapper)
          add_link(resource)
        else
          add_subresource(resource)
        end
      end

      def add_link(resource)
        Link.new(rel, association.href, {})
          .add_to_resource(resource, parent_mapper, nil)
        # Yaks::Mapper::Link doesn't do anything with the context, making it
        # hard to test that we pass it a context. Passing nil for now, until
        # this is actually needed and can be tested.
      end

      def add_subresource(resource)
        object      = parent_mapper.load_association(association.name)
        subresource = association.map_resource(object, context)
        resource.add_subresource(rel, subresource)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
yaks-0.6.0.alpha.1 lib/yaks/mapper/association_mapper.rb
yaks-html-0.6.0.alpha yaks/lib/yaks/mapper/association_mapper.rb
yaks-0.6.0.alpha yaks/lib/yaks/mapper/association_mapper.rb
yaks-0.5.0 lib/yaks/mapper/association_mapper.rb
yaks-0.4.4 lib/yaks/mapper/association_mapper.rb
yaks-0.4.3 lib/yaks/mapper/association_mapper.rb