class ParamsReady::Parameter::AbstractHashParameter

Constants

EMPTY_HASH

Public Instance Methods

[](name) click to toggle source
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 33
def [](name)
  child(name)
end
[]=(name, value) click to toggle source
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 27
def []=(name, value)
  init_for_write
  c = child(name)
  c.set_value(value)
end
find_in_hash(hash, context) click to toggle source
Calls superclass method
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 37
def find_in_hash(hash, context)
  found, result = super hash, context

  if !(default_defined? || optional?) && result == Extensions::Undefined
    # nil value for non-default and non-optional hash means
    # children may be able to set themselves if they have defaults
    [true, {}]
  elsif result == EMPTY_HASH
    [true, {}]
  else
    [found, result]
  end
end
for_frontend(format = :frontend, restriction: nil, data: nil) click to toggle source
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 68
def for_frontend(format = :frontend, restriction: nil, data: nil)
  for_output(format, restriction: restriction, data: data)
end
for_model(format = :update, restriction: nil) click to toggle source
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 72
def for_model(format = :update, restriction: nil)
  for_output(format, restriction: restriction)
end
for_output(format, restriction: nil, data: nil) click to toggle source
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 59
def for_output(format, restriction: nil, data: nil)
  restriction ||= Restriction.blanket_permission

  intent = Intent.new(format, restriction, data: data)
  output = format(intent)
  return {} if output.nil? || output == EMPTY_HASH
  output
end
wrap_output(output, intent) click to toggle source
Calls superclass method
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 51
def wrap_output(output, intent)
  if (output.nil? || output.empty?) && !default_defined? && !optional?
    nil
  else
    super
  end
end

Protected Instance Methods

child(name) click to toggle source
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 89
def child(name)
  return nil if is_nil?

  value = bare_value
  raise ParamsReadyError, "No such name: #{name}" unless names.key? name
  if value.key? name
    value[name]
  else
    child = definition.child_definition(name).create
    raise ParamsReadyError, "Expected definite value for '#{name}' parameter" if child.nil?
    place(name, child) unless frozen?
    child
  end
end
child_for_update(path) click to toggle source
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 78
def child_for_update(path)
  child_name, *path = path
  [self[child_name], child_name, path]
end
init_value() click to toggle source
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 128
def init_value
  @value = names.map do |name, definition|
    [name, definition.create]
  end.to_h
end
place(name, child) click to toggle source
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 124
def place(name, child)
  @value[name] = child
end
populate_with(hash, freeze = false, **replacement) click to toggle source
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 104
def populate_with(hash, freeze = false, **replacement)
  @value = {}
  names.each_key do |name|
    incoming = replacement[name] || hash[name]

    own = if freeze && incoming.frozen?
      incoming
    else
      incoming.dup
    end

    own.freeze if freeze

    place(name, own)
  end

  self.freeze if freeze
  self
end
updated_clone(name, updated) click to toggle source
# File lib/params_ready/parameter/abstract_hash_parameter.rb, line 83
def updated_clone(name, updated)
  clone = definition.create
  frozen = frozen? || @value&.frozen?
  clone.populate_with(bare_value, frozen, name => updated)
end