Class: Monolens::Object::Extend

Inherits:
Object
  • Object
show all
Includes:
Lens
Defined in:
lib/monolens/stdlib/object/extend.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) ⇒ Extend

Returns a new instance of Extend.



11
12
13
14
15
16
17
# File 'lib/monolens/stdlib/object/extend.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
35
36
# File 'lib/monolens/stdlib/object/extend.rb', line 19

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

  result = arg.dup
  is_symbol = arg.keys.any?{|k| k.is_a?(Symbol) }
  option(:defn, {}).each_pair do |attr, lens|
    deeper(world, attr) do |w|
      actual = is_symbol ? attr.to_sym : attr.to_s
      begin
        result[actual] = lens.call(arg, w)
      rescue Monolens::LensError => ex
        strategy = option(:on_error, :fail)
        handle_error(strategy, ex, result, actual, world)
      end
    end
  end
  result
end