Class: Java::OrgMozillaJavascript::ScriptableObject
- Defined in:
- lib/rhino/rhino_ext.rb
Overview
The base class for all JavaScript objects.
Instance Method Summary collapse
-
#[](name) ⇒ Object
get a property from this javascript object, where
k
is a string or symbol corresponding to the property name e.g. -
#[]=(key, value) ⇒ Object
set a property on the javascript object, where
k
is a string or symbol corresponding to the property name, andv
is the value to set. -
#each ⇒ Object
enumerate the key value pairs contained in this javascript object.
- #each_key ⇒ Object
- #each_raw ⇒ Object
- #each_value ⇒ Object
- #keys ⇒ Object
-
#method_missing(name, *args) ⇒ Object
Delegate methods to JS object if possible when called from Ruby.
-
#to_h ⇒ Object
Converts the native object to a hash.
-
#to_json(*args) ⇒ Object
Convert this javascript object into a json string.
- #values ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Delegate methods to JS object if possible when called from Ruby.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rhino/rhino_ext.rb', line 89 def method_missing(name, *args) name_str = name.to_s if name_str[-1, 1] == '=' && args.size == 1 # writer -> JS put self[ name_str[0...-1] ] = args[0] else if property = self[name_str] if property.is_a?(Rhino::JS::Function) begin context = Rhino::JS::Context.enter scope = current_scope(context) js_args = Rhino.args_to_javascript(args, self) # scope == self Rhino.to_ruby property.__call__(context, scope, self, js_args) ensure Rhino::JS::Context.exit end else if args.size > 0 raise ArgumentError, "can't call '#{name_str}' with args: #{args.inspect} as it's a property" end Rhino.to_ruby property end else super end end end |
Instance Method Details
#[](name) ⇒ Object
get a property from this javascript object, where k
is a string or symbol corresponding to the property name e.g.
jsobject = Context.open do |cxt|
cxt.eval('({foo: 'bar', 'Take me to': 'a funky town'})')
end
jsobject[:foo] # => 'bar'
jsobject['foo'] # => 'bar'
jsobject['Take me to'] # => 'a funky town'
17 18 19 |
# File 'lib/rhino/rhino_ext.rb', line 17 def [](name) Rhino.to_ruby ScriptableObject.getProperty(self, name.to_s) end |
#[]=(key, value) ⇒ Object
set a property on the javascript object, where k
is a string or symbol corresponding to the property name, and v
is the value to set. e.g.
jsobject = eval_js "new Object()"
jsobject['foo'] = 'bar'
Context.open(:with => jsobject) do |cxt|
cxt.eval('foo') # => 'bar'
end
30 31 32 33 |
# File 'lib/rhino/rhino_ext.rb', line 30 def []=(key, value) scope = self ScriptableObject.putProperty(self, key.to_s, Rhino.to_javascript(value, scope)) end |
#each ⇒ Object
enumerate the key value pairs contained in this javascript object. e.g.
eval_js("{foo: 'bar', baz: 'bang'}").each do |key,value|
puts "#{key} -> #{value} "
end
outputs foo -> bar baz -> bang
43 44 45 |
# File 'lib/rhino/rhino_ext.rb', line 43 def each each_raw { |key, val| yield key, Rhino.to_ruby(val) } end |
#each_key ⇒ Object
47 48 49 |
# File 'lib/rhino/rhino_ext.rb', line 47 def each_key each_raw { |key, val| yield key } end |
#each_raw ⇒ Object
55 56 57 58 59 |
# File 'lib/rhino/rhino_ext.rb', line 55 def each_raw for id in getAllIds do yield id, get(id, self) end end |
#each_value ⇒ Object
51 52 53 |
# File 'lib/rhino/rhino_ext.rb', line 51 def each_value each_raw { |key, val| yield Rhino.to_ruby(val) } end |
#keys ⇒ Object
61 62 63 64 65 |
# File 'lib/rhino/rhino_ext.rb', line 61 def keys keys = [] each_key { |key| keys << key } keys end |
#to_h ⇒ Object
Converts the native object to a hash. This isn’t really a stretch since it’s pretty much a hash in the first place.
75 76 77 78 79 80 81 |
# File 'lib/rhino/rhino_ext.rb', line 75 def to_h hash = {} each do |key, val| hash[key] = val.is_a?(ScriptableObject) ? val.to_h : val end hash end |
#to_json(*args) ⇒ Object
Convert this javascript object into a json string.
84 85 86 |
# File 'lib/rhino/rhino_ext.rb', line 84 def to_json(*args) to_h.to_json(*args) end |
#values ⇒ Object
67 68 69 70 71 |
# File 'lib/rhino/rhino_ext.rb', line 67 def values vals = [] each_value { |val| vals << val } vals end |