Class: EacRubyUtils::Immutable::ArrayAccessor

Inherits:
BaseAccessor show all
Defined in:
lib/eac_ruby_utils/immutable/array_accessor.rb

Instance Method Summary collapse

Methods inherited from BaseAccessor

#duplicate_object

Instance Method Details

#apply(klass) ⇒ Object


10
11
12
13
14
15
16
17
18
19
# File 'lib/eac_ruby_utils/immutable/array_accessor.rb', line 10

def apply(klass)
  accessor = self
  klass.send(:define_method, name) do |value|
    accessor.immutable_value_set(self, value)
  end

  klass.send(:define_method, ::ActiveSupport::Inflector.pluralize(name)) do
    accessor.immutable_value_get(self)
  end
end

#immutable_value_get(object) ⇒ Object


21
22
23
# File 'lib/eac_ruby_utils/immutable/array_accessor.rb', line 21

def immutable_value_get(object)
  super || []
end

#immutable_value_set(object, value) ⇒ Object


25
26
27
28
29
# File 'lib/eac_ruby_utils/immutable/array_accessor.rb', line 25

def immutable_value_set(object, value)
  duplicate_object(object) do |old_value|
    (old_value || []) + [value]
  end
end