class ConfigPlus::Node

Attributes

node[R]

Public Class Methods

new(collection) click to toggle source
# 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

Public Instance Methods

==(object) click to toggle source
# File lib/config_plus/node.rb, line 62
def ==(object)
  node.data == data_of(object)
end
[](key) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/config_plus/node.rb, line 57
def merge(collection)
  data = data_of(collection)
  self.class.new(node.merge(convert(data)))
end

Protected Instance Methods

merge!(collection) click to toggle source
# File lib/config_plus/node.rb, line 70
def merge!(collection)
  data = data_of(collection)
  node.merge!(convert(data)).tap {
    (node.hash? ? data : node).keys.each {|k| define_accessor(k) }
  }
end

Private Instance Methods

convert(collection) click to toggle source
# File lib/config_plus/node.rb, line 83
def convert(collection)
  case collection
  when Array
    collection.map do |data|
      data.is_a?(Array) || data.is_a?(Hash) ? self.class.new(data) : data
    end
  else
    collection
  end
end
data_of(collection) click to toggle source
# File lib/config_plus/node.rb, line 79
def data_of(collection)
  collection.is_a?(self.class) ? collection.node : collection
end
define_accessor(method_name) click to toggle source
# File lib/config_plus/node.rb, line 94
def define_accessor(method_name)
  name = method_name.to_s
  return if respond_to?(name) or
    private_methods.include?(name.to_sym)
  singleton_class.class_exec(self) do |node|
    define_method name, lambda { node[method_name] }
  end
end