Class: Monolens::Object::Transform

Inherits:
Object
  • Object
show all
Includes:
Lens
Defined in:
lib/monolens/stdlib/object/transform.rb

Instance Attribute Summary

Attributes included from Lens

#options

Instance Method Summary collapse

Methods included from Lens

#fail!, included

Methods included from Lens::FetchSupport

#fetch_on

Constructor Details

#initialize(options, registry) ⇒ Transform

Returns a new instance of Transform.



11
12
13
14
15
16
17
# File 'lib/monolens/stdlib/object/transform.rb', line 11

def initialize(options, registry)
  super(options, registry)
  ts = option(:defn, {})
  ts.each_pair do |k,v|
    ts[k] = lens(v)
  end
end

Instance Method Details

#call(arg, world = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/monolens/stdlib/object/transform.rb', line 19

def call(arg, world = {})
  is_hash!(arg, world)

  result = arg.dup
  option(:defn, {}).each_pair do |attr, sub_lens|
    deeper(world, attr) do |w|
      actual_attr, fetched = fetch_on(attr, arg)
      if actual_attr
        result[actual_attr] = sub_lens.call(fetched, w)
      else
        on_missing(result, attr, world)
      end
    end
  end
  result
end