Class: Bowline::Desktop::Proxy
Overview
Use to call out to JavaScript.
Use the method ‘call’ if you want to call a function, or the method ‘res’ if you want the result of a variable evaluation.
You can pass a block as the last argument which will be called with the result of the evaluation.
All arguments are serialized by JSON, so you can only pass the following objects:
-
Hash
-
Array
-
String
-
Integer
Examples:
proxy.FooObject. = [1,2,3] #=> "FooObject.messages = [1,2,3]"
proxy.FooObject.hi.call #=> "FooObject.hi()"
proxy.FooObject.hi(1,2,3).bye.call #=> "FooObject.hi(1,2,3).bye()"
proxy.FooObject..res #=> "FooObject.messages"
proxy.FooObject..res {|result|
puts "Messages are: #{result}"
}
Reasoning behind this class’s call/res API:
* JavaScript needs to be called all at once
* We don't know if it's a method call, or a variable
Instance Method Summary collapse
-
#call(method = nil, &block) ⇒ Object
Call a JavaScript function: proxy.myFunction(‘arg1’).call.
-
#initialize(win) ⇒ Proxy
constructor
A new instance of Proxy.
- #inspect ⇒ Object
-
#method_missing(sym, *args) ⇒ Object
:nodoc:.
-
#res(method = nil, &block) ⇒ Object
Evaluate a JavaScript variable: proxy.my_variable.res {|result| p result }.
-
#to_s ⇒ Object
(also: #to_js)
Return the JavaScript that is to be evaluated.
Constructor Details
#initialize(win) ⇒ Proxy
Returns a new instance of Proxy.
31 32 33 34 |
# File 'lib/bowline/desktop/proxy.rb', line 31 def initialize(win) @window = win @crumbs = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
:nodoc:
66 67 68 69 70 71 72 73 |
# File 'lib/bowline/desktop/proxy.rb', line 66 def method_missing(sym, *args) #:nodoc: method_name = sym.to_s @crumbs << [method_name, args] if method_name.last == "=" call end self end |
Instance Method Details
#call(method = nil, &block) ⇒ Object
Call a JavaScript function:
proxy.myFunction('arg1').call
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bowline/desktop/proxy.rb', line 38 def call(method = nil, &block) if @crumbs.empty? raise "No method provided" end string = to_s string << "()" unless string.last == ")" Bowline::Desktop::JS.eval( @window, "Bowline.invokeJS(#{string.inspect});", method, &block ) end |
#inspect ⇒ Object
87 88 89 |
# File 'lib/bowline/desktop/proxy.rb', line 87 def inspect "<#{self.class.name}:#{@window} #{to_s}>" end |
#res(method = nil, &block) ⇒ Object
Evaluate a JavaScript variable:
proxy.my_variable.res {|result| p result }
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bowline/desktop/proxy.rb', line 54 def res(method = nil, &block) if @crumbs.empty? raise "No attribute provided" end Bowline::Desktop::JS.eval( @window, "Bowline.invokeJS(#{to_s.inspect});", method, &block ) end |
#to_s ⇒ Object Also known as: to_js
Return the JavaScript that is to be evaluated
76 77 78 79 80 81 82 83 84 |
# File 'lib/bowline/desktop/proxy.rb', line 76 def to_s (@crumbs || []).inject([]) do |arr, (method, args)| str = method if args.any? str << "(" + args.to_json[1..-2] + ")" end arr << str end.join(".") end |