Module: V8::To

Defined in:
lib/v8/to.rb

Class Method Summary collapse

Class Method Details

.camel_case(str) ⇒ Object



73
74
75
# File 'lib/v8/to.rb', line 73

def camel_case(str)
  str.to_s.gsub(/_(\w)/) {$1.upcase}
end

.peer(value) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/v8/to.rb', line 64

def peer(value)
  external = value.GetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyObject"))
  if external && !external.IsEmpty()
    external.Value()
  else
    yield.new(value)
  end
end

.perl_case(str) ⇒ Object



77
78
79
# File 'lib/v8/to.rb', line 77

def perl_case(str)
  str.gsub(/([A-Z])([a-z])/) {"_#{$1.downcase}#{$2}"}.downcase
end

.rb(value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/v8/to.rb', line 6

def rb(value)
  case value
  when V8::C::Function  then peer(value) {V8::Function}
  when V8::C::Array     then peer(value) {V8::Array}
  when V8::C::Object    then peer(value) {V8::Object}
  when V8::C::String    then value.Utf8Value()
  when V8::C::Date      then Time.at(value.NumberValue())
  when V8::C::Value     then nil if value.IsEmpty()
  else
    value
  end
end

.v8(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/v8/to.rb', line 19

def v8(value)
  case value
  when V8::Object
    value.instance_eval {@native}
  when String
    C::String::New(value.to_s)
  when Symbol
    C::String::NewSymbol(value.to_s)
  when Proc,Method
    template = C::FunctionTemplate::New() do |arguments|
      rbargs = []
      for i in 0..arguments.Length() - 1
        rbargs << To.rb(arguments[i])
      end
      V8::Function.rubycall(value, *rbargs)
    end
    return template.GetFunction()
  when ::Array
    C::Array::New(value.length).tap do |a|
      value.each_with_index do |item, i|
        a.Set(i, To.v8(item))
      end
    end
  when ::Hash
    C::Object::New().tap do |o|
      value.each do |key, value|
        o.Set(To.v8(key), To.v8(value))
      end
    end
  when ::Time
    C::Date::New(value)
  when ::Class
    Constructors[value].GetFunction().tap do |f|
      f.SetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyObject"), C::External::New(value))
    end
  when nil,Numeric,TrueClass,FalseClass, C::Value
    value
  else
    args = C::Array::New(1)
    args.Set(0, C::External::New(value))
    obj = Access[value.class].GetFunction().NewInstance(args)
    return obj
  end
end