Class: Chingu::Async::TaskBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/chingu/async/task_builder.rb

Overview

Implements a DSL for appending new tasks to an task queue.

Instance Method Summary collapse

Constructor Details

#initialize(tasks) ⇒ TaskBuilder

Returns a new instance of TaskBuilder



29
30
31
# File 'lib/chingu/async/task_builder.rb', line 29

def initialize(tasks)
  @tasks = tasks
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject

Add a new task to the queue. The first argument is a Symbol or String naming the type of task; remaining arguments are passed on to the task's constructor.

If a block is supplied, it is scheduled to be executed as soon as the task is finished.

Attempting to invoke a nonexistant method automatically calls task with the method name as the task type.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chingu/async/task_builder.rb', line 66

def task(task, *args, &block)
  case task
  when Symbol, String
    klass_name = Chingu::Inflector.camelize(task)
    klass = Chingu::AsyncTasks.const_get(klass_name)
    
    task = klass.new(*args, &block)
    
  when Chingu::Async::BasicTask
    # pass
    
  when Class
    task = task.new(*args, &block)
    
  else raise TypeError, "task must be a Task object or task name"
  end
  
  @tasks.enq(task)
  task
end

Instance Method Details

#task(task, *args, &block) ⇒ Object Also known as: method_missing

Add a new task to the queue. The first argument is a Symbol or String naming the type of task; remaining arguments are passed on to the task's constructor.

If a block is supplied, it is scheduled to be executed as soon as the task is finished.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chingu/async/task_builder.rb', line 41

def task(task, *args, &block)
  case task
  when Symbol, String
    klass_name = Chingu::Inflector.camelize(task)
    klass = Chingu::AsyncTasks.const_get(klass_name)
    
    task = klass.new(*args, &block)
    
  when Chingu::Async::BasicTask
    # pass
    
  when Class
    task = task.new(*args, &block)
    
  else raise TypeError, "task must be a Task object or task name"
  end
  
  @tasks.enq(task)
  task
end