Class: Interaktor::Interaction

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interaktor, input) ⇒ Interaction

Returns a new instance of Interaction.

Parameters:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/interaktor/interaction.rb', line 7

def initialize(interaktor, input)
  @interaktor = interaktor
  @executed = false
  @failed = false
  @rolled_back = false

  @input_args = case input
  when Hash
    input.transform_keys(&:to_sym)
  when Interaction
    input
      .input_args
      .merge(input.success_args || {})
      .slice(*(interaktor.class.input_attributes || []))
  else
    raise ArgumentError, "Invalid input type: #{input.class}"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Only allow access to arguments when appropriate. Input arguments should be accessible only during the interaction’s execution, and after the execution is complete, either the success or failure arguments should be accessible, depending on the outcome.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/interaktor/interaction.rb', line 93

def method_missing(method_name, *args, &block)
  if !@executed && input_args.key?(method_name)
    input_args[method_name]
  elsif success? && allowable_success_attributes.include?(method_name)
    success_args[method_name]
  elsif failure? && allowable_failure_attributes.include?(method_name)
    failure_args[method_name]
  else
    super
  end
end

Instance Attribute Details

#failure_argsObject (readonly)

Returns the value of attribute failure_args.



3
4
5
# File 'lib/interaktor/interaction.rb', line 3

def failure_args
  @failure_args
end

#input_argsObject (readonly)

Returns the value of attribute input_args.



3
4
5
# File 'lib/interaktor/interaction.rb', line 3

def input_args
  @input_args
end

#success_argsObject (readonly)

Returns the value of attribute success_args.



3
4
5
# File 'lib/interaktor/interaction.rb', line 3

def success_args
  @success_args
end

Instance Method Details

#_calledArray<Interaktor>

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

Returns:



139
140
141
# File 'lib/interaktor/interaction.rb', line 139

def _called
  @called ||= []
end

#allowable_failure_attributesObject



85
86
87
# File 'lib/interaktor/interaction.rb', line 85

def allowable_failure_attributes
  @interaktor.class.failure_attributes
end

#allowable_success_attributesObject



81
82
83
# File 'lib/interaktor/interaction.rb', line 81

def allowable_success_attributes
  @interaktor.class.success_attributes
end

#called!(interaktor) ⇒ Object

Track that an Interaktor has been called. The ‘#called!` method is used by the interaktor being invoked. After an interaktor is successfully called, the interaction is tracked for the purpose of potential future rollback.

Parameters:

  • interaktor (Interaktor)

    an interaktor that has been successfully called



130
131
132
133
# File 'lib/interaktor/interaction.rb', line 130

def called!(interaktor)
  @executed = true
  _called << interaktor
end

#early_return!Object

Trigger an early return throw.



144
145
146
147
# File 'lib/interaktor/interaction.rb', line 144

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

#early_return?Boolean

Whether or not the interaction has been returned from early.

Returns:

  • (Boolean)


150
151
152
# File 'lib/interaktor/interaction.rb', line 150

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

#fail!(args = {}) ⇒ Object

Parameters:

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

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/interaktor/interaction.rb', line 53

def fail!(args = {})
  if @executed
    raise Interaktor::Error::InvalidMethodForStateError.new(
      self,
      "Cannot call `fail!` after interaktor execution is already complete"
    )
  end

  @executed = true
  @failed = true
  @failure_args = args.transform_keys(&:to_sym)
  raise Interaktor::Failure, self
end

#failure?Boolean

Whether the interaction has failed.

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
# File 'lib/interaktor/interaction.rb', line 39

def failure?
  if !@executed
    raise Interaktor::Error::InvalidMethodForStateError.new(
      self,
      "Cannot call `failure?` before interaktor execution is complete"
    )
  end

  @failed
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
# File 'lib/interaktor/interaction.rb', line 105

def respond_to_missing?(method_name, include_private = false)
  input_args.key?(method_name) ||
    success_args&.key?(method_name) ||
    failure_args&.key?(method_name) ||
    super
end

#rollback!Boolean

Roll back the interaction. Successful interactions may have this method called to roll back their state.

Returns:

  • (Boolean)

    true if rolled back successfully, false if already rolled back



117
118
119
120
121
122
# File 'lib/interaktor/interaction.rb', line 117

def rollback!
  return false if @rolled_back

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

#success!(args = {}) ⇒ Object

Parameters:

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


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/interaktor/interaction.rb', line 68

def success!(args = {})
  if @executed
    raise Interaktor::Error::InvalidMethodForStateError.new(
      self,
      "Cannot call `success!` after interaktor execution is already complete"
    )
  end

  @executed = true
  @success_args = args.transform_keys(&:to_sym)
  early_return!
end

#success?Boolean

Whether the interaction is successful.

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
# File 'lib/interaktor/interaction.rb', line 27

def success?
  if !@executed
    raise Interaktor::Error::InvalidMethodForStateError.new(
      self,
      "Cannot call `success?` before interaktor execution is complete"
    )
  end

  !failure?
end