Module: Nx

Included in:
Hash
Defined in:
lib/nx.rb,
lib/templates/version.rb

Constant Summary collapse

VERSION =
"0.1.4"

Instance Method Summary collapse

Instance Method Details

#get(path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nx.rb', line 25

def get(path)
  result = self
  path.split(".").each do |dot_part|
    dot_part.split("[").each do |part|
      if part.include?("]")
        index = part.to_i
        result = result[index] rescue nil
      else
        result = result[part] rescue nil
      end
    end
  end
  result
end

#mix(*args) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/nx.rb', line 40

def mix(*args)
  result = {}
  args.each do |arg|
    result.merge! arg
  end
  result
end

#set(path, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nx.rb', line 4

def set(path, value)
  me = self || {}
  keys = []
  path.split(".").each do |dot_part|
    dot_part.split("[").each do |part|
      keys << (part.include?("]") ? part.to_i : part)
    end
  end

  # set by path:
  keys[0..-2].each_with_index do |key, index|
    the_key = key
    the_key = yeild(the_key) if block_given?
    me = me[the_key] = me[the_key] || (keys[index + 1].class == String ? {} : [])
  end

  the_key = keys[-1]
  the_key = yeild(the_key) if block_given?
  me[the_key] = value
end