Class: Astrails::Safe::Config::Node
- Inherits:
-
Object
- Object
- Astrails::Safe::Config::Node
- Includes:
- Enumerable
- Defined in:
- lib/astrails/safe/config/node.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #dump(indent = "") ⇒ Object
- #each(&block) ⇒ Object
-
#find(*path) ⇒ Object
(also: #[])
recursive find starts at the node and continues to the parent.
-
#get(*path) ⇒ Object
looks for the path from this node DOWN.
-
#initialize(parent = nil, data = {}, &block) ⇒ Node
constructor
A new instance of Node.
- #set(key, value, &block) ⇒ Object (also: #[]=)
- #to_hash ⇒ Object
Constructor Details
#initialize(parent = nil, data = {}, &block) ⇒ Node
Returns a new instance of Node.
7 8 9 10 11 |
# File 'lib/astrails/safe/config/node.rb', line 7 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 |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/astrails/safe/config/node.rb', line 6 def parent @parent end |
Instance Method Details
#dump(indent = "") ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/astrails/safe/config/node.rb', line 53 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) ⇒ Object
40 41 42 |
# File 'lib/astrails/safe/config/node.rb', line 40 def each(&block) @data.each(&block) end |
#find(*path) ⇒ Object Also known as: []
recursive find starts at the node and continues to the parent
24 25 26 |
# File 'lib/astrails/safe/config/node.rb', line 24 def find(*path) get(*path) || @parent && @parent.find(*path) end |
#get(*path) ⇒ Object
looks for the path from this node DOWN. will not delegate to parent
14 15 16 17 18 19 20 |
# File 'lib/astrails/safe/config/node.rb', line 14 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) ⇒ Object Also known as: []=
29 30 31 32 33 34 35 36 37 |
# File 'lib/astrails/safe/config/node.rb', line 29 def set(key, value, &block) @data[key.to_s] = if value.is_a?(Hash) Node.new(self, value, &block) else raise(ArgumentError, "#{key}: no block supported for simple values") if block value end end |
#to_hash ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/astrails/safe/config/node.rb', line 45 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 |