Class: TreeRb::AbsNode Abstract

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

Overview

This class is abstract.

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]

Direct Known Subclasses

LeafNode, TreeNode

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ AbsNode

Create a new AbsNode

Parameters:

  • content

    of node



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_separatorObject

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

#contentObject (readonly)

Returns the value of attribute content.



36
37
38
# File 'lib/tree_rb/core/abs_node.rb', line 36

def content
  @content
end

#nextObject

Returns the value of attribute next.



39
40
41
# File 'lib/tree_rb/core/abs_node.rb', line 39

def next
  @next
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#prevObject

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

#depthFixNum

Returns depth of this node.

Returns:

  • (FixNum)

    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

#invalidateObject

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

#pathString

Returns path to this node.

Returns:

  • (String)

    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_prefixString

Returns path to this node with prefix.

Returns:

  • (String)

    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_pathObject

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

#rootObject

Return the root of this node

Returns:

  • AbsNode



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