Sha256: a45fea12d14f8130952f959b85430e1bc6784c584129d40a82ebc20fccb60b0d

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

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

module Yaks
  class Mapper
    extend ClassMethods, Forwardable
    include Util, FP

    def_delegators 'self.class', :config
    def_delegators :config, :attributes, :links, :associations

    attr_reader :object, :context

    def initialize(context)
      @context = context
    end

    def policy
      context.fetch(:policy)
    end

    def env
      context.fetch(:env)
    end

    def call(object)
      @object = object

      return NullResource.new if object.nil?

      Resource.new(
        type:         mapper_name,
        attributes:   map_attributes,
        links:        map_links,
        subresources: map_subresources
      )
    end

    def map_attributes
      filter(attributes).each_with_object({}) do |attr, memo|
        memo[attr] = load_attribute(attr)
      end
    end

    def map_links
      links.map &send_with_args(:map_to_resource_link, self)
    end

    def map_subresources
      attributes   = filter(associations.map(&:name))
      associations = associations().select{|assoc| attributes.include? assoc.name }
      associations.each_with_object({}) do |association, memo|
        rel, subresource = association.create_subresource(
          self,
          method(:load_association),
          context
        )
        memo[rel] = subresource
      end
    end

    def load_attribute(name)
      respond_to?(name) ? public_send(name) : object.public_send(name)
    end
    alias load_association load_attribute

    def filter(attrs)
      attrs
    end

    def mapper_name
      config.type || policy.derive_type_from_mapper_class(self.class)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yaks-0.4.1 lib/yaks/mapper.rb
yaks-0.4.0 lib/yaks/mapper.rb