Class: Hutils::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, common) ⇒ Node

Returns a new instance of Node.



8
9
10
11
12
13
# File 'lib/hutils.rb', line 8

def initialize(parent, common)
  @common = common
  @parent = parent
  @slots = []
  @tags = {}
end

Instance Attribute Details

#commonObject

The set of common attributes that are shared by all slots in this branch of the tree.



17
18
19
# File 'lib/hutils.rb', line 17

def common
  @common
end

#parentObject

Parent node.



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

def parent
  @parent
end

#slotsObject

Ordered child nodes of this node.



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

def slots
  @slots
end

#tagsObject

Arbitrary tags that applications can associate on a node.



26
27
28
# File 'lib/hutils.rb', line 26

def tags
  @tags
end

Instance Method Details

#common_completeObject

The set of common attributes that are shared by all slots in this branch of the tree, but accounting for all parent nodes as well.



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

def common_complete
  @common.merge(@parent ? @parent.common_complete : {})
end

#depthObject



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

def depth
  @parent ? @parent.depth + 1 : 0
end


38
39
40
41
42
# File 'lib/hutils.rb', line 38

def print
  indent = "  " * depth
  $stdout.puts "#{indent}[ #{depth} ]#{@common}"
  slots.each { |node| node.print }
end

#replace_slot(old, new) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/hutils.rb', line 44

def replace_slot(old, new)
  @slots.each_with_index do |node, i|
    if old == node
      @slots[i] = new
      return
    end
  end
  abort("bad replace")
end

#rootObject



54
55
56
# File 'lib/hutils.rb', line 54

def root
  @parent ? @parent.root : self
end

#root?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/hutils.rb', line 58

def root?
  @parent == nil
end