Class: Gemlist::SpecNode

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gemlist/spec_node.rb

Direct Known Subclasses

SpecTree

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/gemlist/spec_node.rb', line 5

def children
  @children
end

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/gemlist/spec_node.rb', line 5

def content
  @content
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/gemlist/spec_node.rb', line 5

def name
  @name
end

#parentObject

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_firstObject



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

Returns:

  • (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

#lineageObject



62
63
64
# File 'lib/gemlist/spec_node.rb', line 62

def lineage
  [self, *parentage].reverse.reject(&:root?)
end

#parentageObject



71
72
73
74
75
# File 'lib/gemlist/spec_node.rb', line 71

def parentage
  return [] if root?

  [parent] + parent.parentage
end

#root?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/gemlist/spec_node.rb', line 77

def root?
  parent.nil?
end

#sizeObject



43
44
45
# File 'lib/gemlist/spec_node.rb', line 43

def size
  to_a.size
end

#total_dependency_countObject



39
40
41
# File 'lib/gemlist/spec_node.rb', line 39

def total_dependency_count
  size - 1
end