class Astrails::Safe::Config::Node

Constants

MULTIVALUES

Attributes

data[R]
parent[R]

Public Class Methods

new(parent = nil, data = {}, &block) click to toggle source
# File lib/astrails/safe/config/node.rb, line 8
def initialize(parent = nil, data = {}, &block)
  @parent, @data = parent, {}
  data.each { |k, v| self[k] = v }
  Builder.new(self).instance_eval(&block) if block
end

Public Instance Methods

[](*path)
Alias for: find
[]=(key, value, &block)
Alias for: set
dump(indent = "") click to toggle source
# File lib/astrails/safe/config/node.rb, line 63
def dump(indent = "")
  @data.each do |key, value|
    if value.is_a?(Node)
      puts "#{indent}#{key}:"
      value.dump(indent + "    ")
    else
      puts "#{indent}#{key}: #{value.inspect}"
    end
  end
end
each(&block) click to toggle source
# File lib/astrails/safe/config/node.rb, line 50
def each(&block)
  @data.each(&block)
end
find(*path) click to toggle source

recursive find starts at the node and continues to the parent

# File lib/astrails/safe/config/node.rb, line 25
def find(*path)
  get(*path) || @parent && @parent.find(*path)
end
Also aliased as: []
get(*path) click to toggle source

looks for the path from this node DOWN. will not delegate to parent

# File lib/astrails/safe/config/node.rb, line 15
def get(*path)
  key = path.shift
  value = @data[key.to_s]
  return value if value && path.empty?

  value && value.get(*path)
end
set(key, value, &block) click to toggle source
# File lib/astrails/safe/config/node.rb, line 31
def set(key, value, &block)
  if @data[key.to_s]
    raise(ArgumentError, "duplicate value for '#{key}'") if value.is_a?(Hash) || !MULTIVALUES.include?(key.to_s)
  end

  if value.is_a?(Hash)
    @data[key.to_s] = Node.new(self, value, &block)
  else
    raise(ArgumentError, "#{key}: no block supported for simple values") if block
    if @data[key.to_s]
      @data[key.to_s] = [*@data[key.to_s]] + [value]
    else
      @data[key.to_s] = value
    end
    value
  end
end
Also aliased as: []=
to_hash() click to toggle source
# File lib/astrails/safe/config/node.rb, line 55
def to_hash
  @data.keys.inject({}) do |res, key|
    value = @data[key]
    res[key] = value.is_a?(Node) ? value.to_hash : value
    res
  end
end