Module: Interaktor

Defined in:
lib/interaktor.rb,
lib/interaktor/attributes.rb,
lib/interaktor/interaction.rb

Defined Under Namespace

Modules: Callable, ClassMethods, Hooks, Organizer Classes: Attributes, Failure, Interaction

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
18
19
20
21
# File 'lib/interaktor.rb', line 12

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

    interaction_class = Class.new(Interaktor::Interaction)
    base.const_set(:Interaction, interaction_class)
  end
end

Instance Method Details

#callObject

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



45
46
# File 'lib/interaktor.rb', line 45

def call
end

#fail!(args = {}) ⇒ Object

Parameters:

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


33
34
35
# File 'lib/interaktor.rb', line 33

def fail!(args = {})
  @interaction.fail!(args)
end

#initialize(args = {}) ⇒ Object

Parameters:

  • args (Hash, Interaktor::Interaction) (defaults to: {})

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



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

def initialize(args = {})
  @interaction = self.class::Interaction.new(self, args)
end

#rollbackObject

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



51
52
# File 'lib/interaktor.rb', line 51

def rollback
end

#runObject

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.



58
59
60
61
# File 'lib/interaktor.rb', line 58

def run
  run!
rescue Interaktor::Failure
end

#run!Object

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 interaction. If the interaction is failed or any error is raised, the interaction is rolled back. This method behaves identically to #run with one notable exception - if the interaction is failed during the invocation of the interaktor, Interaktor::Failure is raised.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/interaktor.rb', line 72

def run!
  with_hooks do
    catch(:early_return) do
      call
    end

    @interaction.called!(self)
  end
rescue
  @interaction.rollback!
  raise
end

#success!(args = {}) ⇒ Object

Parameters:

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


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

def success!(args = {})
  @interaction.success!(args)
end