Class: Spade::MainContext

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

Overview

The primary context created when running spade exec or spade console. This context will also automatically start a reactor loop.

Instance Attribute Summary collapse

Attributes inherited from Context

#reactor, #verbose

Instance Method Summary collapse

Methods inherited from Context

#require

Constructor Details

#initialize(opts = {}) ⇒ MainContext

Load the spade and racer-loader.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/spade/context.rb', line 66

def initialize(opts={})
  env = opts[:env] || ENV
  @rootdir = opts[:rootdir] || opts['rootdir']
  @caller_id = opts[:caller_id] || opts['caller_id']
  @reactor = opts[:reactor] ||= Reactor.new(self)
  lang = opts[:language] ||= (env['LANG']||'en_US').gsub(/\..*/, '')
  lang = lang.gsub '_', '-'


  super(opts) do |ctx|
    ctx['ENV'] = env.to_hash
    ctx['ENV']['SPADE_PLATFORM'] = { 'ENGINE'   => 'spade-ruby' }
    ctx['ENV']['LANG'] = lang

    ctx['ARGV'] = opts[:argv] || ARGV

    # Load spade and patch in evaluator and loader plugins
    ctx.load(Spade.jspath)
    ctx['rubyLoader'] = Loader.new(self)
    ctx['rubyEvaluator'] = Evaluator.new(self)

    ctx.eval %[
      spade.loader = rubyLoader;
      spade.evaluator = rubyEvaluator;
      spade.defaultSandbox.rootdir = #{@rootdir.to_json};
      spade.globalize();
    ]

    ctx.eval("spade.defaultSandbox.callerId = #{@caller_id.to_json};") if @caller_id

    ctx['rubyLoader'] = ctx['rubyEvaluator'] = nil

    @reactor.start do
      yield(self) if block_given?
    end

  end
end

Instance Attribute Details

#caller_idObject

Returns the value of attribute caller_id.



63
64
65
# File 'lib/spade/context.rb', line 63

def caller_id
  @caller_id
end

#rootdirObject

Returns the value of attribute rootdir.



62
63
64
# File 'lib/spade/context.rb', line 62

def rootdir
  @rootdir
end