class ForemanMaintain::DependencyGraph

Attributes

collection[R]
graph[R]
labels[R]

Public Class Methods

new(collection) click to toggle source
# 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
sort(collection) click to toggle source
# File lib/foreman_maintain/dependency_graph.rb, line 9
def self.sort(collection)
  new(collection).tsort.map(&:ensure_instance)
end

Public Instance Methods

add_to_graph(key, dependencies = []) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/foreman_maintain/dependency_graph.rb, line 26
def tsort_each_node(&block)
  @collection.each(&block)
end

Private Instance Methods

generate_label_graph() click to toggle source
# File lib/foreman_maintain/dependency_graph.rb, line 36
def generate_label_graph
  collection.each do |object|
    klass = object.is_a?(Class) ? object : object.class
    klass.before.each do |label|
      add_to_graph(labels_to_objects(label).first, [object])
    end
    add_to_graph(object, labels_to_objects(klass.after))
  end
end
labels_to_objects(labels) click to toggle source
# File lib/foreman_maintain/dependency_graph.rb, line 46
def labels_to_objects(labels)
  labels = Array(labels)
  labels.map { |label| @steps_by_labels[label] }.compact.flatten
end