Module: TT::Javascript

Defined in:
TT_Lib2/javascript.rb

Overview

Javascript helper module.

Since:

  • 2.5.0

Class Method Summary collapse

Class Method Details

.to_js(object, format = false) ⇒ String

Parameters:

  • object (Object)

Returns:

  • (String)

Since:

  • 2.5.0



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'TT_Lib2/javascript.rb', line 22

def self.to_js(object, format=false)
  if object.is_a?( TT::JSON )
    object.to_s(format)
  elsif object.is_a?( Hash )
    TT::JSON.new(object).to_s(format)
  elsif object.is_a?( Symbol ) # 2.5.0
    object.inspect.inspect
  elsif object.nil?
    'null'
  elsif object.is_a?( Array ) # 2.7.0
    data = object.map { |x| self.to_js(x, format) }
    "[#{data.join(',')}]"
  elsif object.is_a?( Geom::Point3d ) # 2.7.0
    "new Point3d( #{object.to_a.join(', ')} )"
  elsif object.is_a?( Geom::Vector3d ) # 2.7.0
    "new Vector3d( #{object.to_a.join(', ')} )"
  else
    # (!) Filter out accepted objects.
    # (!) Convert unknown into strings - then inspect.
    object.inspect
  end
end