Class: ConfigPlus::Node

Inherits:
Hash
  • Object
show all
Defined in:
lib/config_plus/node.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ Node

Returns a new instance of Node.



3
4
5
6
# File 'lib/config_plus/node.rb', line 3

def initialize(hash = nil)
  node = super()
  node.merge!(hash) if hash
end

Instance Method Details

#[](key) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/config_plus/node.rb', line 8

def [](key)
  value = self.fetch(key.to_s, nil)
  value = self.fetch(key.to_i, nil) if
    value.nil? and key.to_s =~ /\A\d+\z/
  return value unless value.is_a?(Hash)
  return value if value.is_a?(self.class)

  self.store(key.to_s, self.class.new(value))
end

#get(path) ⇒ Object



18
19
20
21
22
23
# File 'lib/config_plus/node.rb', line 18

def get(path)
  key, rest = path.split('.', 2)
  return self[key] unless rest
  return nil unless self[key]
  self[key].get(rest)
end

#merge(hash) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/config_plus/node.rb', line 31

def merge(hash)
  result = super
  result.instance_eval {
    hash.keys.each {|k| define_accessor(k) }
  }
  result
end

#merge!(hash) ⇒ Object



25
26
27
28
29
# File 'lib/config_plus/node.rb', line 25

def merge!(hash)
  result = super
  hash.keys.each {|k| define_accessor(k) }
  result
end