Class: RubyAwaitNode::Context
- Inherits:
-
Object
- Object
- RubyAwaitNode::Context
- Defined in:
- lib/ruby-await-node/context.rb
Instance Method Summary collapse
- #call(identifier, *args) ⇒ Object
- #eval(src) ⇒ Object
- #exec(src) ⇒ Object
-
#initialize(runtime, src = "", options = {}) ⇒ Context
constructor
runtime is an instance of RubyAwaitNode src is the js code to be eval()‘d in the nodejs context.
- #load(path) ⇒ Object
Constructor Details
#initialize(runtime, src = "", options = {}) ⇒ Context
runtime is an instance of RubyAwaitNode src is the js code to be eval()‘d in the nodejs context
11 12 13 14 15 16 17 18 |
# File 'lib/ruby-await-node/context.rb', line 11 def initialize(runtime, src = "", = {}) @runtime = runtime # compile context source, in most cases # this is something like the CoffeeScript compiler # or the SASS compiler eval(src) end |
Instance Method Details
#call(identifier, *args) ⇒ Object
52 53 54 |
# File 'lib/ruby-await-node/context.rb', line 52 def call(identifier, *args) eval "#{identifier}.apply(this, #{::JSON.generate(args)})" end |
#eval(src) ⇒ Object
20 21 22 23 24 |
# File 'lib/ruby-await-node/context.rb', line 20 def eval(src) if /\S/ =~ src #IMPORTANT! /\S/ =~ "()" => 0 exec(src) end end |
#exec(src) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ruby-await-node/context.rb', line 30 def exec(src) return "" unless src.length > 0 src = src.encode('UTF-8', :undef => :replace, :replace => '') src = compile_source(src) # src is either an empty object # OR a valid JSON string in the form # ['ok', 'result-of-coffeescript-or-sass-compiler'] # OR if an error occured # ['err', 'some sort of error to be presented to the developer as a sprockets error'] # status, value = src.empty? ? [] : ::JSON.parse(src, create_additions: false) if status == "ok" value elsif value =~ /SyntaxError:/ raise RuntimeError, value else raise ProgramError, value end end |
#load(path) ⇒ Object
26 27 28 |
# File 'lib/ruby-await-node/context.rb', line 26 def load(path) exec("require(#{path.to_json})") end |