Class: AASM::Core::Invoker

Inherits:
Object
  • Object
show all
Defined in:
lib/aasm/core/invoker.rb

Overview

main invoker class which encapsulates the logic for invoking literal-based, proc-based, class-based and array-based callbacks for different entities.

Constant Summary collapse

DEFAULT_RETURN_VALUE =
true

Instance Method Summary collapse

Constructor Details

#initialize(subject, record, args) ⇒ Invoker

Initialize a new invoker instance. NOTE that invoker must be used per-subject/record

(one instance per subject/record)

Options:

subject - invoking subject, may be Proc,

Class, String, Symbol or Array

record - invoking record args - arguments which will be passed to the callback



24
25
26
27
28
29
30
31
# File 'lib/aasm/core/invoker.rb', line 24

def initialize(subject, record, args)
  @subject = subject
  @record = record
  @args = args
  @options = {}
  @failures = []
  @default_return_value = DEFAULT_RETURN_VALUE
end

Instance Method Details

#invokeObject

rubocop:disable Metrics/AbcSize



83
84
85
86
87
88
89
# File 'lib/aasm/core/invoker.rb', line 83

def invoke
  return invoke_array if subject.is_a?(Array)
  return literal_invoker.invoke if literal_invoker.may_invoke?
  return proc_invoker.invoke if proc_invoker.may_invoke?
  return class_invoker.invoke if class_invoker.may_invoke?
  default_return_value
end

#with_default_return_value(value) ⇒ Object

Change default return value of #invoke method if none of invokers processed the request.

The default return value is #DEFAULT_RETURN_VALUE

Options:

value - default return value for #invoke method



72
73
74
75
# File 'lib/aasm/core/invoker.rb', line 72

def with_default_return_value(value)
  @default_return_value = value
  self
end

#with_failures(failures) ⇒ Object

Collect failures to a specified buffer

Options:

failures - failures buffer to collect failures



57
58
59
60
# File 'lib/aasm/core/invoker.rb', line 57

def with_failures(failures)
  @failures = failures
  self
end

#with_options(options) ⇒ Object

Pass additional options to concrete invoker

Options:

options - hash of options which will be passed to

concrete invokers

Example:

with_options(guard: proc …)



45
46
47
48
# File 'lib/aasm/core/invoker.rb', line 45

def with_options(options)
  @options = options
  self
end