Class: Hyrb::Pipeline

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/hyrb/pipeline.rb

Direct Known Subclasses

Commands::Pipeline

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_class) ⇒ Pipeline

Returns a new instance of Pipeline.



13
14
15
16
17
# File 'lib/hyrb/pipeline.rb', line 13

def initialize(task_class)
  @task_class = task_class
  @prev_tasks = []
  @next_tasks = build_task_list
end

Instance Attribute Details

#current_taskObject

Returns the value of attribute current_task.



7
8
9
# File 'lib/hyrb/pipeline.rb', line 7

def current_task
  @current_task
end

#envObject

Returns the value of attribute env.



7
8
9
# File 'lib/hyrb/pipeline.rb', line 7

def env
  @env
end

#next_tasksObject

Returns the value of attribute next_tasks.



7
8
9
# File 'lib/hyrb/pipeline.rb', line 7

def next_tasks
  @next_tasks
end

#prev_tasksObject

Returns the value of attribute prev_tasks.



7
8
9
# File 'lib/hyrb/pipeline.rb', line 7

def prev_tasks
  @prev_tasks
end

Class Method Details

.rulesObject



9
10
11
# File 'lib/hyrb/pipeline.rb', line 9

def self.rules
  @@rules ||= {}
end

Instance Method Details

#build_task_list(id_map = {}, stack = []) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hyrb/pipeline.rb', line 51

def build_task_list(id_map={}, stack=[])
  arr = []
  each_strongly_connected_component_from(@task_class, id_map, stack) do |t|
    if t.length == 1
      arr << t.first
    else
      raise TSort::Cyclic.new("cyclic dependencies: #{t.inspect}")
    end
  end
  arr
end

#info(message) ⇒ Object



39
40
41
# File 'lib/hyrb/pipeline.rb', line 39

def info(message)
  puts message
end

#invoke(task_class) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/hyrb/pipeline.rb', line 31

def invoke(task_class)
  pipeline = self.class.new(task_class)
  pipeline.prev_tasks += self.prev_tasks
  pipeline.next_tasks -= self.prev_tasks
  pipeline.run(@env)
  self.next_tasks -= pipeline.prev_tasks
end

#run(env = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hyrb/pipeline.rb', line 19

def run(env = {})
  @env = env
  while @current_task = @next_tasks.shift
    task = @current_task.new(self)
    info "--> Running #{@current_task}" if @env[:verbose]
    task.run_before(@env)
    task.run(@env)
    @prev_tasks << @current_task
  end
  @env
end

#tsort_each_child(node, &block) ⇒ Object



43
44
45
# File 'lib/hyrb/pipeline.rb', line 43

def tsort_each_child(node, &block)
  (self.class.rules[node] || []).each(&block)
end

#tsort_each_node(&block) ⇒ Object



47
48
49
# File 'lib/hyrb/pipeline.rb', line 47

def tsort_each_node(&block)
  (self.class.rules || {}).each_key(&block)
end