Class: Orchestra::RunList::Builder::Sorter

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/orchestra/run_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(steps_hash) ⇒ Sorter

Returns a new instance of Sorter.



127
128
129
# File 'lib/orchestra/run_list.rb', line 127

def initialize steps_hash
  @steps = steps_hash
end

Instance Method Details

#build_dependencies_for(step) ⇒ Object



146
147
148
149
150
151
# File 'lib/orchestra/run_list.rb', line 146

def build_dependencies_for step
  step.required_dependencies.each_with_object Set.new do |dep, set|
    provider = provider_for dep
    set << provider if provider
  end
end

#build_dependency_treeObject



140
141
142
143
144
# File 'lib/orchestra/run_list.rb', line 140

def build_dependency_tree
  @hsh = @steps.each_with_object Hash.new do |(name, step), hsh|
  hsh[name] = build_dependencies_for step
  end
end

#effective_provisions_for(step, dep) ⇒ Object



170
171
172
# File 'lib/orchestra/run_list.rb', line 170

def effective_provisions_for step, dep
  step.optional_dependencies | step.provisions
end

#provider_for(dep) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/orchestra/run_list.rb', line 162

def provider_for dep
  @steps.each do |name, step|
    provisions = effective_provisions_for step, dep
    return name if provisions.include? dep
  end
  nil
end

#sort!Object



131
132
133
134
135
136
137
138
# File 'lib/orchestra/run_list.rb', line 131

def sort!
  build_dependency_tree
  tsort.each do |name|
    @steps[name] = @steps.delete name
  end
rescue TSort::Cyclic
  raise CircularDependencyError.new
end

#tsort_each_child(name, &block) ⇒ Object



157
158
159
160
# File 'lib/orchestra/run_list.rb', line 157

def tsort_each_child(name, &block)
  deps = @hsh.fetch name
  deps.each &block
end

#tsort_each_node(&block) ⇒ Object



153
154
155
# File 'lib/orchestra/run_list.rb', line 153

def tsort_each_node(&block)
  @hsh.each_key &block
end