Class: Dech::HashKeyMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/dech/hash_key_mapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, mapping = {}) ⇒ HashKeyMapper

Returns a new instance of HashKeyMapper.



11
12
13
14
# File 'lib/dech/hash_key_mapper.rb', line 11

def initialize(hash={}, mapping={})
  @hash = hash
  @mapping = mapping
end

Class Method Details

.map(hash, mapping) ⇒ Object



6
7
8
# File 'lib/dech/hash_key_mapper.rb', line 6

def map(hash, mapping)
  new(hash, mapping).map
end

Instance Method Details

#mapObject



16
17
18
19
20
21
22
23
24
# File 'lib/dech/hash_key_mapper.rb', line 16

def map
  new_hash = {}
  @hash.each do |k, v|
    [@mapping[k] || k].flatten.each do |new_key|
      new_hash[new_key] = v
    end
  end
  new_hash
end