Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/hash.rb

Instance Method Summary collapse

Instance Method Details

#unwind(separator = ".", key = nil, start = {}) ⇒ Object

>> “d”=>“bar”, “c”=>“j”}, “q”=>“asd”}.unwind

> “a.b.d”=>“bar”, “q”=>“asd”, “a.b.c”=>“foo”



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/i18n/hash.rb', line 5

def unwind(separator = ".", key = nil, start = {}) 
  self.inject(start){|hash,k|
    expanded_key = [key, k[0]].compact.join( separator )
    if k[1].is_a? Hash
      k[1].unwind(separator, expanded_key, hash)
    else
      hash[ expanded_key ] = k[1]
    end
    hash
  }
end

#wind(separator = ".", key = nil, start = {}) ⇒ Object

>> => “foo”, “a.b.d” => “bar”, “a.c” => “j”, “q” => “asd”.wind

> “d”=>“bar”, “c”=>“j”}, “q”=>“asd”}



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/i18n/hash.rb', line 19

def wind(separator = ".", key = nil, start = {})
  wound = Hash.new
  self.each {|key, value|
    keys = key.split( separator )
    index = 0
    keys.inject(wound){|h,v|
      index += 1
      if index >= keys.size
        h[v.to_sym] = value
        break
      else
        h[v.to_sym] ||= {}
      end
    }
  }
  wound
end