Module: L43::OpenObject::Behavior

Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/l43/open_object/behavior.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



13
14
15
16
# File 'lib/l43/open_object/behavior.rb', line 13

def ==(other)
  return false unless self.class === other
  other.to_h == to_h
end

#update(**kwds) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/l43/open_object/behavior.rb', line 18

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



30
31
32
33
34
35
36
# File 'lib/l43/open_object/behavior.rb', line 30

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



38
39
40
41
42
43
44
# File 'lib/l43/open_object/behavior.rb', line 38

def update_attribute_if(attribute, condition, &blk)
  if condition
    update_attribute(attribute, condition, &blk)
  else
    self
  end
end

#update_attribute_or(attribute, value, &blk) ⇒ Object



46
47
48
49
# File 'lib/l43/open_object/behavior.rb', line 46

def update_attribute_or(attribute, value, &blk)
  return update_attribute(attribute, value) if self[attribute].nil?
  update_attribute(attribute, blk.(self[attribute], value))
end

#update_attribute_unless(attribute, condition, &blk) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/l43/open_object/behavior.rb', line 51

def update_attribute_unless(attribute, condition, &blk)
  if condition
    self
  else
    update(attribute => blk.(self))
  end
end