Class: Cuporter::Node

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/cuporter/node.rb

Direct Known Subclasses

TagListNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Node

Returns a new instance of Node.



10
11
12
13
# File 'lib/cuporter/node.rb', line 10

def initialize(name)
  @name = name.to_s.strip
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



7
8
9
# File 'lib/cuporter/node.rb', line 7

def children
  @children
end

#fileObject

Returns the value of attribute file.



7
8
9
# File 'lib/cuporter/node.rb', line 7

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/cuporter/node.rb', line 6

def name
  @name
end

#numberObject

Returns the value of attribute number.



7
8
9
# File 'lib/cuporter/node.rb', line 7

def number
  @number
end

#numerable=(value) ⇒ Object (writeonly)

Sets the attribute numerable

Parameters:

  • value

    the value to set the attribute numerable to.



85
86
87
# File 'lib/cuporter/node.rb', line 85

def numerable=(value)
  @numerable = value
end

Instance Method Details

#<=>(other) ⇒ Object

sort on name or substring of name after any ‘:’



61
62
63
# File 'lib/cuporter/node.rb', line 61

def <=>(other)
  name_without_title <=> other.name_without_title
end

#add_child(node) ⇒ Object

will not add duplicate



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

def add_child(node)
  @children << node unless has_child?(node)
end

#eql?(other) ⇒ Boolean Also known as: ==

value equivalence

Returns:

  • (Boolean)


66
67
68
# File 'lib/cuporter/node.rb', line 66

def eql?(other)
  name == other.name && children == other.children
end

#find(node) ⇒ Object Also known as: has_child?



42
43
44
# File 'lib/cuporter/node.rb', line 42

def find(node)
  children.find {|c| c == node}
end

#find_by_name(name) ⇒ Object Also known as: []



37
38
39
# File 'lib/cuporter/node.rb', line 37

def find_by_name(name)
  children.find {|c| c.name == name.to_s}
end

#find_or_create_child(name) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/cuporter/node.rb', line 28

def find_or_create_child(name)
  child_node = self[name]
  unless child_node
    children << Node.new(name)
    child_node = children.last
  end
  child_node
end

#has_children?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/cuporter/node.rb', line 15

def has_children?
  @children.size > 0
end

#name_without_titleObject



47
48
49
# File 'lib/cuporter/node.rb', line 47

def name_without_title
  @name_without_title ||= name.split(/:\s+/).last
end

#namesObject



24
25
26
# File 'lib/cuporter/node.rb', line 24

def names
  children.collect {|c| c.name }
end

#number_all_descendantsObject



77
78
79
80
# File 'lib/cuporter/node.rb', line 77

def number_all_descendants
  @numberer = NodeNumberer.new
  @numberer.number(self)
end

#numerable?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/cuporter/node.rb', line 82

def numerable?
  @numerable.nil? ? !has_children? : @numerable
end

#sort!Object



56
57
58
# File 'lib/cuporter/node.rb', line 56

def sort!
  children.sort!
end

#sort_all_descendants!Object



51
52
53
54
# File 'lib/cuporter/node.rb', line 51

def sort_all_descendants!
  sort!
  children.each {|child| child.sort_all_descendants! }
end

#totalObject



72
73
74
75
# File 'lib/cuporter/node.rb', line 72

def total
  number_all_descendants unless @numberer
  @numberer.total
end