class Interaktor::Context
The object for tracking state of an Interaktor's invocation. The context is used to initialize the interaktor with the information required for invocation. The interaktor manipulates the context to produce the result of invocation. The context is the mechanism by which success and failure are determined and the context is responsible for tracking individual interaktor invocations for the purpose of rollback. It may be manipulated using arbitrary getter and setter methods.
Public Class Methods
Initialize an Interaktor::Context
or preserve an existing one. If the argument given is an Interaktor::Context
, the argument is returned. Otherwise, a new Interaktor::Context
is initialized from the provided hash. Used during interaktor initialization.
@param context [Hash, Interaktor::Context] the context object as a hash with attributes or an already-built context
@return [Interaktor::Context]
# File lib/interaktor/context.rb, line 20 def self.build(context = {}) context.is_a?(Interaktor::Context) ? context : new(context) end
Public Instance Methods
An array of successfully called Interaktor
instances invoked against this Interaktor::Context
instance.
@return [Array<Interaktor>]
# File lib/interaktor/context.rb, line 98 def _called @called ||= [] end
Track that an Interaktor
has been called. The `#called!` method is used by the interaktor being invoked with this context. After an interaktor is successfully called, the interaktor instance is tracked in the context for the purpose of potential future rollback.
@param interaktor [Interaktor] an interaktor that has been successfully
called
@return [void]
# File lib/interaktor/context.rb, line 90 def called!(interaktor) _called << interaktor end
Trigger an early return throw.
@return [void]
# File lib/interaktor/context.rb, line 105 def early_return! @early_return = true throw :early_return, self end
Whether or not the context has been returned from early.
@return [Boolean]
# File lib/interaktor/context.rb, line 113 def early_return? (@early_return == true) || false end
Fail the Interaktor::Context
. Failing a context raises an error that may be rescued by the calling interaktor. The context is also flagged as having failed. Optionally the caller may provide a hash of key/value pairs to be merged into the context before failure.
@param context [Hash] data to be merged into the existing context
@raises [Interaktor::Failure]
@return [void]
# File lib/interaktor/context.rb, line 52 def fail!(context = {}) context.each { |key, value| self[key.to_sym] = value } @failure = true raise Interaktor::Failure, self end
Whether the Interaktor::Context
has failed. By default, a new context is successful and only changes when explicitly failed. This method is the inverse of the `#success?` method.
@return [Boolean] false by default, or true if failed
# File lib/interaktor/context.rb, line 38 def failure? @failure || false end
Roll back the Interaktor::Context
. Any interaktors to which this context has been passed and which have been successfully called are asked to roll themselves back by invoking their `#rollback` methods.
@return [Boolean] true if rolled back successfully, false if already
rolled back
# File lib/interaktor/context.rb, line 74 def rollback! return false if @rolled_back _called.reverse_each(&:rollback) @rolled_back = true end
@param context [Hash] data to be merged into the existing context
@raises [Interaktor::Failure]
@return [void]
# File lib/interaktor/context.rb, line 63 def success!(context = {}) context.each { |key, value| self[key.to_sym] = value } early_return! end
Whether the Interaktor::Context
is successful. By default, a new context is successful and only changes when explicitly failed. This method is the inverse of the `#failure?` method.
@return [Boolean] true by default, or false if failed
# File lib/interaktor/context.rb, line 29 def success? !failure? end