class Serially::TaskManager
Attributes
klass[RW]
options[RW]
queue[RW]
tasks[RW]
Public Class Methods
[](klass)
click to toggle source
the following two methods provide the storage of all task managers
# File lib/serially/task_manager.rb, line 5 def self.[](klass) (@task_managers ||= {})[klass.to_s] end
[]=(klass, task_manager)
click to toggle source
# File lib/serially/task_manager.rb, line 9 def self.[]=(klass, task_manager) (@task_managers ||= {})[klass.to_s] = task_manager end
new(klass, options = {})
click to toggle source
# 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
Public Instance Methods
add_task(task_name, task_options, &block)
click to toggle source
# 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)
click to toggle source
# 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
each() { |task| ... }
click to toggle source
Allow iterating over tasks
# 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
Private Instance Methods
next_task_order!()
click to toggle source
returns next task order, and advances the counter
# File lib/serially/task_manager.rb, line 56 def next_task_order! current_order = @last_task_order @last_task_order += 1 current_order end