Class: Serially::TaskManager

Inherits:
Object
  • Object
show all
Defined in:
lib/serially/task_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ TaskManager

Returns a new instance of TaskManager.



15
16
17
18
19
20
21
# File 'lib/serially/task_manager.rb', line 15

def initialize(klass, options = {})
  @klass = klass
  @options = options
  # Hash is ordered since Ruby 1.9
  @tasks = {}
  @last_task_order = 0
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



13
14
15
# File 'lib/serially/task_manager.rb', line 13

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/serially/task_manager.rb', line 13

def options
  @options
end

#queueObject

Returns the value of attribute queue.



13
14
15
# File 'lib/serially/task_manager.rb', line 13

def queue
  @queue
end

#tasksObject

Returns the value of attribute tasks.



13
14
15
# File 'lib/serially/task_manager.rb', line 13

def tasks
  @tasks
end

Class Method Details

.[](klass) ⇒ Object

the following two methods provide the storage of all task managers



5
6
7
# File 'lib/serially/task_manager.rb', line 5

def self.[](klass)
  (@task_managers ||= {})[klass.to_s]
end

.[]=(klass, task_manager) ⇒ Object



9
10
11
# File 'lib/serially/task_manager.rb', line 9

def self.[]=(klass, task_manager)
  (@task_managers ||= {})[klass.to_s] = task_manager
end

Instance Method Details

#add_task(task_name, task_options, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/serially/task_manager.rb', line 34

def add_task(task_name, task_options, &block)
  raise Serially::ConfigurationError.new("Task #{task_name} is already defined in class #{@klass}") if @tasks.include?(task_name)
  raise Serially::ConfigurationError.new("Task name #{task_name} defined in class #{@klass} is not a symbol") if !task_name.is_a?(Symbol)

  invalid_options = Serially::TaskOptions.validate(task_options)
  raise Serially::ConfigurationError.new("Task #{task_name} received the following invalid options: #{invalid_options}") if invalid_options.present?

  @tasks[task_name] = Serially::Task.new(task_name, next_task_order!, task_options, self, &block)
end

#clone_for(new_klass) ⇒ Object



27
28
29
30
31
# File 'lib/serially/task_manager.rb', line 27

def clone_for(new_klass)
  new_mgr = TaskManager.new(new_klass, self.options)
  self.each { |task| new_mgr.add_task(task.name, task.options, &task.run_block) }
  new_mgr
end

#eachObject

Allow iterating over tasks



45
46
47
48
49
50
51
# File 'lib/serially/task_manager.rb', line 45

def each
  return enum_for(:each) unless block_given?  # return Enumerator

  @tasks.values.each do |task|
    yield task
  end
end