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.



80
81
82
# File 'lib/cuporter/node.rb', line 80

def numerable=(value)
  @numerable = value
end

Instance Method Details

#<=>(other) ⇒ Object

sort on: file path, name, substring of name after any ‘:’



52
53
54
55
56
57
58
# File 'lib/cuporter/node.rb', line 52

def <=>(other)
  if file
    file <=> other.file
  else
    name_without_title <=> other.name_without_title
  end
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)


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

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

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



33
34
35
# File 'lib/cuporter/node.rb', line 33

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

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



28
29
30
# File 'lib/cuporter/node.rb', line 28

def find_by_name(name)
  children.find {|c| c.name == name.to_s}
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



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

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



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

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

#numerable?Boolean

Returns:

  • (Boolean)


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

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

#sort!Object



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

def sort!
  children.sort!
end

#sort_all_descendants!Object



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

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

#totalObject



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

def total
  number_all_descendants unless @numberer
  @numberer.total
end