Class: ParamsReady::Helpers::KeyMap::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/params_ready/helpers/key_map.rb

Defined Under Namespace

Classes: Path

Instance Method Summary collapse

Constructor Details

#initialize(alt_path, alt_names, std_path, std_names) ⇒ Mapping

Returns a new instance of Mapping.



68
69
70
71
72
73
74
75
76
# 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

Instance Method Details

#=~(other) ⇒ Object

Raises:



115
116
117
118
119
120
121
# 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) ⇒ Object



78
79
80
81
# 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) ⇒ Object



98
99
100
# File 'lib/params_ready/helpers/key_map.rb', line 98

def dig(schema, name, hash)
  path(schema).dig(name, hash)
end

#merge!(other) ⇒ Object

Raises:



91
92
93
94
95
96
# 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) ⇒ Object



106
107
108
109
110
111
112
113
# 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) ⇒ Object



83
84
85
86
87
88
89
# 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) ⇒ Object



102
103
104
# File 'lib/params_ready/helpers/key_map.rb', line 102

def store(schema, name, value, hash)
  path(schema).store(name, value, hash)
end