Class: V8::Object

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/v8/object.rb

Direct Known Subclasses

Array, Function

Instance Method Summary collapse

Constructor Details

#initialize(native, portal) ⇒ Object

Returns a new instance of Object.



6
7
8
# File 'lib/v8/object.rb', line 6

def initialize(native, portal)
  @native, @portal = native, portal
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(name, *args, &block)
  if name.to_s =~ /(.*)=$/
    if args.length > 1
      self[$1] = args
      return args
    else
      self[$1] = args.first
      return args
    end
  end
  return super(name, *args, &block) unless self.respond_to?(name)
  property = self[name]
  if property.kind_of?(V8::Function)
    property.methodcall(self, *args)
  elsif args.empty?
    property
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 0)" unless args.empty?
  end
end

Instance Method Details

#[](key) ⇒ Object



10
11
12
13
14
# File 'lib/v8/object.rb', line 10

def [](key)
  @portal.open do |to|
    to.rb(@native.Get(to.v8(key)))
  end
end

#[]=(key, value) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/v8/object.rb', line 16

def []=(key, value)
  value.tap do
    @portal.open do |to|
      @native.Set(to.v8(key), to.v8(value))
    end
  end
end

#eachObject



30
31
32
33
34
35
36
# File 'lib/v8/object.rb', line 30

def each
  @portal.open do |to|
    for prop in to.rb(@native.GetPropertyNames())
      yield prop, self[prop]
    end
  end
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/v8/object.rb', line 38

def respond_to?(method)
  super or self[method] != nil
end

#to_sObject



24
25
26
27
28
# File 'lib/v8/object.rb', line 24

def to_s
  @portal.open do |to|
    to.rb(@native.ToString())
  end
end