Module: Antrapol::ToolRack::HashConfig

Includes:
ConditionUtils
Defined in:
lib/toolrack/hash_config.rb

Defined Under Namespace

Classes: HashConfigError

Instance Method Summary collapse

Instance Method Details

#hcdeliObject



14
15
16
17
18
19
# File 'lib/toolrack/hash_config.rb', line 14

def hcdeli
  if is_empty?(@hcdeli)
    @hcdeli = "."
  end
  @hcdeli
end

#hcdeli=(deli) ⇒ Object



11
12
13
# File 'lib/toolrack/hash_config.rb', line 11

def hcdeli=(deli)
  @hcdeli = deli
end

#hcget(key, defVal = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/toolrack/hash_config.rb', line 45

def hcget(key, defVal = nil)
  if key.nil?
    return nil
  else
    keys = transform_keys(key)
    node = hcConf
    keys.each do |k|
      if node.keys.include?(k)
        node = node[k] 
      else
        node = nil
        break
      end
    end
    node.nil? ? defVal : node
  end
end

#hcinclude?(key) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/toolrack/hash_config.rb', line 63

def hcinclude?(key)
  if key.nil?
    return false
  else
    keys = transform_keys(key) 
    node = hcConf 
    keys.each do |k|
      if node.is_a?(Hash)
        if node.keys.include?(k)
          node = node[k]
        else
          node = nil
          break
        end
      else
        node = nil
      end
    end
    not node.nil?
  end
end

#hcset(key, value) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/toolrack/hash_config.rb', line 21

def hcset(key, value)
  raise HashConfigError, "Nil key is not supported" if is_empty?(key)

  keys = transform_keys(key)
  root = hcConf
  keys.each do |k|
    if root.is_a?(Hash)
      if not root.keys.include?(k)
        root[k] = { }
      end

      if k == keys[-1]
        root[k] = value
      else
        root = root[k]
      end
    
    else
      raise HashConfigError, "The key '#{k}' currently not tie to a hash. It is an #{root.class} object of value '#{root}'. There is no way to add another value to key '#{key}'. Try to change it to Hash object instead." 
    end
  end
  root
end