Module: L43::OpenObject::Behavior
- Defined in:
- lib/l43/open_object/behavior.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
- #update(**kwds) ⇒ Object
- #update_attribute(attribute, &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, &blk) ⇒ Object
24 25 26 27 |
# File 'lib/l43/open_object/behavior.rb', line 24 def update_attribute(attribute, &blk) raise ArgumentError, "cannot update attribute #{attribute.inspect} without a block" unless blk update(attribute => blk.(to_h[attribute])) end |
#update_attribute_if(attribute, condition, &blk) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/l43/open_object/behavior.rb', line 29 def update_attribute_if(attribute, condition, &blk) if condition update(attribute => blk.(self)) else self end end |
#update_attribute_unless(attribute, condition, &blk) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/l43/open_object/behavior.rb', line 37 def update_attribute_unless(attribute, condition, &blk) if condition self else update(attribute => blk.(self)) end end |