Class: ConfigPlus::Node

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/config_plus/node.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Node

Returns a new instance of Node.



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

def initialize(collection)
  data = data_of(collection)
  @node = ::ConfigPlus::Collection.generate_for(data)
  self.merge!(data) if hash
end

Instance Method Details

#==(object) ⇒ Object



62
63
64
# File 'lib/config_plus/node.rb', line 62

def ==(object)
  node.data == data_of(object)
end

#[](key) ⇒ Object



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

def [](key)
  value = node[key]
  return value if value.is_a?(self.class)

  case value
  when Hash, Array, ::ConfigPlus::Collection
    node.store(key.to_s, self.class.new(value))
  else
    value
  end
end

#dig(*keys) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/config_plus/node.rb', line 49

def dig(*keys)
  key = keys.first
  rest = keys[1..-1]
  return self[key] if rest.empty?
  return nil unless self[key]
  self[key].dig(*rest)
end

#get(path) ⇒ Object



42
43
44
45
46
47
# File 'lib/config_plus/node.rb', line 42

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

#merge(collection) ⇒ Object



57
58
59
60
# File 'lib/config_plus/node.rb', line 57

def merge(collection)
  data = data_of(collection)
  self.class.new(node.merge(convert(data)))
end