Class: Kaboose::Processor

Public Class Methods


processes (name)

Takes a class name and returns and creates an accessor method and instance variable for it. If the class responds to find (i.e. ActiveRecord objects), then it tries to call it with the value of name_id extracted from the Task. If not, it creates a new instaces of the class, sets an instance variable with that name and returns it.

    # File lib/kaboose/processor.rb, line 22
22:     def self.processes(name)
23:       name = name.to_s
24:       define_method(name) do
25:         ivar = instance_variable_get("@#{name}")
26:         return ivar unless ivar.blank?
27:         
28:         object = Object.const_get(name.camelize)
29:         
30:         value = if object.respond_to?(:find)
31:           begin
32:             object.find(@task.options["#{name}_id".intern]) 
33:           # We provide automatic ActiveRecord::RecordNotFound protection because this method
34:           # useless without a correct instance of the model.
35:           rescue ActiveRecord::RecordNotFound => rnf
36:             raise Kaboose::NotFound, rnf.message
37:           end
38:         else
39:           Object.const_get(name.camelize).send('new')
40:         end
41:         instance_variable_set("@#{name}", value)
42:         
43:         value
44:       end      
45:     end

Public Instance Methods


options ()

    # File lib/kaboose/processor.rb, line 13
13:     def options
14:       @task.options
15:     end

process! (task)

Runs the process method defined in the Processor subclass (in app/processors). Any error that is fatal should be rescued to avoid re-entry in to the queue. As a default, wee rescue Errno::ENOENT (File not found), and ActiveRecord::RecordNotFound (record not found). Subclasses of Processor should rescue any fatal errors specific to them in their own process method.

    # File lib/kaboose/processor.rb, line 51
51:     def process!(task)
52:       @task = task
53:       begin
54:         process
55:       rescue Kaboose::NotFound => notfound
56:       rescue Exception => e
57:         task.enqueue_with_error(e)
58:       end
59:     end