Module: GoodGuide::Gibbon::Util

Extended by:
Util
Included in:
Context, RuntimeClient, StaticClient, Util
Defined in:
lib/goodguide/gibbon.rb

Instance Method Summary collapse

Instance Method Details

#hash_to_js(ruby_hash) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/goodguide/gibbon.rb', line 22

def hash_to_js(ruby_hash)
  js_hash = gibbon[:Hash].new
  ruby_hash.each do |k, v|
    js_hash.set(k, v)
  end
  js_hash
end

#hash_to_ruby(js_hash) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/goodguide/gibbon.rb', line 9

def hash_to_ruby(js_hash)
  return nil unless js_hash

  ruby_hash = {}
  iterator = lambda { |this, k, v|
    ruby_hash[k] = obj_to_ruby(v)
  }

  js_hash[:each].methodcall js_hash, iterator

  ruby_hash
end

#obj_to_ruby(o) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/goodguide/gibbon.rb', line 30

def obj_to_ruby(o)
  case o
  when V8::Array
    o.map { |x| obj_to_ruby(x) }
  when V8::Object
    o = o.asJSON if o.respond_to? :asJSON

    out = {}
    o.each do |k, v|
      out[k] = obj_to_ruby(v)
    end
    out
  else
    o
  end
end