Class: H8::Value
- Includes:
- Comparable
- Defined in:
- lib/h8/value.rb,
ext/h8/main.cpp
Overview
Wrapper for javascript objects.
Important: when accessing fields of the object, respond_to? will not work due to js notation, instead, check the returned value (there will be always one) to not to be value.undefined?
Precaution! Values are always bounded to the context where they are created. You can not pass values between context, as they often map native Javascript objects. If you need, you can copy, for example, by converting it H8::Value#to_ruby first. Another approach is to use JSON serialization from the script.
Interesting thing about H8::Value is that when passed back to some javascript environment (say, callback parameted or as a global variable), it unwraps source javascript object - no copying, no modifying.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare to other object, either usual or another Value instance.
-
#[](name_index) ⇒ H8::Value
Get js object attribute by either name or index (should be Fixnum instance).
-
#[]=(name_index, value) ⇒ Object
Set js object attribute by either name or index (should be Fixnum instance).
-
#apply(this, *args) ⇒ H8::Value
Like JS apply: call the value that should be function() bounded to a given object.
-
#array? ⇒ Boolean
native method.
-
#call(*args) ⇒ H8::Value
Call javascript function represented by this instance (which should be function()) with given (or no) arguments.
-
#context ⇒ H8::Context
Context to which this value is bounded.
-
#each ⇒ Object
enumerate |key, value| pairs for javascript object attributes.
-
#each_key ⇒ Object
Iterate over javascript object keys.
-
#each_value ⇒ Object
iterate over values of the javascript object attributes.
-
#function? ⇒ Boolean
native method.
- #inspect ⇒ Object
-
#keys ⇒ Object
Generate set of keys of the wrapped object.
-
#method_missing(method_sym, *arguments, &block) ⇒ Object
Optimizing JS member access.
-
#object? ⇒ Boolean
native method.
-
#to_ary ⇒ Object
(also: #to_a)
Tries to convert JS object to ruby array.
-
#to_h(depth = 0) ⇒ Object
Try to convert javascript object to a ruby hash.
-
#to_proc ⇒ Object
Convert to Proc instance so it could be used as &block parameter.
-
#to_ruby(depth = 0) ⇒ Object
Tries to convert wrapped JS object to ruby primitive (Fixed, String, Float, Array, Hash).
- #to_str ⇒ Object
-
#undefined? ⇒ Boolean
native method.
-
#values ⇒ Array
Values array.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
Optimizing JS member access. Support class members and member functions - will call them automatically. Use val to get callable instead.
First invocation creates accessor method so future calls happen much faster
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/h8/value.rb', line 45 def method_missing(method_sym, *arguments, &block) name = method_sym.to_s if name[-1] != '=' instance_eval <<-End def #{name} *args, **kwargs res = _get_attr('#{name}') (res.is_a?(H8::Value) && res.function?) ? res.apply(self,*args) : res end End else instance_eval <<-End def #{name} value _set_attr('#{name[0..-2]}', value) end End end send method_sym, *arguments end |
Instance Method Details
#<=>(other) ⇒ Object
Compare to other object, either usual or another Value instance. Tries its best. be sure to use it wisely and report any problems
66 67 68 69 |
# File 'lib/h8/value.rb', line 66 def <=> other other = other.to_ruby if other.is_a?(H8::Value) to_ruby <=> other end |
#[](name_index) ⇒ H8::Value
Get js object attribute by either name or index (should be Fixnum instance). It always return H8::Value instance, check it to (not) be undefined? to see if there is such attribute
31 32 33 |
# File 'lib/h8/value.rb', line 31 def [] name_index name_index.is_a?(Fixnum) ? _get_index(name_index) : _get_attr(name_index.to_s) end |
#[]=(name_index, value) ⇒ Object
Set js object attribute by either name or index (should be Fixnum instance).
36 37 38 39 |
# File 'lib/h8/value.rb', line 36 def []= name_index, value # name_index.is_a?(Fixnum) ? _get_index(name_index) : _get_attr(name_index.to_s) _set_attr(name_index.to_s, value) end |
#apply(this, *args) ⇒ H8::Value
Like JS apply: call the value that should be function() bounded to a given object
82 83 84 |
# File 'lib/h8/value.rb', line 82 def apply this, *args _apply this, args end |
#array? ⇒ Boolean
native method. stub for documentation
163 164 |
# File 'lib/h8/value.rb', line 163 def array? # native method. stub for documentation end |
#call(*args) ⇒ H8::Value
Call javascript function represented by this instance (which should be function()) with given (or no) arguments.
74 75 76 |
# File 'lib/h8/value.rb', line 74 def call *args _call args end |
#context ⇒ H8::Context
Returns context to which this value is bounded.
167 168 |
# File 'lib/h8/value.rb', line 167 def context # native method. stub for documentation end |
#each ⇒ Object
enumerate |key, value| pairs for javascript object attributes
102 103 104 105 106 107 |
# File 'lib/h8/value.rb', line 102 def each return enum_for(:each) unless block_given? # Sparkling magic! keys.each { |k| yield k, _get_attr(k) } end |
#each_key ⇒ Object
Iterate over javascript object keys
115 116 117 |
# File 'lib/h8/value.rb', line 115 def each_key keys.each end |
#each_value ⇒ Object
iterate over values of the javascript object attributes
125 126 127 |
# File 'lib/h8/value.rb', line 125 def each_value values.each end |
#function? ⇒ Boolean
native method. stub for documentation
154 155 |
# File 'lib/h8/value.rb', line 154 def function? # native method. stub for documentation end |
#inspect ⇒ Object
23 24 25 |
# File 'lib/h8/value.rb', line 23 def inspect "<H8::Value #{to_ruby rescue '(too deep)'}>" end |
#keys ⇒ Object
Generate set of keys of the wrapped object
96 97 98 99 |
# File 'lib/h8/value.rb', line 96 def keys context[:__obj] = self Set.new context.eval("(Object.keys(__obj));").to_a end |
#object? ⇒ Boolean
native method. stub for documentation
157 158 |
# File 'lib/h8/value.rb', line 157 def object? # native method. stub for documentation end |
#to_ary ⇒ Object Also known as: to_a
Tries to convert JS object to ruby array
88 89 90 91 |
# File 'lib/h8/value.rb', line 88 def to_ary raise Error, 'Is not an array' unless array? to_ruby end |
#to_h(depth = 0) ⇒ Object
Try to convert javascript object to a ruby hash
110 111 112 |
# File 'lib/h8/value.rb', line 110 def to_h depth=0 each.reduce({}) { |all, kv| all[kv[0]] = kv[1].to_ruby depth; all } end |
#to_proc ⇒ Object
Convert to Proc instance so it could be used as &block parameter
172 173 174 175 |
# File 'lib/h8/value.rb', line 172 def to_proc function? or raise H8::Error, 'JS object is not a function' -> (*args) { call *args } end |
#to_ruby(depth = 0) ⇒ Object
Tries to convert wrapped JS object to ruby primitive (Fixed, String, Float, Array, Hash). Note that this conversion looses information about source javascript class (if any).
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/h8/value.rb', line 133 def to_ruby depth=0 depth += 1 raise H8::Error, "object tree too deep" if depth > 100 case when integer? to_i when string? to_s when float? to_f when array? _get_attr('length').to_i.times.map { |i| _get_index(i).to_ruby depth } when function? to_proc when object? to_h depth else raise Error, "Dont know how to convert #{self.class}" end end |
#to_str ⇒ Object
177 178 179 |
# File 'lib/h8/value.rb', line 177 def to_str to_s end |
#undefined? ⇒ Boolean
native method. stub for documentation
160 161 |
# File 'lib/h8/value.rb', line 160 def undefined? # native method. stub for documentation end |
#values ⇒ Array
Returns values array. does NOT convert values to_ruby().
120 121 122 |
# File 'lib/h8/value.rb', line 120 def values each.reduce([]) { |all, kv| all << kv[1]; all } end |