Class: Isorun::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/isorun/context.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(options = default_options) {|context| ... } ⇒ Object

Creates a new context and yields the context as the first argument to the block.

Examples:

Creates a new context, imports the default as function

result = Isorun::Context.create do |context|
  func = context.import.from(module_path)
  func.call("Hello, World!")
end

Yields:

  • (context)

    The newly created JavaScript context

Yield Parameters:

Yield Returns:

  • (Object, nil)

    An optional return value from the execution context



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/isorun/context.rb', line 75

def create(options = default_options, &block)
  raise "[Isorun::Context] block missing when creating context" unless block

  context = Isorun::Context.new

  context.receiver = options[:receiver] if options[:receiver].present?

  result = yield(context)

  context.receiver = nil if options[:receiver].present?

  result
end

Instance Method Details

#import(*export_names) ⇒ Object

Specify items you want to import from the module. If none is specified, the default export is taken.

Examples:

Import default export

result = Isorun::Context.create do |context|
  item = context.import.from(module_path)
end

Import default export explicitly

result = Isorun::Context.create do |context|
  item = context.import(:default).from(module_path)
end

Import various exports

result = Isorun::Context.create do |context|
  hello, world = context.import(:hello, :world).from(module_path)
end


118
119
120
121
122
# File 'lib/isorun/context.rb', line 118

def import(*export_names)
  export_names = [*export_names].map(&:to_s)
  export_names = [:default.to_s] if export_names.empty?
  Import.new(self, export_names)
end

#receiver=(receiver) ⇒ Isorun::Context

Returns the newly created context.

Parameters:

  • receiver (Proc, nil)

Returns:



# File 'lib/isorun/context.rb', line 124