3 #The module where all the Ruby-to-JavaScript conversion takes place. It is included by all the classes in the YM4R library.
\r
5 #The name of the variable in JavaScript space.
\r
6 attr_reader :variable
\r
8 #Creates javascript code for missing methods + takes care of listeners
\r
9 def method_missing(name,*args)
\r
10 str_name = name.to_s
\r
11 if str_name =~ /^on_(.*)/
\r
13 raise ArgumentError("Only 1 argument is allowed on on_ methods");
\r
15 Variable.new("GEvent.addListener(#{to_javascript},\"#{MappingObject.javascriptify_method($1)}\",#{args[0]})")
\r
18 args.collect! do |arg|
\r
19 MappingObject.javascriptify_variable(arg)
\r
21 Variable.new("#{to_javascript}.#{MappingObject.javascriptify_method(str_name)}(#{args.join(",")})")
\r
25 #Creates javascript code for array or hash indexing
\r
26 def [](index) #index could be an integer or string
\r
27 return Variable.new("#{to_javascript}[#{MappingObject.javascriptify_variable(index)}]")
\r
30 #Transforms a Ruby object into a JavaScript string : MAppingObject, String, Array, Hash and general case (using to_s)
\r
31 def self.javascriptify_variable(arg)
\r
32 if arg.is_a?(MappingObject)
\r
34 elsif arg.is_a?(String)
\r
35 "\"#{MappingObject.escape_javascript(arg)}\""
\r
36 elsif arg.is_a?(Array)
\r
37 "[" + arg.collect{ |a| MappingObject.javascriptify_variable(a)}.join(",") + "]"
\r
38 elsif arg.is_a?(Hash)
\r
39 "{" + arg.to_a.collect do |v|
\r
40 "#{MappingObject.javascriptify_method(v[0].to_s)} : #{MappingObject.javascriptify_variable(v[1])}"
\r
49 #Escape string to be used in JavaScript. Lifted from rails.
\r
50 def self.escape_javascript(javascript)
\r
51 javascript.gsub(/\r\n|\n|\r/, "\\n").gsub("\"") { |m| "\\#{m}" }
\r
54 #Transform a ruby-type method name (like add_overlay) to a JavaScript-style one (like addOverlay).
\r
55 def self.javascriptify_method(method_name)
\r
56 method_name.gsub(/_(\w)/){|s| $1.upcase}
\r
59 #Declares a Mapping Object bound to a JavaScript variable of name +variable+.
\r
60 def declare(variable)
\r
61 @variable = variable
\r
62 "var #{@variable} = #{create};"
\r
65 #declare with a random variable name
\r
66 def declare_random(init,size = 8)
\r
68 6.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
\r
72 #Checks if the MappinObject has been declared
\r
77 #Binds a Mapping object to a previously declared JavaScript variable of name +variable+.
\r
78 def assign_to(variable)
\r
79 @variable = variable
\r
80 "#{@variable} = #{create};"
\r
83 #Assign the +value+ to the +property+ of the MappingObject
\r
84 def set_property(property, value)
\r
85 "#{to_javascript}.#{MappingObject.javascriptify_method(property.to_s)} = #{MappingObject.javascriptify_variable(value)}"
\r
88 #Returns the code to get a +property+ from the MappingObject
\r
89 def get_property(property)
\r
90 Variable.new("#{to_javascript}.#{MappingObject.javascriptify_method(property.to_s)}")
\r
93 #Returns a Javascript code representing the object
\r
95 unless @variable.nil?
\r
102 #Creates a Mapping Object in JavaScript.
\r
103 #To be implemented by subclasses if needed
\r
108 #Used to bind a ruby variable to an already existing JavaScript one. It doesn't have to be a variable in the sense "var variable" but it can be any valid JavaScript expression that has a value.
\r
110 include MappingObject
\r
112 def initialize(variable)
\r
113 @variable = variable
\r
115 #Returns the javascript expression contained in the object.
\r
119 #Returns the expression inside the Variable followed by a ";"
\r
124 UNDEFINED = Variable.new("undefined")
\r