Class: ParamsReady::Helpers::KeyMap
- Inherits:
-
Object
- Object
- ParamsReady::Helpers::KeyMap
- Defined in:
- lib/params_ready/helpers/key_map.rb
Defined Under Namespace
Classes: Mapping
Class Method Summary collapse
Instance Method Summary collapse
- #freeze ⇒ Object
-
#initialize ⇒ KeyMap
constructor
A new instance of KeyMap.
- #map(from, to:) ⇒ Object
- #merge_or_add_mapping(mapping) ⇒ Object
- #remap(from, to, input) ⇒ Object
- #to_alternative(hash) ⇒ Object
- #to_standard(hash) ⇒ Object
Constructor Details
#initialize ⇒ KeyMap
Returns a new instance of KeyMap.
127 128 129 |
# File 'lib/params_ready/helpers/key_map.rb', line 127 def initialize @mappings = [] end |
Class Method Details
.split_map(array) ⇒ Object
167 168 169 170 171 172 173 |
# 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 |
Instance Method Details
#freeze ⇒ Object
162 163 164 165 |
# File 'lib/params_ready/helpers/key_map.rb', line 162 def freeze @mappings.each(&:freeze) super end |
#map(from, to:) ⇒ Object
131 132 133 134 135 136 137 |
# 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) ⇒ Object
139 140 141 142 143 144 145 146 |
# 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) ⇒ Object
156 157 158 159 160 |
# 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) ⇒ Object
152 153 154 |
# File 'lib/params_ready/helpers/key_map.rb', line 152 def to_alternative(hash) remap(:std, :alt, hash) end |
#to_standard(hash) ⇒ Object
148 149 150 |
# File 'lib/params_ready/helpers/key_map.rb', line 148 def to_standard(hash) remap(:alt, :std, hash) end |