class ParamsReady::Helpers::KeyMap::Mapping

Attributes

alt[R]
std[R]

Public Class Methods

new(alt_path, alt_names, std_path, std_names) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 68
def initialize(alt_path, alt_names, std_path, std_names)
  if alt_names.length != std_names.length
    msg = "Expected equal number of alternative and standard names, got #{alt_names.length}/#{std_names.length}"
    raise ParamsReadyError, msg
  end

  @alt = Path.new(alt_path, alt_names)
  @std = Path.new(std_path, std_names)
end

Public Instance Methods

=~(other) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 115
def =~(other)
  raise ParamsReadyError, "Can't match path with #{other.class.name}" unless other.is_a? Mapping
  return false unless path(:alt) =~ other.path(:alt)
  return false unless path(:std) =~ other.path(:std)

  true
end
add_names(altn, stdn) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 78
def add_names(altn, stdn)
  @alt.add_name altn
  @std.add_name stdn
end
dig(schema, name, hash) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 98
def dig(schema, name, hash)
  path(schema).dig(name, hash)
end
merge!(other) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 91
def merge!(other)
  raise ParamsReadyError, "Can't merge non_matching mapping" unless self =~ other

  @alt.add_names(other.alt.names)
  @std.add_names(other.std.names)
end
path(schema) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 106
def path(schema)
  case schema
  when :alt then @alt
  when :std then @std
  else
    raise ParamsReadyError, "Unexpected naming schema: #{schema}"
  end
end
remap(from, to, input, target) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 83
def remap(from, to, input, target)
  path(from).names.each_with_index do |input_name, idx|
    value = dig(from, input_name, input)
    target_name = path(to).names[idx]
    store(to, target_name, value, target)
  end
end
store(schema, name, value, hash) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 102
def store(schema, name, value, hash)
  path(schema).store(name, value, hash)
end