Class: ForemanMaintain::DependencyGraph

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/foreman_maintain/dependency_graph.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ DependencyGraph

Returns a new instance of DependencyGraph.



13
14
15
16
17
18
# File 'lib/foreman_maintain/dependency_graph.rb', line 13

def initialize(collection)
  @graph = Hash.new([])
  @collection = collection
  @steps_by_labels = @collection.group_by(&:label)
  generate_label_graph
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



7
8
9
# File 'lib/foreman_maintain/dependency_graph.rb', line 7

def collection
  @collection
end

#graphObject (readonly)

Returns the value of attribute graph.



7
8
9
# File 'lib/foreman_maintain/dependency_graph.rb', line 7

def graph
  @graph
end

#labelsObject (readonly)

Returns the value of attribute labels.



7
8
9
# File 'lib/foreman_maintain/dependency_graph.rb', line 7

def labels
  @labels
end

Class Method Details

.sort(collection) ⇒ Object



9
10
11
# File 'lib/foreman_maintain/dependency_graph.rb', line 9

def self.sort(collection)
  new(collection).tsort.map(&:ensure_instance)
end

Instance Method Details

#add_to_graph(key, dependencies = []) ⇒ Object



20
21
22
23
24
# File 'lib/foreman_maintain/dependency_graph.rb', line 20

def add_to_graph(key, dependencies = [])
  return unless key

  graph[key] = dependencies
end

#tsort_each_child(node, &block) ⇒ Object



30
31
32
# File 'lib/foreman_maintain/dependency_graph.rb', line 30

def tsort_each_child(node, &block)
  graph.fetch(node).each(&block)
end

#tsort_each_node(&block) ⇒ Object



26
27
28
# File 'lib/foreman_maintain/dependency_graph.rb', line 26

def tsort_each_node(&block)
  @collection.each(&block)
end