Module: Interaktor

Defined in:
lib/interaktor.rb

Defined Under Namespace

Modules: ClassMethods, Hooks, Organizer Classes: Context, Failure

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

When the Interaktor module is included in a class, add the relevant class methods and hooks to that class.

Parameters:

  • base (Class)

    the class which is including the Interaktor module



12
13
14
15
16
17
# File 'lib/interaktor.rb', line 12

def self.included(base)
  base.class_eval do
    extend ClassMethods
    include Hooks
  end
end

Instance Method Details

#callvoid

This method returns an undefined value.

Invoke an Interaktor instance without any hooks, tracking, or rollback. It is expected that the ‘#call` instance method is overwritten for each interaktor class.



208
# File 'lib/interaktor.rb', line 208

def call; end

#fail!(failure_attributes = {}) ⇒ void

This method returns an undefined value.

Fail the current interaktor.

Parameters:

  • failure_attributes (Hash{Symbol=>Object}) (defaults to: {})

    the context attributes



174
175
176
177
178
179
180
181
# File 'lib/interaktor.rb', line 174

def fail!(failure_attributes = {})
  # Make sure we have all required attributes
  missing_attrs = self.class.failure_attributes
                      .reject { |failure_attr| failure_attributes.key?(failure_attr) }
  raise "Missing failure attrs: #{missing_attrs.join(", ")}" if missing_attrs.any?

  @context.fail!(failure_attributes)
end

#initialize(context = {}) ⇒ Object

Parameters:

  • context (Hash, Interaktor::Context) (defaults to: {})

    the context object as a hash with attributes or an already-built context



165
166
167
# File 'lib/interaktor.rb', line 165

def initialize(context = {})
  @context = Interaktor::Context.build(context)
end

#rollbackvoid

This method returns an undefined value.

Reverse prior invocation of an Interaktor instance. Any interaktor class that requires undoing upon downstream failure is expected to overwrite the ‘#rollback` instance method.



215
# File 'lib/interaktor.rb', line 215

def rollback; end

#runvoid

This method returns an undefined value.

Invoke an interaktor instance along with all defined hooks. The ‘run` method is used internally by the `call` class method. After successful invocation of the interaktor, the instance is tracked within the context. If the context is failed or any error is raised, the context is rolled back.



224
225
226
227
# File 'lib/interaktor.rb', line 224

def run
  run!
rescue Interaktor::Failure # rubocop:disable Lint/SuppressedException
end

#run!void

This method returns an undefined value.

Invoke an Interaktor instance along with all defined hooks, typically used internally by ‘.call!`. After successful invocation of the interaktor, the instance is tracked within the context. If the context is failed or any error is raised, the context is rolled back. This method behaves identically to `#run` with one notable exception - if the context is failed during the invocation of the interaktor, `Interaktor::Failure` is raised.



239
240
241
242
243
244
245
246
247
# File 'lib/interaktor.rb', line 239

def run!
  with_hooks do
    call
    @context.called!(self)
  end
rescue StandardError
  @context.rollback!
  raise
end

#success!(success_attributes = {}) ⇒ void

This method returns an undefined value.

Terminate execution of the current interaktor and copy the success attributes into the context.

Parameters:

  • success_attributes (Hash{Symbol=>Object}) (defaults to: {})

    the context attributes



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/interaktor.rb', line 189

def success!(success_attributes = {})
  # Make sure we have all required attributes
  missing_attrs = self.class.success_attributes
                      .reject { |success_attr| success_attributes.key?(success_attr) }
  raise "Missing success attrs: #{missing_attrs.join(", ")}" if missing_attrs.any?

  # Make sure we haven't provided any unknown attributes
  unknown_attrs = success_attributes.keys
                                    .reject { |success_attr| self.class.success_attributes.include?(success_attr) }
  raise "Unknown success attrs: #{unknown_attrs.join(", ")}" if unknown_attrs.any?

  @context.success!(success_attributes)
end