Module Ym4r::GmPlugin::MappingObject
In: lib/gm_plugin/mapping.rb

The module where all the Ruby-to-JavaScript conversion takes place. It is included by all the classes in the YM4R library.

Methods

Attributes

variable  [R]  The name of the variable in JavaScript space.

Public Class methods

Escape string to be used in JavaScript. Lifted from rails.

[Source]

    # File lib/gm_plugin/mapping.rb, line 50
50:       def self.escape_javascript(javascript)
51:         javascript.gsub(/\r\n|\n|\r/, "\\n").gsub("\"") { |m| "\\#{m}" }
52:       end

Transform a ruby-type method name (like add_overlay) to a JavaScript-style one (like addOverlay).

[Source]

    # File lib/gm_plugin/mapping.rb, line 55
55:       def self.javascriptify_method(method_name)
56:         method_name.gsub(/_(\w)/){|s| $1.upcase}
57:       end

Transforms a Ruby object into a JavaScript string : MAppingObject, String, Array, Hash and general case (using to_s)

[Source]

    # File lib/gm_plugin/mapping.rb, line 31
31:       def self.javascriptify_variable(arg)
32:         if arg.is_a?(MappingObject)
33:           arg.to_javascript
34:         elsif arg.is_a?(String)
35:           "\"#{MappingObject.escape_javascript(arg)}\""
36:         elsif arg.is_a?(Array)
37:           "[" + arg.collect{ |a| MappingObject.javascriptify_variable(a)}.join(",") + "]"
38:         elsif arg.is_a?(Hash)
39:           "{" + arg.to_a.collect do |v|
40:             "#{MappingObject.javascriptify_method(v[0].to_s)} : #{MappingObject.javascriptify_variable(v[1])}"
41:           end.join(",") + "}"
42:         elsif arg.nil?
43:           "undefined"
44:         else
45:           arg.to_s
46:         end
47:       end

Public Instance methods

Creates javascript code for array or hash indexing

[Source]

    # File lib/gm_plugin/mapping.rb, line 26
26:       def [](index) #index could be an integer or string
27:         return Variable.new("#{to_javascript}[#{MappingObject.javascriptify_variable(index)}]")
28:       end

Binds a Mapping object to a previously declared JavaScript variable of name variable.

[Source]

    # File lib/gm_plugin/mapping.rb, line 78
78:       def assign_to(variable)
79:         @variable = variable
80:         "#{@variable} = #{create};"
81:       end

Creates a Mapping Object in JavaScript. To be implemented by subclasses if needed

[Source]

     # File lib/gm_plugin/mapping.rb, line 104
104:       def create
105:       end

Declares a Mapping Object bound to a JavaScript variable of name variable.

[Source]

    # File lib/gm_plugin/mapping.rb, line 60
60:       def declare(variable)
61:         @variable = variable
62:         "var #{@variable} = #{create};"
63:       end

declare with a random variable name

[Source]

    # File lib/gm_plugin/mapping.rb, line 66
66:       def declare_random(init,size = 8)
67:         s = init.clone
68:         6.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
69:         declare(s)
70:       end

Checks if the MappinObject has been declared

[Source]

    # File lib/gm_plugin/mapping.rb, line 73
73:       def declared?
74:         !@variable.nil?
75:       end

Returns the code to get a property from the MappingObject

[Source]

    # File lib/gm_plugin/mapping.rb, line 89
89:       def get_property(property)
90:         Variable.new("#{to_javascript}.#{MappingObject.javascriptify_method(property.to_s)}")
91:       end

Creates javascript code for missing methods + takes care of listeners

[Source]

    # File lib/gm_plugin/mapping.rb, line 9
 9:       def method_missing(name,*args)
10:         str_name = name.to_s
11:         if str_name =~ /^on_(.*)/
12:           if args.length != 1
13:             raise ArgumentError("Only 1 argument is allowed on on_ methods");
14:           else
15:             Variable.new("GEvent.addListener(#{to_javascript},\"#{MappingObject.javascriptify_method($1)}\",#{args[0]})")
16:           end
17:         else
18:           args.collect! do |arg|
19:             MappingObject.javascriptify_variable(arg)
20:           end
21:           Variable.new("#{to_javascript}.#{MappingObject.javascriptify_method(str_name)}(#{args.join(",")})")
22:         end
23:       end

Assign the value to the property of the MappingObject

[Source]

    # File lib/gm_plugin/mapping.rb, line 84
84:       def set_property(property, value)
85:         "#{to_javascript}.#{MappingObject.javascriptify_method(property.to_s)} = #{MappingObject.javascriptify_variable(value)}"
86:       end

Returns a Javascript code representing the object

[Source]

     # File lib/gm_plugin/mapping.rb, line 94
 94:       def to_javascript
 95:         unless @variable.nil?
 96:           @variable
 97:         else
 98:           create
 99:         end
100:       end

[Validate]