Class: InlineJavaScript
- Inherits:
-
Object
- Object
- InlineJavaScript
- Defined in:
- lib/inline_java_script.rb,
ext/inline_java_script_v8_wrapper/inline_java_script_v8_wrapper.cc
Defined Under Namespace
Classes: V8Wrapper
Constant Summary collapse
- VERSION =
'0.1'
Instance Method Summary collapse
-
#initialize(javascript_functions) ⇒ InlineJavaScript
constructor
Takes a string containing some JavaScript and executes it.
-
#method_missing(method, *arguments, &unused_block) ⇒ Object
Attempts to call whatever JavaScript function you defined in the constructor.
Constructor Details
#initialize(javascript_functions) ⇒ InlineJavaScript
Takes a string containing some JavaScript and executes it. The state is maintained. JavaScript functions defined in the string can be called directly from an instance of InlineJavaScript as if they were methods.
25 26 27 28 29 30 |
# File 'lib/inline_java_script.rb', line 25 def initialize javascript_functions require File.join(File.dirname(__FILE__), %w{.. ext inline_java_script_v8_wrapper inline_java_script_v8_wrapper}) @wrapper = InlineJavaScript::V8Wrapper.new @wrapper.execute(javascript_functions) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments, &unused_block) ⇒ Object
Attempts to call whatever JavaScript function you defined in the constructor. It will accept any number of parameters of String, Fixnum, Array or Hash and can support returning the same from JavaScript.
36 37 38 39 40 41 42 |
# File 'lib/inline_java_script.rb', line 36 def method_missing(method, *arguments, &unused_block) json_arguments = arguments.map do |arg| "JSON.parse(" + arg.to_json.to_json + ")" end JSON[@wrapper.execute("JSON.stringify({result: #{method}(#{json_arguments.join(',')})})")]['result'] end |