Class: Native::Object

Inherits:
BasicObject
Includes:
Native
Defined in:
opal/stdlib/native.rb

Instance Method Summary collapse

Methods included from Native

call, convert, included, #initialize, is_a?, #to_n, try_convert

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



203
204
205
206
207
208
209
210
211
212
# File 'opal/stdlib/native.rb', line 203

def method_missing(mid, *args, &block)
  %x{
    if (mid.charAt(mid.length - 1) === '=') {
      return #{self[mid.slice(0, mid.length - 1)] = args[0]};
    }
    else {
      return #{::Native.call(@native, mid, *args, &block)};
    }
  }
end

Instance Method Details

#==(other) ⇒ Object



154
155
156
# File 'opal/stdlib/native.rb', line 154

def ==(other)
  `#@native === #{Native.try_convert(other)}`
end

#[](key) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'opal/stdlib/native.rb', line 180

def [](key)
  %x{
    var prop = #@native[key];

    if (prop instanceof Function) {
      return prop;
    }
    else {
      return #{::Native.call(@native, key)}
    }
  }
end

#[]=(key, value) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'opal/stdlib/native.rb', line 193

def []=(key, value)
  native = Native.try_convert(value)

  if `#{native} === nil`
    `#@native[key] = #{value}`
  else
    `#@native[key] = #{native}`
  end
end

#classObject



228
229
230
# File 'opal/stdlib/native.rb', line 228

def class
  `self._klass`
end

#each(*args) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'opal/stdlib/native.rb', line 166

def each(*args)
  if block_given?
    %x{
      for (var key in #@native) {
        #{yield `key`, `#@native[key]`}
      }
    }

    self
  else
    method_missing(:each, *args)
  end
end

#has_key?(name) ⇒ Boolean Also known as: key?, include?, member?

Returns:



158
159
160
# File 'opal/stdlib/native.rb', line 158

def has_key?(name)
  `#@native.hasOwnProperty(#{name})`
end

#inspectObject



240
241
242
# File 'opal/stdlib/native.rb', line 240

def inspect
  "#<Native:#{`String(#@native)`}>"
end

#instance_of?(klass) ⇒ Boolean

Returns:



224
225
226
# File 'opal/stdlib/native.rb', line 224

def instance_of?(klass)
  klass == Native
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Returns:



218
219
220
# File 'opal/stdlib/native.rb', line 218

def is_a?(klass)
  klass == Native
end

#nil?Boolean

Returns:



214
215
216
# File 'opal/stdlib/native.rb', line 214

def nil?
  false
end

#to_a(options = {}, &block) ⇒ Object



232
233
234
# File 'opal/stdlib/native.rb', line 232

def to_a(options = {}, &block)
  Native::Array.new(@native, options, &block).to_a
end

#to_ary(options = {}, &block) ⇒ Object



236
237
238
# File 'opal/stdlib/native.rb', line 236

def to_ary(options = {}, &block)
  Native::Array.new(@native, options, &block)
end