class Yaks::Mapper

Attributes

context[R]
object[R]

Public Class Methods

mapper_name(policy) click to toggle source
# File lib/yaks/mapper.rb, line 40
def self.mapper_name(policy)
  config.type || policy.derive_type_from_mapper_class(self)
end
new(context) click to toggle source
# File lib/yaks/mapper.rb, line 24
def initialize(context)
  @context = context
end

Public Instance Methods

call(object, _env = nil) click to toggle source
# File lib/yaks/mapper.rb, line 48
def call(object, _env = nil)
  @object = object

  return NullResource.new if object.nil?

  [ :map_attributes,
    :map_links,
    :map_subresources,
    :map_forms
  ].inject(Resource.new(type: mapper_name)) do |resource, method|
    __send__(method, resource)
  end
end
env() click to toggle source
# File lib/yaks/mapper.rb, line 32
def env
  context.fetch(:env)
end
expand_uri(uri, expand = true) click to toggle source
# File lib/yaks/mapper.rb, line 71
def expand_uri(uri, expand = true)
  return if uri.nil?
  return Resolve(uri, self) if uri.respond_to?(:to_proc)

  template = URITemplate.new(uri)
  expand_vars = case expand
                when true
                  template.variables
                when false
                  []
                else
                  expand
                end

  mapping = expand_vars.each_with_object({}) do |name, hsh|
    hsh[name] = load_attribute(name)
  end

  template.expand_partial(mapping).to_s
end
expand_value(value) click to toggle source
# File lib/yaks/mapper.rb, line 67
def expand_value(value)
  Resolve(value, self)
end
load_association(name)
Alias for: load_attribute
load_attribute(name) click to toggle source
# File lib/yaks/mapper.rb, line 62
def load_attribute(name)
  respond_to?(name) ? public_send(name) : object.public_send(name)
end
Also aliased as: load_association
mapper_name() click to toggle source
# File lib/yaks/mapper.rb, line 44
def mapper_name
  self.class.mapper_name(policy)
end
mapper_stack() click to toggle source
# File lib/yaks/mapper.rb, line 36
def mapper_stack
  context.fetch(:mapper_stack)
end
policy() click to toggle source
# File lib/yaks/mapper.rb, line 28
def policy
  context.fetch(:policy)
end

Private Instance Methods

map_attributes(resource) click to toggle source
# File lib/yaks/mapper.rb, line 94
def map_attributes(resource)
  attributes.inject(resource) do |res, attribute|
    attribute.add_to_resource(res, self, context)
  end
end
map_forms(resource) click to toggle source
# File lib/yaks/mapper.rb, line 112
def map_forms(resource)
  forms.inject(resource) do |res, form|
    form.add_to_resource(res, self, context)
  end
end
map_subresources(resource) click to toggle source
# File lib/yaks/mapper.rb, line 106
def map_subresources(resource)
  associations.inject(resource) do |res, association|
    association.add_to_resource(res, self, context)
  end
end