Class: EacRubyUtils::ActsAsImmutable::EnumerableAccessor
- Inherits:
-
BaseAccessor
show all
- Defined in:
- lib/eac_ruby_utils/acts_as_immutable/enumerable_accessor.rb
Overview
Should implement in concrect classes:
Constant Summary
Constants inherited
from BaseAccessor
BaseAccessor::FILTER_GET_METHOD_NAME_FORMAT, BaseAccessor::FILTER_SET_METHOD_NAME_FORMAT
Instance Method Summary
collapse
#duplicate_object, #immutable_value_get_filtered, #immutable_value_get_filtered_method_name, #immutable_value_set_filtered, #immutable_value_set_filtered_method_name
Instance Method Details
#apply(klass) ⇒ Object
13
14
15
16
|
# File 'lib/eac_ruby_utils/acts_as_immutable/enumerable_accessor.rb', line 13
def apply(klass)
apply_singular(klass)
apply_plural(klass)
end
|
#apply_plural(klass) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/eac_ruby_utils/acts_as_immutable/enumerable_accessor.rb', line 18
def apply_plural(klass)
accessor = self
klass.send(:define_method, ::ActiveSupport::Inflector.pluralize(name)) do |*args|
case args.count
when 0 then next accessor.immutable_value_get_filtered(self)
when 1 then next accessor.immutable_value_set_filtered(self, args.first)
else
raise ::ArgumentError, "wrong number of arguments (given #{args.count}, expected 0..1)"
end
end
end
|
#apply_singular(klass) ⇒ Object
30
31
32
33
34
35
|
# File 'lib/eac_ruby_utils/acts_as_immutable/enumerable_accessor.rb', line 30
def apply_singular(klass)
accessor = self
klass.send(:define_method, name) do |value|
accessor.immutable_value_push(self, value)
end
end
|
#immutable_value_get(object) ⇒ Object
37
38
39
|
# File 'lib/eac_ruby_utils/acts_as_immutable/enumerable_accessor.rb', line 37
def immutable_value_get(object)
super || initial_value.dup
end
|
#immutable_value_push(object, value) ⇒ Object
41
42
43
44
45
|
# File 'lib/eac_ruby_utils/acts_as_immutable/enumerable_accessor.rb', line 41
def immutable_value_push(object, value)
duplicate_object(object) do |old_value|
(old_value || initial_value.dup) + [value]
end
end
|
#immutable_value_set(object, value) ⇒ Object
47
48
49
|
# File 'lib/eac_ruby_utils/acts_as_immutable/enumerable_accessor.rb', line 47
def immutable_value_set(object, value)
duplicate_object(object) { |_old_value| immutable_value_set_assert(value) }
end
|