Class: EacRubyUtils::Immutable::CommonAccessor

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

Direct Known Subclasses

BooleanAccessor

Instance Method Summary collapse

Methods inherited from BaseAccessor

#duplicate_object

Instance Method Details

#apply(klass) ⇒ Object


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

def apply(klass)
  accessor = self
  klass.send(:define_method, name) do |*args|
    case args.count
    when 0 then next accessor.immutable_value_get(self)
    when 1 then next accessor.immutable_value_set(self, args.first)
    else
      raise ::ArgumentError, "wrong number of arguments (given #{args.count}, expected 0..1)"
    end
  end
end

#immutable_value_get(object) ⇒ Object


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

def immutable_value_get(object)
  object.send(:immutable_values_get)[name]
end

#immutable_value_set(object, value) ⇒ Object


25
26
27
# File 'lib/eac_ruby_utils/immutable/common_accessor.rb', line 25

def immutable_value_set(object, value)
  duplicate_object(object) { |_old_value| value }
end