Sha256: 197fc57f1b106afd0475bccf0e0ae09da8d17bf1a6c54e36b13495ce8e2da756

Contents?: true

Size: 1.25 KB

Versions: 16

Compression:

Stored size: 1.25 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.create(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).add_rel(rel)
        resource.add_subresource(subresource)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
yaks-0.9.0 lib/yaks/mapper/association_mapper.rb
yaks-0.8.3 lib/yaks/mapper/association_mapper.rb
yaks-0.8.2 lib/yaks/mapper/association_mapper.rb
yaks-0.8.1 lib/yaks/mapper/association_mapper.rb
yaks-0.8.0 lib/yaks/mapper/association_mapper.rb
yaks-0.8.0.beta2 lib/yaks/mapper/association_mapper.rb
yaks-0.8.0.beta1 lib/yaks/mapper/association_mapper.rb
yaks-0.8.0.alpha lib/yaks/mapper/association_mapper.rb
yaks-0.7.7 lib/yaks/mapper/association_mapper.rb
yaks-0.7.6 lib/yaks/mapper/association_mapper.rb
yaks-0.7.5 lib/yaks/mapper/association_mapper.rb
yaks-0.7.4 lib/yaks/mapper/association_mapper.rb
yaks-0.7.3 lib/yaks/mapper/association_mapper.rb
yaks-0.7.2 lib/yaks/mapper/association_mapper.rb
yaks-0.7.1 lib/yaks/mapper/association_mapper.rb
yaks-0.7.0 lib/yaks/mapper/association_mapper.rb