Class: Gemlist::SpecTree

Inherits:
SpecNode show all
Defined in:
lib/gemlist/spec_tree.rb

Instance Attribute Summary

Attributes inherited from SpecNode

#children, #content, #name, #parent

Instance Method Summary collapse

Methods inherited from SpecNode

#<<, #depth_first_children_first, #descends_from?, #each, #lineage, #parentage, #root?, #size, #total_dependency_count

Constructor Details

#initialize(lockfile_parser) ⇒ SpecTree

Returns a new instance of SpecTree.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gemlist/spec_tree.rb', line 6

def initialize(lockfile_parser)
  super("root", lockfile_parser)

  specs_to_place = lockfile_parser.specs.to_set

  # start with the top-level declared dependencies
  lockfile_parser.dependencies.each do |dependency|
    spec_for_dependency = specs_to_place.find { |spec| spec.name == dependency.name }

    self << SpecNode.new(spec_for_dependency.name, spec_for_dependency)

    specs_to_place.delete(spec_for_dependency)
  end

  until specs_to_place.empty?
    specs_to_place.each do |spec|
      # we find all the specs that are parent to this one, so that we can
      # just remove entire top-level branches when it comes time to exclude
      # groups
      parent_nodes = select do |node|
        node.content.dependencies.any? do |dependency|
          dependency.name == spec.name
        end
      end

      parent_nodes.each do |node|
        node << SpecNode.new(spec.name, spec)
      end

      specs_to_place.delete(spec) if parent_nodes.any?
    end
  end
end