Class: Qmin::Resque::BaseJob
- Inherits:
-
Object
- Object
- Qmin::Resque::BaseJob
- Defined in:
- lib/qmin/resque/base_job.rb
Overview
Defines a handy base class for Resque workers for the following pattern:
-
Find an instance of a model by id
-
call a method on the instance
Constant Summary collapse
- MustRespondToFindMethodError =
Class.new(StandardError) do @message = 'Model must respond to #find method!' end
- ImplementationMissing =
Class.new(StandardError)
Class Method Summary collapse
- .model(model) ⇒ Object
-
.perform(*args) ⇒ Object
delegate perform to job instance.
- .queue ⇒ Object
Instance Method Summary collapse
- #find_instance(id) ⇒ Object
-
#initialize(id, *args) ⇒ BaseJob
constructor
A new instance of BaseJob.
- #instance ⇒ Object
- #model ⇒ Object
- #perform ⇒ Object
Constructor Details
#initialize(id, *args) ⇒ BaseJob
Returns a new instance of BaseJob.
27 28 29 |
# File 'lib/qmin/resque/base_job.rb', line 27 def initialize(id, *args) @id = id end |
Class Method Details
.model(model) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/qmin/resque/base_job.rb', line 19 def self.model(model) if model.respond_to? :find @@model = model else raise MustRespondToFindMethodError end end |
.perform(*args) ⇒ Object
delegate perform to job instance
15 16 17 |
# File 'lib/qmin/resque/base_job.rb', line 15 def self.perform(*args) new(*args).perform end |
.queue ⇒ Object
47 48 49 50 |
# File 'lib/qmin/resque/base_job.rb', line 47 def self.queue # automatic queue naming @queue ||= self.name.to_queue_name end |
Instance Method Details
#find_instance(id) ⇒ Object
39 40 41 |
# File 'lib/qmin/resque/base_job.rb', line 39 def find_instance(id) model.find(id) end |
#instance ⇒ Object
35 36 37 |
# File 'lib/qmin/resque/base_job.rb', line 35 def instance @instance ||= find_instance(@id) end |
#model ⇒ Object
43 44 45 |
# File 'lib/qmin/resque/base_job.rb', line 43 def model @@model end |
#perform ⇒ Object
31 32 33 |
# File 'lib/qmin/resque/base_job.rb', line 31 def perform raise ImplementationMissing.new("Please implement #perform instance method for job: #{self.class.name}") end |