class ParamsReady::Helpers::KeyMap

Public Class Methods

new() click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 127
def initialize
  @mappings = []
end
split_map(array) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 167
def self.split_map(array)
  raise ParamsReadyError, "Array expected, got: #{array.class.name}" unless array.is_a? Array
  names = array.last || []
  raise ParamsReadyError, "Array expected, got: #{names.class.name}" unless names.is_a? Array
  paths = array[0...-1]
  [paths, names]
end

Public Instance Methods

freeze() click to toggle source
Calls superclass method
# File lib/params_ready/helpers/key_map.rb, line 162
def freeze
  @mappings.each(&:freeze)
  super
end
map(from, to:) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 131
def map(from, to:)
  alt_path, alt_names = self.class.split_map(from)
  std_path, std_names = self.class.split_map(to)
  mapping = Mapping.new(alt_path, alt_names, std_path, std_names)
  merge_or_add_mapping(mapping)
  self
end
merge_or_add_mapping(mapping) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 139
def merge_or_add_mapping(mapping)
  if (existing = @mappings.find { |candidate| candidate =~ mapping })
    existing.merge!(mapping)
  else
    @mappings << mapping
  end
  mapping
end
remap(from, to, input) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 156
def remap(from, to, input)
  @mappings.each_with_object({}) do |mapping, result|
    mapping.remap(from, to, input, result)
  end
end
to_alternative(hash) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 152
def to_alternative(hash)
  remap(:std, :alt, hash)
end
to_standard(hash) click to toggle source
# File lib/params_ready/helpers/key_map.rb, line 148
def to_standard(hash)
  remap(:alt, :std, hash)
end