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.



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
104
105
106
107
108
109
# File 'lib/spade/context.rb', line 72

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' }
    ctx['ENV']['LANG'] = lang
     
    ctx['ARGV'] = opts[:argv] || ARGV
    
    # Load spade and patch in compiler and loader plugins
    ctx.load(SPADEJS_PATH)
    ctx['rubyLoader'] = Loader.new(self)
    ctx['rubyCompiler'] = Compiler.new(self)
    
    ctx.eval %[
      spade.loader = rubyLoader;
      spade.compiler = rubyCompiler;
      spade.defaultSandbox.rootdir = #{@rootdir.to_json};
      spade.globalize();
    ]

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

    ctx['rubyLoader'] = ctx['rubyCompiler'] = nil
    
    @reactor.start do
      yield(self) if block_given?
    end
    
  end
end

Instance Attribute Details

#caller_idObject

Returns the value of attribute caller_id.



69
70
71
# File 'lib/spade/context.rb', line 69

def caller_id
  @caller_id
end

#rootdirObject

Returns the value of attribute rootdir.



68
69
70
# File 'lib/spade/context.rb', line 68

def rootdir
  @rootdir
end