Class: ActiveInteractor::Context::Base
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- ActiveInteractor::Context::Base
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/active_interactor/context.rb
Overview
The base context class inherited by all Interactor::Context classes
Class Method Summary collapse
-
.attributes ⇒ Array<Symbol>
Attributes defined on the context class.
-
.attributes=(*attributes) ⇒ Array<Symbol>
Set attributes on a context class.
Instance Method Summary collapse
-
#attributes ⇒ Hash{Symbol => *}
Attributes defined on the instance.
-
#called! ⇒ Array<ActiveInteractor::Base>
Track that an Interactor has been called.
-
#clean! ⇒ Hash{Symbol => *}
Removes properties from the instance that are not explicitly defined in the context instance #attributes.
-
#fail!(errors = {}) ⇒ Object
Fail the context instance.
-
#failure? ⇒ Boolean
(also: #fail?)
Whether the context instance has failed.
-
#initialize(interactor, attributes = {}) ⇒ ActiveInteractor::Context::Base
constructor
A new instance of Base.
-
#keys ⇒ Array<Symbol>
All keys of properties currently defined on the instance.
-
#method_missing(name, *args, &block) ⇒ Object
Attempt to call the interactor for missing validation callback methods.
-
#respond_to_missing?(name, include_private) ⇒ Boolean
Attempt to call the interactor for missing validation callback methods.
-
#rollback! ⇒ Boolean
Roll back an interactor context.
-
#success? ⇒ Boolean
(also: #successful?)
Whether the context instance is successful.
Constructor Details
#initialize(interactor, attributes = {}) ⇒ ActiveInteractor::Context::Base
A new instance of ActiveInteractor::Context::Base
47 48 49 50 51 |
# File 'lib/active_interactor/context.rb', line 47 def initialize(interactor, attributes = {}) copy_flags!(attributes) @interactor = interactor super(attributes) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Attempt to call the interactor for missing validation callback methods
218 219 220 221 |
# File 'lib/active_interactor/context.rb', line 218 def method_missing(name, *args, &block) interactor.send(name, *args, &block) if validation_callback?(name) super end |
Class Method Details
.attributes ⇒ Array<Symbol>
Attributes defined on the context class
61 62 63 64 65 66 |
# File 'lib/active_interactor/context.rb', line 61 def attributes __default_attributes .concat(_validate_callbacks.map(&:filter).map(&:attributes).flatten) .flatten .uniq end |
.attributes=(*attributes) ⇒ Array<Symbol>
Set attributes on a context class
76 77 78 |
# File 'lib/active_interactor/context.rb', line 76 def attributes=(*attributes) self.__default_attributes = self.attributes.concat(attributes.flatten.map(&:to_sym)).uniq end |
Instance Method Details
#attributes ⇒ Hash{Symbol => *}
Attributes defined on the instance
107 108 109 110 111 |
# File 'lib/active_interactor/context.rb', line 107 def attributes self.class.attributes.each_with_object({}) do |attribute, hash| hash[attribute] = self[attribute] if self[attribute] end end |
#called! ⇒ Array<ActiveInteractor::Base>
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
119 120 121 |
# File 'lib/active_interactor/context.rb', line 119 def called! _called << interactor end |
#clean! ⇒ Hash{Symbol => *}
Removes properties from the instance that are not explicitly defined in the context instance #attributes
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/active_interactor/context.rb', line 143 def clean! deleted = {} return deleted if keys.empty? keys.reject { |key| self.class.attributes.include?(key) }.each do |attribute| deleted[attribute] = self[attribute] if self[attribute] delete_field(key.to_s) end deleted end |
#fail!(errors = {}) ⇒ Object
Fail the context instance. Failing a context raises an error
that may be rescued by the calling interactor. The context is also flagged
as having failed
168 169 170 171 172 |
# File 'lib/active_interactor/context.rb', line 168 def fail!(errors = {}) self.errors.merge!(errors) unless errors.empty? @_failed = true raise Failure, self end |
#failure? ⇒ Boolean Also known as: fail?
193 194 195 |
# File 'lib/active_interactor/context.rb', line 193 def failure? @_failed || false end |
#keys ⇒ Array<Symbol>
All keys of properties currently defined on the instance
211 212 213 |
# File 'lib/active_interactor/context.rb', line 211 def keys each_pair.map { |pair| pair[0].to_sym } end |
#respond_to_missing?(name, include_private) ⇒ Boolean
Attempt to call the interactor for missing validation callback methods
226 227 228 229 230 |
# File 'lib/active_interactor/context.rb', line 226 def respond_to_missing?(name, include_private) return false unless validation_callback?(name) interactor.respond_to?(name, include_private) end |
#rollback! ⇒ Boolean
Roll back an 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.
249 250 251 252 253 254 |
# File 'lib/active_interactor/context.rb', line 249 def rollback! return false if @_rolled_back _called.reverse_each(&:execute_rollback) @_rolled_back = true end |
#success? ⇒ Boolean Also known as: successful?
275 276 277 |
# File 'lib/active_interactor/context.rb', line 275 def success? !failure? end |