Module: Interaktor
- Defined in:
- lib/interaktor.rb
Defined Under Namespace
Modules: Callable, ClassMethods, Hooks, Organizer Classes: Context, Failure
Class Method Summary collapse
-
.included(base) ⇒ Object
When the Interaktor module is included in a class, add the relevant class methods and hooks to that class.
Instance Method Summary collapse
-
#call ⇒ void
Invoke an Interaktor instance without any hooks, tracking, or rollback.
-
#fail!(failure_attributes = {}) ⇒ void
Fail the current interaktor.
- #initialize(context = {}) ⇒ Object
-
#rollback ⇒ void
Reverse prior invocation of an Interaktor instance.
-
#run ⇒ void
Invoke an interaktor instance along with all defined hooks.
-
#run! ⇒ void
Invoke an Interaktor instance along with all defined hooks, typically used internally by ‘.call!`.
-
#success!(success_attributes = {}) ⇒ void
Terminate execution of the current interaktor and copy the success attributes into the context.
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.
12 13 14 15 16 17 18 |
# File 'lib/interaktor.rb', line 12 def self.included(base) base.class_eval do extend ClassMethods include Hooks include Callable end end |
Instance Method Details
#call ⇒ void
This method returns an undefined value.
Invoke an Interaktor instance without any hooks, tracking, or rollback. It is expected that the ‘#call` instance method is overwritten for each interaktor class.
65 |
# File 'lib/interaktor.rb', line 65 def call; end |
#fail!(failure_attributes = {}) ⇒ void
This method returns an undefined value.
Fail the current interaktor.
34 35 36 37 38 39 40 41 42 |
# File 'lib/interaktor.rb', line 34 def fail!(failure_attributes = {}) # Silently remove any attributes that are not included in the schema allowed_keys = self.class.failure_schema.key_map.keys.map { |k| k.name.to_sym } failure_attributes.select! { |k, _| allowed_keys.include?(k.to_sym) } self.class.validate_failure_schema(failure_attributes) @context.fail!(failure_attributes) end |
#initialize(context = {}) ⇒ Object
25 26 27 |
# File 'lib/interaktor.rb', line 25 def initialize(context = {}) @context = Interaktor::Context.build(context) end |
#rollback ⇒ void
This method returns an undefined value.
Reverse prior invocation of an Interaktor instance. Any interaktor class that requires undoing upon downstream failure is expected to overwrite the ‘#rollback` instance method.
72 |
# File 'lib/interaktor.rb', line 72 def rollback; end |
#run ⇒ void
This method returns an undefined value.
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.
81 82 83 84 |
# File 'lib/interaktor.rb', line 81 def run run! rescue Interaktor::Failure # rubocop:disable Lint/SuppressedException end |
#run! ⇒ void
This method returns an undefined value.
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 context. If the context is failed or any error is raised, the context is rolled back. This method behaves identically to `#run` with one notable exception - if the context is failed during the invocation of the interaktor, `Interaktor::Failure` is raised.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/interaktor.rb', line 96 def run! with_hooks do catch(:early_return) do call end if !@context.early_return? && self.class.required_success_attributes.any? raise Interaktor::Error::MissingExplicitSuccessError.new(self, self.class.required_success_attributes) end @context.called!(self) end rescue StandardError @context.rollback! raise end |
#success!(success_attributes = {}) ⇒ void
This method returns an undefined value.
Terminate execution of the current interaktor and copy the success attributes into the context.
50 51 52 53 54 55 56 57 58 |
# File 'lib/interaktor.rb', line 50 def success!(success_attributes = {}) # Silently remove any attributes that are not included in the schema allowed_keys = self.class.success_schema.key_map.keys.map { |k| k.name.to_sym } success_attributes.select! { |k, _| allowed_keys.include?(k.to_sym) } self.class.validate_success_schema(success_attributes) @context.success!(success_attributes) end |