Class: Tooled::TreeNode
- Inherits:
-
Object
- Object
- Tooled::TreeNode
- Defined in:
- lib/tooled/tree_node.rb
Instance Method Summary collapse
- #ancestors ⇒ Object
- #children ⇒ Object
- #descendants ⇒ Object
- #each_child ⇒ Object
-
#initialize ⇒ TreeNode
constructor
A new instance of TreeNode.
- #parent ⇒ Object
- #parent=(node) ⇒ Object
Constructor Details
#initialize ⇒ TreeNode
Returns a new instance of TreeNode.
5 6 7 8 |
# File 'lib/tooled/tree_node.rb', line 5 def initialize @children = TreeNodeCollection.new parent = nil end |
Instance Method Details
#ancestors ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tooled/tree_node.rb', line 31 def ancestors enum = ::Enumerator.new do |yielder| parent_node = parent while parent_node yielder << parent_node parent_node = parent_node.parent end end TreeNodeCollection.new(enum.respond_to?(:lazy) ? enum.lazy : enum) end |
#children ⇒ Object
10 11 12 |
# File 'lib/tooled/tree_node.rb', line 10 def children @children end |
#descendants ⇒ Object
26 27 28 29 |
# File 'lib/tooled/tree_node.rb', line 26 def descendants enum = to_enum(:each_child) TreeNodeCollection.new(enum.respond_to?(:lazy) ? enum.lazy : enum) end |
#each_child ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/tooled/tree_node.rb', line 14 def each_child return to_enum(:each_child) unless block_given? children.each do |child| yield child child.each_child { |c| yield c } end end |
#parent ⇒ Object
22 23 24 |
# File 'lib/tooled/tree_node.rb', line 22 def parent @parent end |
#parent=(node) ⇒ Object
42 43 44 45 46 |
# File 'lib/tooled/tree_node.rb', line 42 def parent=(node) raise InvalidNodeException.new("`parent' can only be a TreeNode") unless node.is_a?(TreeNode) raise InvalidNodeException.new("`parent' can not be a descendant") if descendants.member?(node) @parent = node.tap { |n| n.children.add(self) } end |