Class: AbsNode

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

Overview

Nodo Astratto

Gerarchia delle classi

AbsNode ha un nome, un parent
 |      definisce un path
 |
 |- LeafNode
 |
 |- TreeNode

Direct Known Subclasses

LeafNode, TreeNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ AbsNode

Returns a new instance of AbsNode.



27
28
29
30
31
32
# File 'lib/tree_visitor/abs_node.rb', line 27

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/tree_visitor/abs_node.rb', line 20

def name
  @name
end

#nextObject

Returns the value of attribute next.



24
25
26
# File 'lib/tree_visitor/abs_node.rb', line 24

def next
  @next
end

#parentObject

Returns the value of attribute parent.



19
20
21
# File 'lib/tree_visitor/abs_node.rb', line 19

def parent
  @parent
end

#prevObject

solo TreeNode puo’ scrivere vedi funzione add_leaf



23
24
25
# File 'lib/tree_visitor/abs_node.rb', line 23

def prev
  @prev
end

Instance Method Details

#accept(visitor) ⇒ Object



106
107
108
# File 'lib/tree_visitor/abs_node.rb', line 106

def accept( visitor )
  not_implemented
end

#depthObject



96
97
98
99
100
101
102
103
104
# File 'lib/tree_visitor/abs_node.rb', line 96

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

#invalidateObject

invalidate cached info



37
38
39
40
41
# File 'lib/tree_visitor/abs_node.rb', line 37

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

#pathObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tree_visitor/abs_node.rb', line 68

def path
  return @path unless @path.nil?
  if @parent.nil?
    if @prefix_path
      @path = @prefix_path + @name
    else
      @path = @name
    end
  else
    @path = File.join( @parent.path, @name )
  end
  @path
end

#path_from_rootObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tree_visitor/abs_node.rb', line 82

def path_from_root
  return @path_from_root unless @path_from_root.nil?
  if @parent.nil?
    if @prefix_path
      @path_from_root = @prefix_path
    else
      @path_from_root = ""
    end
  else
    @path_from_root = File.join( @parent.path_from_root, @name )
  end
  @path_from_root
end

#prefix_pathObject



43
44
45
46
47
48
# File 'lib/tree_visitor/abs_node.rb', line 43

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

#prefix_path=(prefix) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/tree_visitor/abs_node.rb', line 50

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

#rootObject



60
61
62
63
64
65
66
# File 'lib/tree_visitor/abs_node.rb', line 60

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