Class: Symbiont::Isolator

Inherits:
Object
  • Object
show all
Defined in:
lib/symbiont/isolator.rb

Overview

Special object that wraps your proc object from any place and provides an ability to invoke this proc object lazily inside an any series of contexts.

Since:

  • 0.3.0

Constant Summary collapse

UnprovidedClosureAttributeError =

Is raised when closure is not provided.

See Also:

Since:

  • 0.3.0

Class.new(ArgumentError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_direction: Trigger::IOK, &closure) ⇒ Isolator

Instantiates isolator object with corresponding default direction and closure.

Parameters:

  • closure (Proc)

    Proc object that will be evaluated in many contexts: initial, outer and kernel. Will be used as an outer-context for the method resolution.

  • default_direction (Hash) (defaults to: Trigger::IOK)

    a customizable set of options

Options Hash (default_direction:):

  • An (Array<Symbol>)

    array of symbols that represents the direction of contexts which is used as default context direction. Symbiont::Trigger::IOK is chosen by default.

Raises:

Since:

  • 0.3.0


46
47
48
49
50
51
# File 'lib/symbiont/isolator.rb', line 46

def initialize(default_direction: Trigger::IOK, &closure)
  raise UnprovidedClosureAttributeError, 'You should provide a closure' unless block_given?

  @default_direction = default_direction
  @closure = closure
end

Instance Attribute Details

#closureProc (readonly)

Proc object that will be evaluated in many contexts: initial, outer and kernel. Will be used as an outer-context for the method resolution.

Returns:

  • (Proc)

Since:

  • 0.3.0


25
26
27
# File 'lib/symbiont/isolator.rb', line 25

def closure
  @closure
end

#default_directionArray<Symbol> (readonly)

An array of symbols that represents the direction of contexts. Used by default.

Returns:

  • (Array<Symbol>)

Since:

  • 0.3.0


33
34
35
# File 'lib/symbiont/isolator.rb', line 33

def default_direction
  @default_direction
end

Instance Method Details

#evaluate(*required_contexts, direction: default_direction) ⇒ void

This method returns an undefined value.

Starts execution of a proc object in the context of the passed object with the selected direction of method dispatching. Delegates execution to a public trigger.

Parameters:

  • required_contexts (Array<Object>)

    A set of objects that should be used as the main context series for method resolving algorithm.

  • direction (Array<Symbol>) (defaults to: default_direction)

    An array of symbols that represents the direction of contexts.

See Also:

Since:

  • 0.3.0


68
69
70
# File 'lib/symbiont/isolator.rb', line 68

def evaluate(*required_contexts, direction: default_direction)
  public_trigger(*required_contexts, direction: direction).__evaluate__
end

#evaluate_private(*required_contexts, direction: default_direction) ⇒ void

This method returns an undefined value.

Starts execution of a proc object in the context of the passed object with the selected direction of method dispatching. Delegates execution to a private trigger.

Parameters:

  • required_contexts (Array<Object>)

    A set of objects that should be used as the main context series for method resolving algorithm.

  • direction (Array<Symbol>) (defaults to: default_direction)

    An array of symbols that represents the direction of contexts.

See Also:

Since:

  • 0.3.0


87
88
89
# File 'lib/symbiont/isolator.rb', line 87

def evaluate_private(*required_contexts, direction: default_direction)
  private_trigger(*required_contexts, direction: direction).__evaluate__
end

#private_method(method_name, *required_contexts, direction: default_direction) ⇒ Method

Gets the method object taken from the context that can respond to it. Considers private methods and public methods.

Parameters:

  • method_name (Symbol, String)

    A name of required method.

  • required_contexts (Array<Object>)

    A set of objects that should be used as the main context series for method resolving algorithm.

  • direction (Array<Symbol>) (defaults to: default_direction)

    An array of symbols that represents the direction of contexts.

Returns:

  • (Method)

See Also:

Since:

  • 0.3.0


127
128
129
# File 'lib/symbiont/isolator.rb', line 127

def private_method(method_name, *required_contexts, direction: default_direction)
  private_trigger(*required_contexts, direction: direction).method(method_name)
end

#private_trigger(*required_contexts, direction: default_direction) ⇒ Symbiont::PrivateTrigger (private)

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.

Factory method that instantiates a private trigger with the desired execution context, the direction of method dispatching and the closure that needs to be performed.

Parameters:

  • required_contexts (Array<Object>)

    A set of objects that should be used as the main context series for method resolving algorithm.

  • direction (Array<Symbol>) (defaults to: default_direction)

    An array of symbols that represents the direction of contexts. Possible values:

    • Symbiont::IOK
    • Symbiont::OIK
    • Symbiont::OKI
    • Symbiont::IKO
    • Symbiont::KOI
    • Symbiont::KIO

Returns:

See Also:

Since:

  • 0.3.0


181
182
183
# File 'lib/symbiont/isolator.rb', line 181

def private_trigger(*required_contexts, direction: default_direction)
  PrivateTrigger.new(*required_contexts, context_direction: direction, &closure)
end

#public_method(method_name, *required_contexts, direction: default_direction) ⇒ Method

Gets the method object taken from the context that can respond to it. Considers only public methods.

Parameters:

  • method_name (Symbol, String)

    A name of required method.

  • required_contexts (Array<Object>)

    A set of objects that should be used as the main context series for method resolving algorithm.

  • direction (Array<Symbol>) (defaults to: default_direction)

    An array of symbols that represents the direction of contexts.

Returns:

  • (Method)

See Also:

Since:

  • 0.3.0


107
108
109
# File 'lib/symbiont/isolator.rb', line 107

def public_method(method_name, *required_contexts, direction: default_direction)
  public_trigger(*required_contexts, direction: direction).method(method_name)
end

#public_trigger(*required_contexts, direction: default_direction) ⇒ Symbiont::PublicTrigger (private)

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.

Factory method that instantiates a public trigger with the desired execution context, the direction of method dispatching and the closure that needs to be performed.

Parameters:

  • required_contexts (Array<Object>)

    A set of objects that should be used as the main context series for method resolving algorithm.

  • direction (Array<Symbol>) (defaults to: default_direction)

    An array of symbols that represents the direction of contexts. Possible values:

    • Symbiont::IOK
    • Symbiont::OIK
    • Symbiont::OKI
    • Symbiont::IKO
    • Symbiont::KOI
    • Symbiont::KIO

Returns:

See Also:

Since:

  • 0.3.0


155
156
157
# File 'lib/symbiont/isolator.rb', line 155

def public_trigger(*required_contexts, direction: default_direction)
  PublicTrigger.new(*required_contexts, context_direction: direction, &closure)
end