Class: Optics::Lens

Inherits:
Object
  • Object
show all
Defined in:
lib/optics.rb

Direct Known Subclasses

HashLens, IxLens

Instance Method Summary collapse

Constructor Details

#initialize(getter, setter) ⇒ Lens

Returns a new instance of Lens.



5
6
7
8
# File 'lib/optics.rb', line 5

def initialize(getter, setter)
  @getter = getter
  @setter = setter
end

Instance Method Details

#combine(other) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/optics.rb', line 18

def combine(other)
  Lens.new(
    -> (subject) { other.get(get(subject)) },
    -> (subject, value) {
      set(subject, other.set(get(subject), value))
    }
  )
end

#get(subject) ⇒ Object



14
15
16
# File 'lib/optics.rb', line 14

def get(subject)
  @getter.(subject)
end

#set(subject, value) ⇒ Object



10
11
12
# File 'lib/optics.rb', line 10

def set(subject, value)
  @setter.(subject, value)
end