Class: ActiveModel::Jobs::Performer
- Inherits:
-
Object
- Object
- ActiveModel::Jobs::Performer
- Defined in:
- lib/active_model/jobs/performer.rb
Overview
A support class for finding the ActiveJob::Base that corresponds to a given action method on a given model. When the job class is found, the action method fires off a new instance of the job.
Instance Attribute Summary collapse
-
#method_name ⇒ Object
readonly
The method name given to the class as a String.
-
#model_name ⇒ Object
readonly
The model name given to the class by
ActiveModel::Naming.
Instance Method Summary collapse
-
#action_name ⇒ String
Strip the ‘!’ off of the end of the method.
-
#call(model) ⇒ TrueClass, FalseClass
Perform this action on the given model.
-
#initialize(method_name, model_name) ⇒ Performer
constructor
A new instance of Performer.
-
#job? ⇒ Boolean
Tests whether this method name corresponds to a job class in the application.
-
#job_class ⇒ ActiveJob::Base
Attempts to find the job class for this method and return it, otherwise it returns
nilwhen encountering aNameError. -
#job_name ⇒ String
Build the conventional job name from the given method and model.
Constructor Details
#initialize(method_name, model_name) ⇒ Performer
Returns a new instance of Performer.
22 23 24 25 |
# File 'lib/active_model/jobs/performer.rb', line 22 def initialize(method_name, model_name) @method_name = method_name.to_s @model_name = model_name.to_s end |
Instance Attribute Details
#method_name ⇒ Object (readonly)
The method name given to the class as a String.
12 13 14 |
# File 'lib/active_model/jobs/performer.rb', line 12 def method_name @method_name end |
#model_name ⇒ Object (readonly)
The model name given to the class by ActiveModel::Naming.
18 19 20 |
# File 'lib/active_model/jobs/performer.rb', line 18 def model_name @model_name end |
Instance Method Details
#action_name ⇒ String
Strip the ‘!’ off of the end of the method.
56 57 58 |
# File 'lib/active_model/jobs/performer.rb', line 56 def action_name method_name.gsub ACTION_SUFFIX, '' end |
#call(model) ⇒ TrueClass, FalseClass
Perform this action on the given model.
performing the job on enqueue.
66 67 68 |
# File 'lib/active_model/jobs/performer.rb', line 66 def call(model) job_class.perform_later model end |
#job? ⇒ Boolean
Tests whether this method name corresponds to a job class in the application.
31 32 33 |
# File 'lib/active_model/jobs/performer.rb', line 31 def job? job_class.present? end |
#job_class ⇒ ActiveJob::Base
Attempts to find the job class for this method and return it, otherwise it returns nil when encountering a NameError.
39 40 41 42 43 |
# File 'lib/active_model/jobs/performer.rb', line 39 def job_class job_name.classify.constantize rescue NameError nil end |
#job_name ⇒ String
Build the conventional job name from the given method and model. Suffix with job and separate with underscores.
49 50 51 |
# File 'lib/active_model/jobs/performer.rb', line 49 def job_name "#{action_name}_#{model_name}_job" end |