Module: Stretchy::Utils::DotHandler

Defined in:
lib/stretchy/utils/dot_handler.rb

Class Method Summary collapse

Class Method Details

.convert_from_dotted_keys(hash) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stretchy/utils/dot_handler.rb', line 7

def self.convert_from_dotted_keys(hash)
  new_hash = {}

  hash.each do |key, value|
    h = new_hash

    parts = key.to_s.split('.')
    while parts.length > 0
      new_key = parts[0]
      rest = parts[1..-1]

      if not h.instance_of? Hash
        raise ArgumentError, "Trying to set key #{new_key} to value #{value} on a non hash #{h}\n"
      end

      if rest.length == 0
        if h[new_key].instance_of? Hash
          raise ArgumentError, "Replacing a hash with a scalar. key #{new_key}, value #{value}, current value #{h[new_key]}\n"
        end

        h.store(new_key, value)
        break
      end

      if h[new_key].nil?
        h[new_key] = {}
      end

      h = h[new_key]
      parts = rest
    end
  end

  new_hash
end