Class: Interaktor::Context

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/interaktor/context.rb

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(context = {}) ⇒ Interaktor::Context

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.

with attributes or an already-built context

Parameters:

Returns:



20
21
22
# File 'lib/interaktor/context.rb', line 20

def self.build(context = {})
  context.is_a?(Interaktor::Context) ? context : new(context)
end

Instance Method Details

#_calledArray<Interaktor>

An array of successfully called Interaktor instances invoked against this Interaktor::Context instance.

Returns:



98
99
100
# File 'lib/interaktor/context.rb', line 98

def _called
  @called ||= []
end

#called!(interaktor) ⇒ void

This method returns an undefined value.

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.

Parameters:

  • interaktor (Interaktor)

    an interaktor that has been successfully called



90
91
92
# File 'lib/interaktor/context.rb', line 90

def called!(interaktor)
  _called << interaktor
end

#early_return!void

This method returns an undefined value.

Trigger an early return throw.



105
106
107
108
# File 'lib/interaktor/context.rb', line 105

def early_return!
  @early_return = true
  throw :early_return, self
end

#early_return?Boolean

Whether or not the context has been returned from early.

Returns:

  • (Boolean)


113
114
115
# File 'lib/interaktor/context.rb', line 113

def early_return?
  (@early_return == true) || false
end

#fail!(context = {}) ⇒ void

This method returns an undefined value.

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.

Parameters:

  • context (Hash) (defaults to: {})

    data to be merged into the existing context

Raises:



52
53
54
55
56
# 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

#failure?Boolean

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.

Returns:

  • (Boolean)

    false by default, or true if failed



38
39
40
# File 'lib/interaktor/context.rb', line 38

def failure?
  @failure || false
end

#rollback!Boolean

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.

Returns:

  • (Boolean)

    true if rolled back successfully, false if already rolled back



74
75
76
77
78
79
# File 'lib/interaktor/context.rb', line 74

def rollback!
  return false if @rolled_back

  _called.reverse_each(&:rollback)
  @rolled_back = true
end

#success!(context = {}) ⇒ void

This method returns an undefined value.

Parameters:

  • context (Hash) (defaults to: {})

    data to be merged into the existing context



63
64
65
66
# File 'lib/interaktor/context.rb', line 63

def success!(context = {})
  context.each { |key, value| self[key.to_sym] = value }
  early_return!
end

#success?Boolean

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.

Returns:

  • (Boolean)

    true by default, or false if failed



29
30
31
# File 'lib/interaktor/context.rb', line 29

def success?
  !failure?
end