Class: ActiveModel::Jobs::Performer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/jobs/performer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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.

Constant Summary collapse

BANG =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/!\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, model_name) ⇒ Performer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Performer.

Parameters:

  • method_name (String)
    • A method corresponding to a job.

  • model_name (String)
    • The model we are calling this from.



24
25
26
27
# File 'lib/active_model/jobs/performer.rb', line 24

def initialize(method_name, model_name)
  @method_name = method_name.to_s
  @model_name = model_name.to_s
end

Instance Attribute Details

#method_nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/active_model/jobs/performer.rb', line 16

def method_name
  @method_name
end

#model_nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
# File 'lib/active_model/jobs/performer.rb', line 20

def model_name
  @model_name
end

Instance Method Details

#action_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Strip the ‘!’ off of the end of the method.



60
61
62
# File 'lib/active_model/jobs/performer.rb', line 60

def action_name
  method_name.gsub BANG, ''
end

#call(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Perform this action on the given model.

Parameters:

  • (ActiveModel::Model)


68
69
70
71
# File 'lib/active_model/jobs/performer.rb', line 68

def call(model)
  return false unless job?
  job_class.perform_later model
end

#job?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Tests whether this method name corresponds to a job class in the application.

Returns:

  • (Boolean)


33
34
35
# File 'lib/active_model/jobs/performer.rb', line 33

def job?
  method_name =~ BANG && job_class.present?
end

#job_classObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Attempts to find the job class for this method and return it, otherwise it returns nil when encountering a NameError.



41
42
43
44
45
# File 'lib/active_model/jobs/performer.rb', line 41

def job_class
  job_name.classify.constantize
rescue NameError
  nil
end

#job_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build the conventional job name from the given method and model. Suffix with job and separate with underscores.



51
52
53
54
55
# File 'lib/active_model/jobs/performer.rb', line 51

def job_name
  [
    action_name, model_name, 'job'
  ].join '_'
end