Class: NxtSupport::HashTranslator::HashTranslationService
- Inherits:
-
Object
- Object
- NxtSupport::HashTranslator::HashTranslationService
- Includes:
- NxtInit
- Defined in:
- lib/nxt_support/util/hash_translator.rb
Defined Under Namespace
Classes: InvalidTranslationArgument
Instance Method Summary collapse
Instance Method Details
#call ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/nxt_support/util/hash_translator.rb', line 11 def call tuples.inject(hash.with_indifferent_access) do |acc, (old_key, new_key)| if new_key.is_a?(Hash) raise InvalidTranslationArgument.new("#{new_key} hash must contain only 1 key-value pair!") if new_key.size > 1 key, value = new_key.shift raise InvalidTranslationArgument.new("#{value} must be a callable block!") unless value.respond_to?(:call) acc[key] = value.call(acc.delete(old_key)) elsif new_key.is_a?(Array) value = acc.delete(old_key) new_key.each { |key| acc[key] = value } else acc[new_key] = acc.delete(old_key) end acc end end |