class EacRubyUtils::ActsAsImmutable::EnumerableAccessor

  • initial_value()
    * immutable_value_set_assert(value)
    Should implement in concrect classes:

def apply(klass)

def apply(klass)
  apply_singular(klass)
  apply_plural(klass)
end

def apply_plural(klass)

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

def apply_singular(klass)

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

def immutable_value_get(object)

def immutable_value_get(object)
  super || initial_value.dup
end

def immutable_value_push(object, value)

def immutable_value_push(object, value)
  duplicate_object(object) do |old_value|
    (old_value || initial_value.dup) + [value]
  end
end

def immutable_value_set(object, value)

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