Class: Optics::Lens
- Inherits:
-
Object
- Object
- Optics::Lens
- Defined in:
- lib/optics.rb
Instance Method Summary collapse
- #combine(other) ⇒ Object
- #get(subject) ⇒ Object
-
#initialize(getter, setter) ⇒ Lens
constructor
A new instance of Lens.
- #set(subject, value) ⇒ Object
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 |