Class: TreeRb::AbsNode Abstract
- Inherits:
-
Object
- Object
- TreeRb::AbsNode
- Defined in:
- lib/tree_rb/core/abs_node.rb
Overview
Subclass to implement a concrete Node (Leaf or Tree).
Abstract Node
Class hierarchy
AbsNode has a name, a parent
^ and define a path i.e. concatenation ancestor names
|
|-- LeafNode
|
`-- TreeNode
Object diagram
TreeNode (parent: nil)
|
|--->[ LeafNode, LeafNode, LeafNode ]
|
|--->[ TreeNode, TreeNode ]
|
|--> [LeafNode]
|
`--> [TreeNode, TreeNode]
Class Attribute Summary collapse
-
.path_separator ⇒ Object
Returns the value of attribute path_separator.
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#next ⇒ Object
Returns the value of attribute next.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#prev ⇒ Object
Returns the value of attribute prev.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
Accept a node visitor.
-
#depth ⇒ FixNum
Depth of this node.
-
#initialize(content) ⇒ AbsNode
constructor
Create a new AbsNode.
-
#invalidate ⇒ Object
invalidate cached path info.
-
#path ⇒ String
Path to this node.
-
#path_with_prefix ⇒ String
Path to this node with prefix.
-
#prefix_path ⇒ Object
Root node could have assigned a path.
- #prefix_path=(prefix) ⇒ Object
-
#root ⇒ Object
Return the root of this node.
Constructor Details
#initialize(content) ⇒ AbsNode
Create a new AbsNode
46 47 48 49 50 51 |
# File 'lib/tree_rb/core/abs_node.rb', line 46 def initialize(content) @parent = nil @content = content @prefix_path = nil invalidate end |
Class Attribute Details
.path_separator ⇒ Object
Returns the value of attribute path_separator.
31 32 33 |
# File 'lib/tree_rb/core/abs_node.rb', line 31 def path_separator @path_separator end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
36 37 38 |
# File 'lib/tree_rb/core/abs_node.rb', line 36 def content @content end |
#next ⇒ Object
Returns the value of attribute next.
39 40 41 |
# File 'lib/tree_rb/core/abs_node.rb', line 39 def next @next end |
#parent ⇒ Object
Returns the value of attribute parent.
35 36 37 |
# File 'lib/tree_rb/core/abs_node.rb', line 35 def parent @parent end |
#prev ⇒ Object
Returns the value of attribute prev.
38 39 40 |
# File 'lib/tree_rb/core/abs_node.rb', line 38 def prev @prev end |
Instance Method Details
#accept(visitor) ⇒ Object
Accept a node visitor
128 129 130 |
# File 'lib/tree_rb/core/abs_node.rb', line 128 def accept(visitor) not_implemented end |
#depth ⇒ FixNum
Returns depth of this node.
120 121 122 123 |
# File 'lib/tree_rb/core/abs_node.rb', line 120 def depth return @depth unless @depth.nil? @depth = @parent.nil? ? 1 : @parent.depth + 1 end |
#invalidate ⇒ Object
invalidate cached path info
56 57 58 59 60 61 |
# File 'lib/tree_rb/core/abs_node.rb', line 56 def invalidate @path = nil @path_with_prefix = nil @depth = nil @root = nil end |
#path ⇒ String
Returns path to this node.
95 96 97 98 99 100 101 102 103 |
# File 'lib/tree_rb/core/abs_node.rb', line 95 def path return @path unless @path.nil? if @parent.nil? @path = @content else @path = @parent.path + AbsNode::path_separator + @content end @path end |
#path_with_prefix ⇒ String
Returns path to this node with prefix.
108 109 110 111 112 113 114 115 |
# File 'lib/tree_rb/core/abs_node.rb', line 108 def path_with_prefix return @path_with_prefix if @path_with_prefix if root.prefix_path @path_with_prefix = root.prefix_path + path else @path_with_prefix = path end end |
#prefix_path ⇒ Object
Root node could have assigned a path
66 67 68 69 |
# File 'lib/tree_rb/core/abs_node.rb', line 66 def prefix_path raise 'Not root!!' unless @parent.nil? @prefix_path end |
#prefix_path=(prefix) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tree_rb/core/abs_node.rb', line 71 def prefix_path=(prefix) raise 'Not root!!' unless @parent.nil? if prefix != @prefix_path @prefix_path = prefix if prefix and prefix !~ /\/$/ @prefix_path += AbsNode::path_separator end invalidate end end |
#root ⇒ Object
Return the root of this node
87 88 89 90 |
# File 'lib/tree_rb/core/abs_node.rb', line 87 def root return @root if @root @root = parent.nil? ? self : parent.root end |