Class: RubyAwaitNodejs::Context
- Inherits:
-
Object
- Object
- RubyAwaitNodejs::Context
- Defined in:
- lib/ruby-await-nodejs/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 RubyAwaitNodejs 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 RubyAwaitNodejs src is the js code to be eval()‘d in the nodejs context
11 12 13 14 15 16 17 18 19 |
# File 'lib/ruby-await-nodejs/context.rb', line 11 def initialize(runtime, src = "", = {}) @runtime = runtime @execution_timeout = [:execution_timeout] || 60 # seconds # 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
53 54 55 |
# File 'lib/ruby-await-nodejs/context.rb', line 53 def call(identifier, *args) eval "#{identifier}.apply(this, #{::JSON.generate(args)})" end |
#eval(src) ⇒ Object
21 22 23 24 25 |
# File 'lib/ruby-await-nodejs/context.rb', line 21 def eval(src) if /\S/ =~ src #IMPORTANT! /\S/ =~ "()" => 0 exec(src) end end |
#exec(src) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ruby-await-nodejs/context.rb', line 31 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
27 28 29 |
# File 'lib/ruby-await-nodejs/context.rb', line 27 def load(path) exec("require(#{path.to_json})") end |