Class: QML::JSObject

Inherits:
Object
  • Object
show all
Defined in:
lib/qml/js_object.rb,
ext/qml/js_object.c

Overview

The JSObject represents JavaScript objects.

Direct Known Subclasses

JSFunction, JSWrapper

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Gets or sets a JS property, or call it as a method if it is a function.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/qml/js_object.rb', line 41

def method_missing(method, *args, &block)
  if method[-1] == '='
    # setter
    key = method.slice(0...-1).to_sym

    unless has_key?(key)
      super
    end
    self[key] = args[0]
  else
    unless has_key?(method)
      super
    end

    prop = self[method]
    if prop.is_a? JSFunction
      prop.call_with_instance(self, *args, &block)
    else
      prop
    end
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Compares two JS objects by JavaScript identity (===).

Returns:

  • (Boolean)


261
262
263
264
265
266
267
268
269
270
271
# File 'ext/qml/js_object.c', line 261

static VALUE js_object_equal_p(VALUE self, VALUE other)
{
    qmlbind_value obj = rbqml_js_object_get(self);
    qmlbind_value otherObj = rbqml_js_object_get(other);

    if (qmlbind_value_is_identical(obj, otherObj)) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}

#[](key) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'ext/qml/js_object.c', line 144

static VALUE js_object_aref(VALUE self, VALUE key)
{
    qmlbind_value obj = rbqml_js_object_get(self);

    int index = -1;
    const char *keyStr;
    get_property_key(key, &index, &keyStr);

    qmlbind_value value;

    if (index >= 0) {
        value = rbqml_get_array_item(obj, index);
    } else {
        value = rbqml_get_property(obj, keyStr);
    }

    qmlbind_value_release(obj);

    return rb_ensure(&rbqml_to_ruby, (VALUE)value, (VALUE (*)())&qmlbind_value_release, (VALUE)value);
}

#[]=(key, value) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'ext/qml/js_object.c', line 165

static VALUE js_object_aset(VALUE self, VALUE key, VALUE value)
{
    qmlbind_value obj = rbqml_js_object_get(self);

    qmlbind_value qmlValue = rbqml_to_qml(value);

    int index = -1;
    const char *keyStr;

    get_property_key(key, &index, &keyStr);

    if (index >= 0) {
        rbqml_set_array_item(obj, index, qmlValue);
    } else {
        rbqml_set_property(obj, keyStr, qmlValue);
    }

    qmlbind_value_release(obj);

    return value;
}

#each_pairObject Also known as: each



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'ext/qml/js_object.c', line 207

static VALUE js_object_each_pair(VALUE self)
{
    RETURN_ENUMERATOR(self, 0, 0);

    qmlbind_value obj = rbqml_js_object_get(self);

    qmlbind_iterator it = qmlbind_iterator_new(obj);
    rb_ensure(&js_object_each_iterator, (VALUE)it, (VALUE (*)())&qmlbind_iterator_release, (VALUE)it);

    qmlbind_value_release(obj);

    return self;
}

#error?Boolean

Returns:

  • (Boolean)


247
248
249
250
251
252
253
254
255
# File 'ext/qml/js_object.c', line 247

static VALUE js_object_error_p(VALUE self)
{
    qmlbind_value obj = rbqml_js_object_get(self);
    if (qmlbind_value_is_error(obj)) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'ext/qml/js_object.c', line 221

static VALUE js_object_has_key_p(VALUE self, VALUE key)
{
    qmlbind_value obj = rbqml_js_object_get(self);

    int index = -1;
    const char *keyStr;

    get_property_key(key, &index, &keyStr);

    int ret;

    if (index >= 0) {
        ret = qmlbind_value_has_index(obj, index);
    } else {
        ret = qmlbind_value_has_property(obj, keyStr);
    }

    qmlbind_value_release(obj);

    if (ret) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}

#keysArray<String>

Returns:



8
9
10
# File 'lib/qml/js_object.rb', line 8

def keys
  each.map { |k, v| k }
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/qml/js_object.rb', line 36

def respond_to?(method)
  has_key?(method) || super
end

#to_errorQML::QMLError

Returns:



32
33
34
# File 'lib/qml/js_object.rb', line 32

def to_error
  QMLError.new(self['message'])
end

#to_hashHash

Returns:



18
19
20
21
22
23
24
# File 'lib/qml/js_object.rb', line 18

def to_hash
  {}.tap do |hash|
    each do |k, v|
      hash[k] =v
    end
  end
end

#to_qmlQML::JSObject

Returns self.

Returns:



65
66
67
# File 'lib/qml/js_object.rb', line 65

def to_qml
  self
end

#to_timeTime

Returns:



27
28
29
# File 'lib/qml/js_object.rb', line 27

def to_time
  Time.at(getTime.to_i / 1000r).getlocal(-getTimezoneOffset * 60)
end

#valuesArray

Returns:



13
14
15
# File 'lib/qml/js_object.rb', line 13

def values
  each.map { |k, v| v }
end