Module: Arq::Action::InstanceMethods

Defined in:
lib/arq/action.rb

Overview

Methods and fields exposed to the run block.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Used to easily call other actions via snake-cased paths with dot accessors. IE ‘Foo::Bar::Action` can be called via `foo.bar.action`



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/arq/action.rb', line 122

def method_missing(method, *args, &block)
  # Format method as module path.
  formatted = method.to_s.camelize

  # Attempt to find object.
  obj = if Object.const_defined?(formatted)
          Object.const_get(formatted)
        else
          return super
        end

  # Defer handling to ActionModuleHash
  Arq::ActionModuleHash.from(obj, self)
end

Instance Method Details

#call_other(action) ⇒ Object

Used to call another action using the current action’s context. Because of this, the context is exported and then imported again.



104
105
106
107
108
# File 'lib/arq/action.rb', line 104

def call_other(action)
  _export_variables
  action.call(@_ctx)
  _import_context
end

#fail!(message = nil) ⇒ Object

Fails the context without exiting the current action.



111
112
113
# File 'lib/arq/action.rb', line 111

def fail!(message = nil)
  @_ctx.fail!(message)
end

#fail_now!(message = nil) ⇒ Object

Fails the context and exits the current action.



116
117
118
# File 'lib/arq/action.rb', line 116

def fail_now!(message = nil)
  @_ctx.fail_now!(message)
end

#initialize(ctx) ⇒ Object



73
74
75
# File 'lib/arq/action.rb', line 73

def initialize(ctx)
  @_ctx = ctx
end

#respond_to_missing?(method, include_private: false) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/arq/action.rb', line 137

def respond_to_missing?(method, include_private: false)
  Object.const_defined?(method.to_s.camelize) || super
end

#runObject

This is the entry-point for the action’s execution. rubocop:disable Metrics/MethodLength



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/arq/action.rb', line 79

def run
  return if @_ctx.failure?

  _validate_required_params
  _import_context

  # Suppress `FailureError`s since they're used to just exit the action.
  begin
    # Instance eval to expose instance variables.
    instance_eval(&self.class.run_block)
  rescue Arq::FailureError
    nil
  end

  _export_variables

  # Only validate returns if context is successful
  _validate_required_returns if @_ctx.success?

  nil
end