Module: L43::OpenObject::Behavior
- Defined in:
- lib/l43/open_object/behavior.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
- #update(**kwds) ⇒ Object
- #update_attribute(attribute, value = nil, &blk) ⇒ Object
- #update_attribute_if(attribute, condition, &blk) ⇒ Object
- #update_attribute_unless(attribute, condition, &blk) ⇒ Object
Instance Method Details
#==(other) ⇒ Object
7 8 9 10 |
# File 'lib/l43/open_object/behavior.rb', line 7 def ==(other) return false unless self.class === other other.to_h == to_h end |
#update(**kwds) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/l43/open_object/behavior.rb', line 12 def update(**kwds) if frozen? new_values = to_h.merge(**kwds) self.class.new(**new_values) else kwds.each do |k, v| instance_variable_set("@#{k}", v) end self end end |
#update_attribute(attribute, value = nil, &blk) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/l43/open_object/behavior.rb', line 24 def update_attribute(attribute, value=nil, &blk) if blk update(attribute => blk.(to_h[attribute])) else update(attribute => value) end end |
#update_attribute_if(attribute, condition, &blk) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/l43/open_object/behavior.rb', line 32 def update_attribute_if(attribute, condition, &blk) if condition update_attribute(attribute, condition, &blk) else self end end |
#update_attribute_unless(attribute, condition, &blk) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/l43/open_object/behavior.rb', line 40 def update_attribute_unless(attribute, condition, &blk) if condition self else update(attribute => blk.(self)) end end |