class EacRubyUtils::ActsAsImmutable::HashAccessor

def apply(klass)

def apply(klass)
  apply_plural(klass)
  apply_singular(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[0])
    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 |*args|
    case args.count
    when 1 then next accessor.immutable_value_get_single(self, args[0])
    when 2 then next accessor.immutable_value_set_single(self, *args[0..1])
    else
      raise ::ArgumentError, "wrong number of arguments (given #{args.count}, expected 1..2)"
    end
  end
end

def immutable_value_get(object)

def immutable_value_get(object)
  super || {}
end

def immutable_value_get_single(object, key)

def immutable_value_get_single(object, key)
  immutable_value_get(object)[key]
end

def immutable_value_set(object, new_hash)

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

def immutable_value_set_single(object, key, value)

def immutable_value_set_single(object, key, value)
  immutable_value_set(object, immutable_value_get(object).merge(key => value))
end