Class: Gemlist::SpecNode
- Inherits:
-
Object
- Object
- Gemlist::SpecNode
- Includes:
- Enumerable
- Defined in:
- lib/gemlist/spec_node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #<<(node) ⇒ Object
- #depth_first_children_first ⇒ Object
- #descends_from?(*dependencies) ⇒ Boolean
- #each(&block) ⇒ Object
-
#initialize(name, content) ⇒ SpecNode
constructor
A new instance of SpecNode.
- #lineage ⇒ Object
- #parentage ⇒ Object
- #root? ⇒ Boolean
- #size ⇒ Object
- #total_dependency_count ⇒ Object
Constructor Details
#initialize(name, content) ⇒ SpecNode
Returns a new instance of SpecNode.
8 9 10 11 12 13 |
# File 'lib/gemlist/spec_node.rb', line 8 def initialize(name, content) @name = name @content = content @children = [] @parent = nil end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
5 6 7 |
# File 'lib/gemlist/spec_node.rb', line 5 def children @children end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
5 6 7 |
# File 'lib/gemlist/spec_node.rb', line 5 def content @content end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/gemlist/spec_node.rb', line 5 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
6 7 8 |
# File 'lib/gemlist/spec_node.rb', line 6 def parent @parent end |
Instance Method Details
#<<(node) ⇒ Object
15 16 17 18 19 |
# File 'lib/gemlist/spec_node.rb', line 15 def <<(node) @children << node node.parent = self node end |
#depth_first_children_first ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/gemlist/spec_node.rb', line 47 def depth_first_children_first sorted_children = children.sort_by(&:total_dependency_count) parent = self Enumerator.new do |yielder| sorted_children.each do |child| child.depth_first_children_first.each do |node| yielder << node end end yielder << parent end end |
#descends_from?(*dependencies) ⇒ Boolean
66 67 68 69 |
# File 'lib/gemlist/spec_node.rb', line 66 def descends_from?(*dependencies) !root? && dependencies.map(&:name).include?(lineage.first.name) end |
#each(&block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/gemlist/spec_node.rb', line 21 def each(&block) return to_enum unless block_given? unvisited = [self] until unvisited.empty? current = unvisited.shift if current yield current unvisited.unshift(*current.children) end end return self if block_given? end |
#lineage ⇒ Object
62 63 64 |
# File 'lib/gemlist/spec_node.rb', line 62 def lineage [self, *parentage].reverse.reject(&:root?) end |
#parentage ⇒ Object
71 72 73 74 75 |
# File 'lib/gemlist/spec_node.rb', line 71 def parentage return [] if root? [parent] + parent.parentage end |
#root? ⇒ Boolean
77 78 79 |
# File 'lib/gemlist/spec_node.rb', line 77 def root? parent.nil? end |
#size ⇒ Object
43 44 45 |
# File 'lib/gemlist/spec_node.rb', line 43 def size to_a.size end |
#total_dependency_count ⇒ Object
39 40 41 |
# File 'lib/gemlist/spec_node.rb', line 39 def total_dependency_count size - 1 end |