Class: Handcuffs::Phases
- Inherits:
-
Object
- Object
- Handcuffs::Phases
- Defined in:
- lib/handcuffs/phases.rb
Overview
Phases encapsulates the list of phases and any interdependencies
Instance Method Summary collapse
- #in_order ⇒ Object
- #include?(phase) ⇒ Boolean
-
#initialize(phases) ⇒ Phases
constructor
A new instance of Phases.
- #prereqs(attempted_phase) ⇒ Object
- #to_sentence ⇒ Object
Constructor Details
#initialize(phases) ⇒ Phases
Returns a new instance of Phases.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/handcuffs/phases.rb', line 8 def initialize(phases) @phases = case phases when Hash phases.each_with_object({}) do |phase, acc| acc[phase[0].to_sym] = Array(phase[1]).map(&:to_sym) end else # Assume each entry depends on all entries before it phases.map(&:to_sym).each_with_object({}) do |phase, acc| acc[phase] = phases.take_while { |defined_phase| defined_phase != phase } end end end |
Instance Method Details
#in_order ⇒ Object
30 31 32 33 34 35 |
# File 'lib/handcuffs/phases.rb', line 30 def in_order TSort.tsort( @phases.method(:each_key), ->(phase, &block) { @phases.fetch(phase).each(&block) } ) end |
#include?(phase) ⇒ Boolean
26 27 28 |
# File 'lib/handcuffs/phases.rb', line 26 def include?(phase) @phases.include?(phase) end |
#prereqs(attempted_phase) ⇒ Object
37 38 39 |
# File 'lib/handcuffs/phases.rb', line 37 def prereqs(attempted_phase) @phases.fetch(attempted_phase, []) end |
#to_sentence ⇒ Object
22 23 24 |
# File 'lib/handcuffs/phases.rb', line 22 def to_sentence @phases.keys.to_sentence end |