Class: Yaks::Mapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Configurable
Includes:
FP, FP::Callable, Util
Defined in:
lib/yaks/mapper.rb,
lib/yaks/mapper/form.rb,
lib/yaks/mapper/link.rb,
lib/yaks/mapper/config.rb,
lib/yaks/mapper/has_one.rb,
lib/yaks/mapper/has_many.rb,
lib/yaks/mapper/attribute.rb,
lib/yaks/mapper/form/field.rb,
lib/yaks/mapper/association.rb,
lib/yaks/mapper/form/config.rb,
lib/yaks/mapper/form/legend.rb,
lib/yaks/mapper/form/fieldset.rb,
lib/yaks/mapper/form/field/option.rb,
lib/yaks/mapper/association_mapper.rb,
lib/yaks/mapper/form/dynamic_field.rb

Direct Known Subclasses

CollectionMapper

Defined Under Namespace

Classes: Association, AssociationMapper, Attribute, Config, Form, HasMany, HasOne, Link

Instance Attribute Summary collapse

Attributes included from Configurable

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

def_add, def_forward, def_set, extended, inherited

Methods included from Util

#Resolve, #camelize, #extract_options, #reject_keys, #slice_hash, #symbolize_keys, #underscore

Methods included from FP::Callable

#to_proc

Constructor Details

#initialize(context) ⇒ Mapper

Returns a new instance of Mapper.



24
25
26
# File 'lib/yaks/mapper.rb', line 24

def initialize(context)
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



19
20
21
# File 'lib/yaks/mapper.rb', line 19

def context
  @context
end

#objectObject (readonly)

Returns the value of attribute object.



19
20
21
# File 'lib/yaks/mapper.rb', line 19

def object
  @object
end

Class Method Details

.mapper_name(policy) ⇒ Object



40
41
42
# File 'lib/yaks/mapper.rb', line 40

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

Instance Method Details

#call(object, _env = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

#envObject



32
33
34
# File 'lib/yaks/mapper.rb', line 32

def env
  context.fetch(:env)
end

#expand_uri(uri, expand = true) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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) ⇒ Object



67
68
69
# File 'lib/yaks/mapper.rb', line 67

def expand_value(value)
  Resolve(value, self)
end

#load_attribute(name) ⇒ Object Also known as: load_association



62
63
64
# File 'lib/yaks/mapper.rb', line 62

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

#mapper_nameObject



44
45
46
# File 'lib/yaks/mapper.rb', line 44

def mapper_name
  self.class.mapper_name(policy)
end

#mapper_stackObject



36
37
38
# File 'lib/yaks/mapper.rb', line 36

def mapper_stack
  context.fetch(:mapper_stack)
end

#policyObject



28
29
30
# File 'lib/yaks/mapper.rb', line 28

def policy
  context.fetch(:policy)
end