Class: Cuporter::Node
- Inherits:
-
Object
- Object
- Cuporter::Node
- Includes:
- Comparable
- Defined in:
- lib/cuporter/node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#file ⇒ Object
Returns the value of attribute file.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#number ⇒ Object
Returns the value of attribute number.
-
#numerable ⇒ Object
writeonly
Sets the attribute numerable.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
sort on name or substring of name after any ‘:’.
-
#add_child(node) ⇒ Object
will not add duplicate.
-
#eql?(other) ⇒ Boolean
(also: #==)
value equivalence.
- #find(node) ⇒ Object (also: #has_child?)
- #find_by_name(name) ⇒ Object (also: #[])
- #find_or_create_child(name) ⇒ Object
- #has_children? ⇒ Boolean
-
#initialize(name) ⇒ Node
constructor
A new instance of Node.
- #name_without_title ⇒ Object
- #names ⇒ Object
- #number_all_descendants ⇒ Object
- #numerable? ⇒ Boolean
- #sort! ⇒ Object
- #sort_all_descendants! ⇒ Object
- #total ⇒ Object
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
#children ⇒ Object
Returns the value of attribute children.
7 8 9 |
# File 'lib/cuporter/node.rb', line 7 def children @children end |
#file ⇒ Object
Returns the value of attribute file.
7 8 9 |
# File 'lib/cuporter/node.rb', line 7 def file @file end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/cuporter/node.rb', line 6 def name @name end |
#number ⇒ Object
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
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
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
15 16 17 |
# File 'lib/cuporter/node.rb', line 15 def has_children? @children.size > 0 end |
#name_without_title ⇒ Object
47 48 49 |
# File 'lib/cuporter/node.rb', line 47 def name_without_title @name_without_title ||= name.split(/:\s+/).last end |
#names ⇒ Object
24 25 26 |
# File 'lib/cuporter/node.rb', line 24 def names children.collect {|c| c.name } end |
#number_all_descendants ⇒ Object
77 78 79 80 |
# File 'lib/cuporter/node.rb', line 77 def number_all_descendants @numberer = NodeNumberer.new @numberer.number(self) end |
#numerable? ⇒ 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 |
#total ⇒ Object
72 73 74 75 |
# File 'lib/cuporter/node.rb', line 72 def total number_all_descendants unless @numberer @numberer.total end |