Module: ActiveJob::Attributes
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
::ActiveSupport::Concern
|
|
|
Instance Chain:
|
|
| Defined in: | activejob/lib/active_job/attributes.rb |
Overview
The Attributes module provides typed attributes for jobs using the Active Model Attributes API. Declared attributes are automatically serialized with the job data and restored when the job is deserialized.
This is especially useful with Continuable, where a job may be
interrupted and resumed multiple times and you need to persist attributes
across steps until the job finishes.
class SubmitEnrollmentJob < ApplicationJob
include ActiveJob::Continuable
attribute :payment_token, :string
attribute :billing_profile_id, :integer
def perform(enrollment)
step(:tokenize_payment_instrument) do
self.payment_token = PaymentGateway.tokenize(enrollment.user.payment_instrument)
end
step(:create_billing_profile) do
self.billing_profile_id = BillingProfileApi.create(customer_id: enrollment.user_id)
end
step(:submit_enrollment) do
submission_id = EnrollmentApi.submit(enrollment, billing_profile_id)
enrollment.update!(status: 'processing', submission_id: submission_id)
end
end
end
Attributes also work without Continuable, persisting across retries.
Attributes support all built-in Active Model types, see ::ActiveModel::Attribute
for details. For custom types attribute values must be serializable
as Active Job arguments. See Arguments for the full list of
supported types.
Constant Summary
Class Method Summary
::ActiveSupport::Concern - Extended
| class_methods | Define class methods from given block. |
| included | Evaluate given block in context of base class, so that you can write class macros here. |
| prepended | Evaluate given block in context of base class, so that you can write class macros here. |
| append_features, prepend_features | |
Instance Attribute Summary
::ActiveModel::Attributes - Included
Instance Method Summary
- #deserialize_attribute_values(serialized) private
- #serialize_attribute_values private
- #deserialize(job_data) Internal use only
- #serialize Internal use only
::ActiveModel::Attributes - Included
| #attribute_names | Returns an array of attribute names as strings. |
| #attributes | Returns a hash of all the attributes with their names as keys and the values of the attributes as values. |
| #_write_attribute, #attribute, | |
| #attribute= | Alias for ActiveModel::Attributes#_write_attribute. |
| #freeze, #initialize, #initialize_dup | |
::ActiveModel::AttributeMethods - Included
| #attribute_missing |
|
| #method_missing | Allows access to the object attributes, which are held in the hash returned by |
| #respond_to?, | |
| #respond_to_without_attributes? | A |
| #_read_attribute, #attribute_method?, | |
| #matched_attribute_method | Returns a struct representing the matching attribute method. |
| #missing_attribute | |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActiveModel::AttributeMethods
Instance Method Details
#deserialize(job_data)
# File 'activejob/lib/active_job/attributes.rb', line 52
def deserialize(job_data) # :nodoc: super deserialize_attribute_values(job_data["attributes"]) if job_data["attributes"] end
#deserialize_attribute_values(serialized) (private)
[ GitHub ]# File 'activejob/lib/active_job/attributes.rb', line 66
def deserialize_attribute_values(serialized) values = Arguments.deserialize([serialized]).first values.each do |name, value| name = name.to_s @attributes.write_cast_value(name, value) if self.class.attribute_types.key?(name) end end
#serialize
# File 'activejob/lib/active_job/attributes.rb', line 48
def serialize # :nodoc: super.merge("attributes" => serialize_attribute_values) end