Class: Sevgi::Executor::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/sevgi/executor/scope.rb

Overview

Holds one isolated Sevgi script execution scope and its result.

Constant Summary collapse

Error =

Error raised by low-level executor scope operations.

Class.new(::Sevgi::Error)
MAIN_MODULE =

Constant name used for the temporary script module under Sevgi.

:Main

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope = nil) ⇒ void

Creates a script execution scope.

Parameters:

  • scope (Module, nil) (defaults to: nil)

    existing module to evaluate source in



21
22
23
24
25
26
# File 'lib/sevgi/executor/scope.rb', line 21

def initialize(scope = nil)
  @scope = scope || main
  @recent = nil
  @error = nil
  @stack = {}
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



16
# File 'lib/sevgi/executor/scope.rb', line 16

attr_reader :scope, :recent, :error

#recentObject? (readonly)

Returns last expression result from the executed source.

Returns:

  • (Object, nil)

    last expression result from the executed source



16
# File 'lib/sevgi/executor/scope.rb', line 16

attr_reader :scope, :recent, :error

#scopeModule (readonly)

Returns isolated module where script source is evaluated.

Returns:

  • (Module)

    isolated module where script source is evaluated



16
17
18
# File 'lib/sevgi/executor/scope.rb', line 16

def scope
  @scope
end

Instance Method Details

#call(source, receiver = nil) { ... } ⇒ Sevgi::Executor::Scope

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.

Executes one source object in this scope.

Parameters:

  • source (Sevgi::Executor::Source)

    source to evaluate

  • receiver (Object, nil) (defaults to: nil)

    receiver used while booting the DSL

Yields:

  • optional boot block that installs DSL methods before evaluation

Yield Returns:

  • (void)

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sevgi/executor/scope.rb', line 39

def call(source, receiver = nil, &boot)
  push(source)

  tap do
    @recent = run(source, receiver, &boot)

    # rubocop:disable Lint/RescueException
  rescue Exception => e
    @error = Executor::Error.new(e, self)

    throw(:result, self)
    # rubocop:enable Lint/RescueException
  end
end

#error?Boolean

Reports whether execution captured an error.

Returns:

  • (Boolean)

    true when execution failed



30
# File 'lib/sevgi/executor/scope.rb', line 30

def error? = !error.nil?

#load(file) { ... } ⇒ Sevgi::Executor::Scope

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.

Loads a file into this existing execution scope.

Parameters:

  • file (String)

    source file to read and evaluate

Yields:

  • optional boot block that installs DSL methods before evaluation

Yield Returns:

  • (void)

Returns:

Raises:

  • (Errno::ENOENT)

    when the file cannot be read



61
# File 'lib/sevgi/executor/scope.rb', line 61

def load(file, &block) = call(Source.load(file), &block)

#peekSevgi::Executor::Source?

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 most recently pushed source.

Returns:



70
# File 'lib/sevgi/executor/scope.rb', line 70

def peek = @stack[@stack.keys.last]

#stackArray<String>

Returns the unique source stack for this execution.

Returns:

  • (Array<String>)

    source file keys in load order



65
# File 'lib/sevgi/executor/scope.rb', line 65

def stack = @stack.keys