Class: Symbiont::Trigger Private
- Inherits:
- BasicObject
- Defined in:
- lib/symbiont/trigger.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A class that controls the logic of closure execution in the context of other objects. Responsible for dispatching the methods which should be executed in a certain context. Delegation variations depends on the order of contexts.
Trigger supports 3 contexts:
- closure context;
- passed object's context (or context of the each passed object);
- global ::Kernel context.
If no context is able to respond to the required method - ContextNoMethodError exception is raised (ContextNoMethodError inherits from NoMethodError).
Direct Known Subclasses
Constant Summary collapse
- IOK =
Indicates the direction of context method resolving algorithm. Direction: initial context => outer context => kernel context.
%i[__inner_contexts__ __outer_context__ __kernel_context__].freeze
- OIK =
Indicates the direction of context method resolving algorithm. Direction: outer context => initial contexts => kernel context.
%i[__outer_context__ __inner_contexts__ __kernel_context__].freeze
- OKI =
Indicates the direction of context method resolving algorithm. Direction: outer context => kernel context => initial contexts.
%i[__outer_context__ __kernel_context__ __inner_contexts__].freeze
- IKO =
Indicates the direction of context method resolving algorithm. Direction: initial contexts => kernel context => outer context.
%i[__inner_contexts__ __kernel_context__ __outer_context__].freeze
- KOI =
Indicates the direction of context method resolving algorithm. Direction: kernel context => outer context => initial contexts.
%i[__kernel_context__ __outer_context__ __inner_contexts__].freeze
- KIO =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Indicates the direction of context method resolving algorithm. Direction: kernel context => initial contexts => outer context.
api public
%i[__kernel_context__ __inner_contexts__ __outer_context__].freeze
- IncompatibleContextDirectionError =
Is raised when chosen direction (context_direction instance attribute) is not supported by a trigger. Supports only: OIK, OKI, IOK, IKO, KOI, KIO.
::Class.new(::ArgumentError)
- UnprovidedClosureAttributeError =
Is raised when closure isnt passed.
::Class.new(::ArgumentError)
- ContextNoMethodError =
Is raised when no one is able to respond to the required method.
::Class.new(::NoMethodError)
Instance Attribute Summary collapse
-
#__closure__ ⇒ Proc
readonly
private
Returns proc object that will be triggered in many contexts: initial, outer and kernel.
-
#__context_direction__ ⇒ Array<Symbol>
readonly
private
Returns an array of symbols that represents the direction of contexts.
-
#__inner_contexts__ ⇒ Array<Object>
readonly
private
Returns a set of objects that should be used as the main context series for context method resolving algorithm.
-
#__kernel_context__ ⇒ ::Kernel
readonly
private
Returns Kernel object that will be used as Kernel context for context method resolving algorithm.
-
#__outer_context__ ⇒ Object
readonly
private
Returns a binding object of corresponding closure (see closure).
Instance Method Summary collapse
-
#__actual_context__(method_name) ⇒ Object
private
Returns the first context that is able to respond to the required method.
-
#__directed_contexts__ ⇒ Array<Object>
private
Returns a collection of the all contexts sorted by chosen direction.
-
#__evaluate__ ⇒ Object
private
Triggers a closure in multiple contexts.
-
#initialize(*initial_contexts, context_direction: IOK, &closure) ⇒ Trigger
constructor
private
Instantiates trigger object with corresponding initial contexts, closure and context resolving direction.
-
#method(method_name) ⇒ Method
private
Returns a corresponding metehod object of the actual context.
-
#method_missing(method_name, *arguments, &block) ⇒ Object
private
Delegates method invocation to the corresponding actual context.
-
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
private
Checks that the actual context is able to respond to a required method.
Constructor Details
#initialize(*initial_contexts, context_direction: IOK, &closure) ⇒ Trigger
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Instantiates trigger object with corresponding initial contexts, closure and context resolving direction.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/symbiont/trigger.rb', line 155 def initialize(*initial_contexts, context_direction: IOK, &closure) # :nocov: unless ::Kernel.block_given? ::Kernel.raise(UnprovidedClosureAttributeError, 'block attribute should be provided') end # :nocov: # rubocop:disable Layout/SpaceAroundKeyword, Style/MultipleComparison unless(context_direction == IOK || context_direction == OIK || context_direction == OKI || context_direction == IKO || context_direction == KOI || context_direction == KIO) ::Kernel.raise( IncompatibleContextDirectionError, 'Incompatible context direction attribute. ' \ 'You should use one of this: OIK, OKI, IOK, IKO, KOI, KIO.' ) end # rubocop:enable Layout/SpaceAroundKeyword, Style/MultipleComparison @__closure__ = closure @__context_direction__ = context_direction @__inner_contexts__ = initial_contexts @__outer_context__ = ::Kernel.eval('self', closure.binding) @__kernel_context__ = ::Kernel end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Delegates method invocation to the corresponding actual context.
235 236 237 |
# File 'lib/symbiont/trigger.rb', line 235 def method_missing(method_name, *arguments, &block) __actual_context__(method_name).send(method_name, *arguments, &block) end |
Instance Attribute Details
#__closure__ ⇒ Proc (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns proc object that will be triggered in many contexts: initial, outer and kernel.
127 128 129 |
# File 'lib/symbiont/trigger.rb', line 127 def __closure__ @__closure__ end |
#__context_direction__ ⇒ Array<Symbol> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns an array of symbols that represents the direction of contexts. that represents an access method to each of them.
136 137 138 |
# File 'lib/symbiont/trigger.rb', line 136 def __context_direction__ @__context_direction__ end |
#__inner_contexts__ ⇒ Array<Object> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a set of objects that should be used as the main context series for context method resolving algorithm. The order of object selection depends on their order in a set.
101 102 103 |
# File 'lib/symbiont/trigger.rb', line 101 def __inner_contexts__ @__inner_contexts__ end |
#__kernel_context__ ⇒ ::Kernel (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Kernel object that will be used as Kernel context for context method resolving algorithm.
119 120 121 |
# File 'lib/symbiont/trigger.rb', line 119 def __kernel_context__ @__kernel_context__ end |
#__outer_context__ ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a binding object of corresponding closure (see closure). Used as an outer context for context method resolving algorithm.
110 111 112 |
# File 'lib/symbiont/trigger.rb', line 110 def __outer_context__ @__outer_context__ end |
Instance Method Details
#__actual_context__(method_name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the first context that is able to respond to the required method. The context is chosen in the context direction order (see #context_direction). Raises NoMethodError excepition when no one of the contexts are able to respond to the required method. Basicaly, abstract implementation raises NoMethodError.
218 219 220 |
# File 'lib/symbiont/trigger.rb', line 218 def __actual_context__(method_name) ::Kernel.raise ContextNoMethodError, "No one is able to respond to #{method_name}" end |
#__directed_contexts__ ⇒ Array<Object>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a collection of the all contexts sorted by chosen direction. Represents ordered single-dimentional array of objects (contexts).
201 202 203 |
# File 'lib/symbiont/trigger.rb', line 201 def __directed_contexts__ __context_direction__.map { |direction| __send__(direction) }.flatten end |
#__evaluate__ ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Triggers a closure in multiple contexts.
188 189 190 |
# File 'lib/symbiont/trigger.rb', line 188 def __evaluate__ instance_eval(&__closure__) end |
#method(method_name) ⇒ Method
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a corresponding metehod object of the actual context.
271 272 273 |
# File 'lib/symbiont/trigger.rb', line 271 def method(method_name) __actual_context__(method_name).method(method_name) end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks that the actual context is able to respond to a required method.
:nocov:
253 254 255 |
# File 'lib/symbiont/trigger.rb', line 253 def respond_to_missing?(method_name, _include_private = false) !!__actual_context__(method_name) end |