Class: Js2json::V8Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/js2json/v8_converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ V8Converter

Returns a new instance of V8Converter.



3
4
5
# File 'lib/js2json/v8_converter.rb', line 3

def initialize(options = {})
  @options = options
end

Instance Method Details

#convert_to_ruby(obj) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/js2json/v8_converter.rb', line 7

def convert_to_ruby(obj)
  case obj
  when V8::Array
    obj.map {|i| convert_to_ruby(i) }
  when V8::Function
    convert_to_ruby(obj.to_s)
  when V8::Object
    Hash[*obj.map {|k, v|
      key_conv = @options[:key_conv]
      k = convert_to_ruby(k)
      k = key_conv.call(k) if key_conv
      [k, convert_to_ruby(v)]
    }.flatten]
  else
    conv = @options[:conv]
    conv ? conv.call(obj) : obj
  end
end