Class: Parcels::DependencyParcelList

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/parcels/dependency_parcel_list.rb

Instance Method Summary collapse

Constructor Details

#initializeDependencyParcelList

Returns a new instance of DependencyParcelList.



5
6
7
8
9
10
11
# File 'lib/parcels/dependency_parcel_list.rb', line 5

def initialize
  @tag_to_child_tag_map = { }
  @parcel_to_tag_map = { }
  @tag_to_parcel_map = { }

  @loose_parcels = [ ]
end

Instance Method Details

#add_parcels!(parcels) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/parcels/dependency_parcel_list.rb', line 25

def add_parcels!(parcels)
  parcels.each do |parcel|
    if parcel.tag
      parcel_to_tag_map[parcel] = parcel.tag
      tag_to_parcel_map[parcel.tag] = parcel

      parcel.tags_that_must_come_before.each do |tag_that_must_come_before|
        tag_to_child_tag_map[tag_that_must_come_before] ||= [ ]
        tag_to_child_tag_map[tag_that_must_come_before] << parcel.tag
      end
    else
      loose_parcels << parcel
    end
  end
end

#parcels_in_orderObject



41
42
43
# File 'lib/parcels/dependency_parcel_list.rb', line 41

def parcels_in_order
  tsort.reverse # tsort puts children before parents; we want the exact opposite
end

#tsort_each_child(parcel, &block) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/parcels/dependency_parcel_list.rb', line 17

def tsort_each_child(parcel, &block)
  tag = parcel_to_tag_map[parcel]
  child_tags = tag_to_child_tag_map[tag] || [ ]
  child_parcels = child_tags.map { |t| tag_to_parcel_map[t] }.compact

  child_parcels.each(&block)
end

#tsort_each_node(&block) ⇒ Object



13
14
15
# File 'lib/parcels/dependency_parcel_list.rb', line 13

def tsort_each_node(&block)
  parcel_to_tag_map.keys.each(&block)
end