Module: StateMachineJob

Defined in:
lib/state_machine_job.rb,
lib/state_machine_job/macro.rb,
lib/state_machine_job/version.rb

Defined Under Namespace

Modules: Macro

Constant Summary collapse

VERSION =
'3.1.0'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.result_event_name(job, result) ⇒ Object


21
22
23
# File 'lib/state_machine_job.rb', line 21

def self.result_event_name(job, result)
  [job.name.underscore.split('/'), result].flatten.join('_').to_sym
end

.result_method_name(job, result) ⇒ Object


25
26
27
# File 'lib/state_machine_job.rb', line 25

def self.result_method_name(job, result)
  "#{result_event_name(job, result)}!"
end

Instance Method Details

#perform(record, payload = {}) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/state_machine_job.rb', line 5

def perform(record, payload = {})
  record_name = "#{record.class.name} #{record.id}"
  logger.info "perform for #{record_name}"

  begin
    result = perform_with_result(record, payload)
  rescue StandardError
    record.restore_attributes unless record.valid?
    result = :error
    raise
  ensure
    logger.info "result #{result} for #{record_name}"
    record.send(StateMachineJob.result_method_name(self.class, result))
  end
end