Module: Interaktor

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

Defined Under Namespace

Modules: Callable, ClassMethods, Hooks, Organizer Classes: 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
22
23
# 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) do
    end

    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.



57
58
# File 'lib/interaktor.rb', line 57

def call
end

#fail!(args = {}) ⇒ Object

Parameters:

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


35
36
37
38
39
40
41
42
# File 'lib/interaktor.rb', line 35

def fail!(args = {})
  if (disallowed_key = args.keys.find { |k| !self.class.failure_attributes.include?(k.to_sym) })
    raise Interaktor::Error::UnknownAttributeError.new(self, disallowed_key)
  end

  self.class.validate_failure_schema(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



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

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.



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

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.



70
71
72
73
# File 'lib/interaktor.rb', line 70

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.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/interaktor.rb', line 84

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

    if self.class.required_success_attributes.any? && !@interaction.success_args
      raise Interaktor::Error::MissingExplicitSuccessError.new(self, self.class.required_success_attributes)
    end

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

#success!(args = {}) ⇒ Object

Parameters:

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


45
46
47
48
49
50
51
52
# File 'lib/interaktor.rb', line 45

def success!(args = {})
  if (disallowed_key = args.keys.find { |k| !self.class.success_attributes.include?(k.to_sym) })
    raise Interaktor::Error::UnknownAttributeError.new(self, disallowed_key)
  end

  self.class.validate_success_schema(args)
  @interaction.success!(args)
end