Class: Vissen::Parameterized::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/vissen/parameterized/scope.rb

Overview

The scope exists to protect parameterized objects from being bound to parameters with a different lifetime than their own.

Each scope is bound to a ‘Conditional` and as long as it, as well as the conditionals of all the parents return false, the scope is considered to be alive.

By checking that one scope is included in another, it is possible to guarantee that values belonging to the first scope are safe to use.

Direct Known Subclasses

GlobalScope

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentScope (readonly)

Returns the parent of this scope.

Returns:

  • (Scope)

    the parent of this scope.



16
17
18
# File 'lib/vissen/parameterized/scope.rb', line 16

def parent
  @parent
end

Instance Method Details

#alive?false, true

The inverse of ‘#dead?`

Returns:

  • (false)

    if the conditional is met.

  • (true)

    otherwise.

See Also:



33
34
35
# File 'lib/vissen/parameterized/scope.rb', line 33

def alive?
  !dead?
end

#create_scope(conditional) ⇒ Scope

Creates a new scope that is a direct descendent of this one.

Parameters:

  • conditional (Conditional)

    the conditional to use for the new scope.

Returns:

  • (Scope)

    a new child scope.



61
62
63
# File 'lib/vissen/parameterized/scope.rb', line 61

def create_scope(conditional)
  Scope.new self, conditional
end

#dead?true, false

A scope is considered dead once its conditional returns true, or if the parent scope is also dead.

Returns:

  • (true)

    if the conditional is met.

  • (false)

    otherwise.



23
24
25
# File 'lib/vissen/parameterized/scope.rb', line 23

def dead?
  @conditional.met? || parent.dead?
end

#include?(obj) ⇒ true, false Also known as: ===

Checks if the given object is included, either in this scope or in any of the parent scopes.

Parameters:

  • obj (#scope)

    the object to scope check.

Returns:

  • (true)

    if the object either shares this scope or a parent scope.

  • (false)

    otherwise.



50
51
52
# File 'lib/vissen/parameterized/scope.rb', line 50

def include?(obj)
  include_scope? obj.scope
end

#include_scope?(other) ⇒ true

Checks if the given scope is included in the scope hierarchy of this one.

Parameters:

  • other (Scope, Object)

    the scope to check.

Returns:

  • (true)

    if the given scope is equal to this one, or one of the parents.



71
72
73
# File 'lib/vissen/parameterized/scope.rb', line 71

def include_scope?(other)
  equal?(other) || @parent.include_scope?(other)
end

#kill!nil

Forces the conditional to return true, irregardless of its actual state.

Returns:

  • (nil)


40
41
42
# File 'lib/vissen/parameterized/scope.rb', line 40

def kill!
  @conditional.force!
end