Class: Redwood::Node

Inherits:
Object
  • Object
show all
Includes:
Redwood
Defined in:
lib/redwood/node.rb

Direct Known Subclasses

FileNode

Constant Summary

Constants included from Redwood

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Redwood

#ancestors, #children, #depth, #descendants, #graft, #has_children?, #height, #leaf?, #only_child?, #parent, #prune, #root, #root?, #siblings, #unlink, #view, #walk

Constructor Details

#initialize(name = nil, parent = nil) ⇒ Node

Returns a new instance of Node.



10
11
12
13
# File 'lib/redwood/node.rb', line 10

def initialize(name=nil, parent=nil)
  @name = name
  @parent = parent
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/redwood/node.rb', line 7

def name
  @name
end

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/redwood/node.rb', line 8

def value
  @value
end

Instance Method Details

#<<(child) ⇒ Object

Graft a child



24
25
26
# File 'lib/redwood/node.rb', line 24

def <<(child)
  graft child
end

#[](key) ⇒ Object

Lookup a child node by its name



29
30
31
32
# File 'lib/redwood/node.rb', line 29

def [](key)
  selected_child = children.select {|child| child.name == key }
  selected_child.size.eql?(1) ? selected_child.first : selected_child
end

#add_child(name) {|child| ... } ⇒ Object

Creates a child, adds it to children, and returns the child

Yields:

  • (child)


16
17
18
19
20
21
# File 'lib/redwood/node.rb', line 16

def add_child(name)
  child = self.class.new(name, self)
  yield child if block_given?
  children << child
  child
end