Module: Lasershark::CommonContext
- Defined in:
- lib/lasershark/common_context.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#_called ⇒ Object
Internal: An Array of successfully called Interactor instances invoked against this Interactor::Context instance.
-
#called!(interactor) ⇒ Object
Internal: Track that an Interactor has been called.
-
#fail!(context = {}) ⇒ Object
Public: Fail the Interactor::Context.
-
#failure? ⇒ Boolean
Public: Whether the Interactor::Context has failed.
-
#rollback! ⇒ Object
Public: Roll back the Interactor::Context.
-
#success? ⇒ Boolean
Public: Whether the Interactor::Context is successful.
Class Method Details
.included(base) ⇒ Object
32 33 34 |
# File 'lib/lasershark/common_context.rb', line 32 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#_called ⇒ Object
Internal: An Array of successfully called Interactor instances invoked against this Interactor::Context instance.
Examples
context = Interactor::Context.new
# => #<Interactor::Context>
context._called
# => []
context = MyInteractor.call(foo: "bar")
# => #<Interactor::Context foo="baz">
context._called
# => [#<MyInteractor @context=#<Interactor::Context foo="baz">>]
Returns an Array of Interactor instances or an empty Array.
154 155 156 |
# File 'lib/lasershark/common_context.rb', line 154 def _called @called ||= [] end |
#called!(interactor) ⇒ Object
Internal: Track that an Interactor has been called. The “called!” method is used by the interactor being invoked with this context. After an interactor is successfully called, the interactor instance is tracked in the context for the purpose of potential future rollback.
interactor - An Interactor instance that has been successfully called.
Returns nothing.
114 115 116 |
# File 'lib/lasershark/common_context.rb', line 114 def called!(interactor) _called << interactor end |
#fail!(context = {}) ⇒ Object
Public: Fail the Interactor::Context. Failing a context raises an error that may be rescued by the calling interactor. 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.
context - A Hash whose key/value pairs are merged into the existing
Interactor::Context instance. (default: {})
Examples
context = Interactor::Context.new
# => #<Interactor::Context>
context.fail!
# => Interactor::Failure: #<Interactor::Context>
context.fail! rescue false
# => false
context.fail!(foo: "baz")
# => Interactor::Failure: #<Interactor::Context foo="baz">
Raises Interactor::Failure initialized with the Interactor::Context.
100 101 102 103 104 |
# File 'lib/lasershark/common_context.rb', line 100 def fail!(context = {}) modifiable.update(context) @failure = true raise Interactor::Failure, self end |
#failure? ⇒ Boolean
Public: Whether the Interactor::Context has failed. By default, a new context is successful and only changes when explicitly failed.
The “failure?” method is the inverse of the “success?” method.
Examples
context = Interactor::Context.new
# => #<Interactor::Context>
context.failure?
# => false
context.fail!
# => Interactor::Failure: #<Interactor::Context>
context.failure?
# => true
Returns false by default or true if failed.
74 75 76 |
# File 'lib/lasershark/common_context.rb', line 74 def failure? @failure || false end |
#rollback! ⇒ Object
Public: Roll back the Interactor::Context. Any interactors to which this context has been passed and which have been successfully called are asked to roll themselves back by invoking their “rollback” instance methods.
Examples
context = MyInteractor.call(foo: "bar")
# => #<Interactor::Context foo="baz">
context.rollback!
# => true
context
# => #<Interactor::Context foo="bar">
Returns true if rolled back successfully or false if already rolled back.
132 133 134 135 136 |
# File 'lib/lasershark/common_context.rb', line 132 def rollback! return false if @rolled_back _called.reverse_each(&:rollback) @rolled_back = true end |
#success? ⇒ Boolean
Public: Whether the Interactor::Context is successful. By default, a new context is successful and only changes when explicitly failed.
The “success?” method is the inverse of the “failure?” method.
Examples
context = Interactor::Context.new
# => #<Interactor::Context>
context.success?
# => true
context.fail!
# => Interactor::Failure: #<Interactor::Context>
context.success?
# => false
Returns true by default or false if failed.
53 54 55 |
# File 'lib/lasershark/common_context.rb', line 53 def success? !failure? end |