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).

Since:

  • 0.1.0

Direct Known Subclasses

PrivateTrigger, PublicTrigger

Constant Summary collapse

IOK =

Indicates the direction of context method resolving algorithm. Direction: initial context => outer context => kernel context.

Since:

  • 0.1.0

%i[__inner_contexts__ __outer_context__ __kernel_context__].freeze
OIK =

Indicates the direction of context method resolving algorithm. Direction: outer context => initial contexts => kernel context.

Since:

  • 0.1.0

%i[__outer_context__ __inner_contexts__ __kernel_context__].freeze
OKI =

Indicates the direction of context method resolving algorithm. Direction: outer context => kernel context => initial contexts.

Since:

  • 0.1.0

%i[__outer_context__ __kernel_context__ __inner_contexts__].freeze
IKO =

Indicates the direction of context method resolving algorithm. Direction: initial contexts => kernel context => outer context.

Since:

  • 0.1.0

%i[__inner_contexts__ __kernel_context__ __outer_context__].freeze
KOI =

Indicates the direction of context method resolving algorithm. Direction: kernel context => outer context => initial contexts.

Since:

  • 0.1.0

%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

Since:

  • 0.1.0

%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.

Since:

  • 0.1.0

::Class.new(::ArgumentError)
UnprovidedClosureAttributeError =

Is raised when closure isnt passed.

Since:

  • 0.2.0

::Class.new(::ArgumentError)
ContextNoMethodError =

Is raised when no one is able to respond to the required method.

See Also:

Since:

  • 0.1.0

::Class.new(::NoMethodError)

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Raises:

  • UnprovidedClosureAttributeError Raises when closure attribte isnt passed.

  • IncompatibleContextDirectionError Is raised when chosen direction is not supported by a trigger. Supports only OIK, OKI, IOK, IKO, KOI, KIO (see corresponding constant value above).

Since:

  • 0.1.0


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.

Raises:

  • ContextNoMethodError Is rased when no one of the contexts are able to respond tothe required method.

See Also:

Since:

  • 0.1.0


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.

Since:

  • 0.1.0


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.

Since:

  • 0.1.0


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.

Since:

  • 0.1.0


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.

Since:

  • 0.1.0


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.

Since:

  • 0.1.0


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.

Raises:

  • ContextNoMethodError

See Also:

Since:

  • 0.1.0


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).

See Also:

Since:

  • 0.1.0


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.

See Also:

Since:

  • 0.1.0


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.

Raises:

  • ContextNoMethodError Is raised when no one of the contexts able to respond to the required method.

See Also:

Since:

  • 0.1.0


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:

Raises:

  • NoMethodError Is raised when no one of the contexts are able to respond to the required method.

See Also:

Since:

  • 0.1.0


253
254
255
# File 'lib/symbiont/trigger.rb', line 253

def respond_to_missing?(method_name, _include_private = false)
  !!__actual_context__(method_name)
end