Sha256: 04ea4c61ce8768ea07d2ec0c8a8c7f661cbcdae8de96941ed7f263e0d1517fb3

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

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

module Yaks
  class Mapper
    extend ClassMethods, Forwardable
    include Util, MapLinks, SharedOptions

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

    attr_reader :subject, :options
    private :subject, :options
    alias object subject

    def initialize(subject, options = {})
      @subject = subject
      @options = YAKS_DEFAULT_OPTIONS.merge(options)
    end

    def to_resource
      return NullResource.new if subject.nil?

      Resource.new(
        map_attributes,
        map_links,
        map_subresources
      )
    end

    def profile_type
      config.profile || policy.derive_profile_from_mapper(self)
    end

    def map_attributes
      filter(attributes).map &juxt(identity_function, method(:load_attribute))
    end

    def map_subresources
      filtered = filter(associations.map(&:name))
      associations.select{|assoc| filtered.include? assoc.name}.map do |association|
        association.map_to_resource_pair(profile_type, method(:load_association), options)
      end
    end

    def load_attribute(name)
      respond_to?(name) ? send(name) : subject.send(name)
    end
    alias load_association load_attribute

    def profile
      config.profile || policy.derive_missing_profile_from_mapper(self)
    end

    def filter(attrs)
      attrs
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yaks-0.2.0 lib/yaks/mapper.rb
yaks-0.1.0 lib/yaks/mapper.rb