Class: AbsNode

Inherits:
Object
  • Object
show all
Defined in:
lib/treevisitor/abs_node.rb

Overview

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]

Direct Known Subclasses

LeafNode, TreeNode

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ AbsNode

Returns a new instance of AbsNode.



40
41
42
43
44
45
# File 'lib/treevisitor/abs_node.rb', line 40

def initialize( name )
  @parent = nil
  @name = name
  @prefix_path = nil
  invalidate
end

Class Attribute Details

.path_separatorObject

Returns the value of attribute path_separator.



30
31
32
# File 'lib/treevisitor/abs_node.rb', line 30

def path_separator
  @path_separator
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'lib/treevisitor/abs_node.rb', line 35

def name
  @name
end

#nextObject

Returns the value of attribute next.



38
39
40
# File 'lib/treevisitor/abs_node.rb', line 38

def next
  @next
end

#parentObject

Returns the value of attribute parent.



34
35
36
# File 'lib/treevisitor/abs_node.rb', line 34

def parent
  @parent
end

#prevObject

Returns the value of attribute prev.



37
38
39
# File 'lib/treevisitor/abs_node.rb', line 37

def prev
  @prev
end

Instance Method Details

#accept(visitor) ⇒ Object



107
108
109
# File 'lib/treevisitor/abs_node.rb', line 107

def accept( visitor )
  not_implemented
end

#depthObject



101
102
103
104
105
# File 'lib/treevisitor/abs_node.rb', line 101

def depth
  return @depth unless @depth.nil?
  @depth = @parent.nil? ? 1 : @parent.depth + 1
  @depth
end

#invalidateObject

invalidate cached info



50
51
52
53
54
# File 'lib/treevisitor/abs_node.rb', line 50

def invalidate
  @path = nil
  @path_with_prefix = nil
  @depth = nil    
end

#pathObject



81
82
83
84
85
86
87
88
89
# File 'lib/treevisitor/abs_node.rb', line 81

def path
  return @path unless @path.nil?
  if @parent.nil?
    @path = @name
  else
    @path = @parent.path + AbsNode::path_separator + @name
  end
  @path
end

#path_with_prefixObject



91
92
93
94
95
96
97
98
99
# File 'lib/treevisitor/abs_node.rb', line 91

def path_with_prefix
  return @path_with_prefix unless @path_with_prefix.nil?
  if @parent.nil?
    @path_with_prefix = @prefix_path.nil? ? @name : @prefix_path + @name
  else
    @path_with_prefix = @parent.path_with_prefix + AbsNode::path_separator + @name
  end
  @path_with_prefix
end

#prefix_pathObject



56
57
58
59
60
61
# File 'lib/treevisitor/abs_node.rb', line 56

def prefix_path
  if not @parent.nil?
    raise "Not root!!"
  end
  @prefix_path
end

#prefix_path=(prefix) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/treevisitor/abs_node.rb', line 63

def prefix_path=( prefix )
  if not @parent.nil?
    raise "Not root!!"
  end
  if prefix != @prefix_path
    @prefix_path = prefix
    invalidate
  end
end

#rootObject



73
74
75
76
77
78
79
# File 'lib/treevisitor/abs_node.rb', line 73

def root
  if root?
    self
  else
    parent.root
  end
end

#to_sObject



111
112
113
# File 'lib/treevisitor/abs_node.rb', line 111

def to_s
  @name.to_s
end