Module: Ym4r::GmPlugin::MappingObject
- Included in:
- Clusterer, ELabel, GBounds, GControlPosition, GGeoXml, GHierarchicalMapTypeControl, GIcon, GInfoWindowTab, GLargeMapControl, GLatLng, GLatLngBounds, GLocalSearchControl, GMap, GMapType, GMapTypeControl, GMarker, GMarkerGroup, GMarkerManager, GMercatorProjection, GOverviewMapControl, GPoint, GPolygon, GPolygonEncoded, GPolyline, GPolylineEncoded, GScaleControl, GSize, GSmallMapControl, GSmallZoomControl, GTileLayer, GeoRssOverlay, ManagedMarker, Variable
- Defined in:
- lib/gm_plugin/mapping.rb
Overview
The module where all the Ruby-to-JavaScript conversion takes place. It is included by all the classes in the YM4R library.
Instance Attribute Summary (collapse)
-
- (Object) variable
readonly
The name of the variable in JavaScript space.
Class Method Summary (collapse)
-
+ (Object) escape_javascript(javascript)
Escape string to be used in JavaScript.
-
+ (Object) javascriptify_method(method_name)
Transform a ruby-type method name (like add_overlay) to a JavaScript-style one (like addOverlay).
-
+ (Object) javascriptify_variable(arg)
Transforms a Ruby object into a JavaScript string : MAppingObject, String, Array, Hash and general case (using to_s).
Instance Method Summary (collapse)
-
- (Object) [](index)
Creates javascript code for array or hash indexing.
-
- (Object) assign_to(variable)
Binds a Mapping object to a previously declared JavaScript variable of name variable.
-
- (Object) create
Creates a Mapping Object in JavaScript.
-
- (Object) declare(variable)
Declares a Mapping Object bound to a JavaScript variable of name variable.
-
- (Object) declare_random(init, size = 8)
declare with a random variable name.
-
- (Boolean) declared?
Checks if the MappinObject has been declared.
-
- (Object) get_property(property)
Returns the code to get a property from the MappingObject.
-
- (Object) method_missing(name, *args)
Creates javascript code for missing methods + takes care of listeners.
-
- (Object) set_property(property, value)
Assign the value to the property of the MappingObject.
-
- (Object) to_javascript
Returns a Javascript code representing the object.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(name, *args)
Creates javascript code for missing methods + takes care of listeners
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/gm_plugin/mapping.rb', line 9 def method_missing(name,*args) str_name = name.to_s if str_name =~ /^on_(.*)/ if args.length != 1 raise ArgumentError("Only 1 argument is allowed on on_ methods"); else Variable.new("GEvent.addListener(#{to_javascript},\"#{MappingObject.javascriptify_method($1)}\",#{args[0]})") end else args.collect! do |arg| MappingObject.javascriptify_variable(arg) end Variable.new("#{to_javascript}.#{MappingObject.javascriptify_method(str_name)}(#{args.join(",")})") end end |
Instance Attribute Details
- (Object) variable (readonly)
The name of the variable in JavaScript space.
6 7 8 |
# File 'lib/gm_plugin/mapping.rb', line 6 def variable @variable end |
Class Method Details
+ (Object) escape_javascript(javascript)
Escape string to be used in JavaScript. Lifted from rails.
50 51 52 |
# File 'lib/gm_plugin/mapping.rb', line 50 def self.escape_javascript(javascript) javascript.gsub(/\r\n|\n|\r/, "\\n").gsub("\"") { |m| "\\#{m}" } end |
+ (Object) javascriptify_method(method_name)
Transform a ruby-type method name (like add_overlay) to a JavaScript-style one (like addOverlay).
55 56 57 |
# File 'lib/gm_plugin/mapping.rb', line 55 def self.javascriptify_method(method_name) method_name.gsub(/_(\w)/){|s| $1.upcase} end |
+ (Object) javascriptify_variable(arg)
Transforms a Ruby object into a JavaScript string : MAppingObject, String, Array, Hash and general case (using to_s)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gm_plugin/mapping.rb', line 31 def self.javascriptify_variable(arg) if arg.is_a?(MappingObject) arg.to_javascript elsif arg.is_a?(String) "\"#{MappingObject.escape_javascript(arg)}\"" elsif arg.is_a?(Array) "[" + arg.collect{ |a| MappingObject.javascriptify_variable(a)}.join(",") + "]" elsif arg.is_a?(Hash) "{" + arg.to_a.collect do |v| "#{MappingObject.javascriptify_method(v[0].to_s)} : #{MappingObject.javascriptify_variable(v[1])}" end.join(",") + "}" elsif arg.nil? "undefined" else arg.to_s end end |
Instance Method Details
- (Object) [](index)
Creates javascript code for array or hash indexing
26 27 28 |
# File 'lib/gm_plugin/mapping.rb', line 26 def [](index) #index could be an integer or string return Variable.new("#{to_javascript}[#{MappingObject.javascriptify_variable(index)}]") end |
- (Object) assign_to(variable)
Binds a Mapping object to a previously declared JavaScript variable of name variable.
78 79 80 81 |
# File 'lib/gm_plugin/mapping.rb', line 78 def assign_to(variable) @variable = variable "#{@variable} = #{create};" end |
- (Object) create
Creates a Mapping Object in JavaScript. To be implemented by subclasses if needed
104 105 |
# File 'lib/gm_plugin/mapping.rb', line 104 def create end |
- (Object) declare(variable)
Declares a Mapping Object bound to a JavaScript variable of name variable.
60 61 62 63 |
# File 'lib/gm_plugin/mapping.rb', line 60 def declare(variable) @variable = variable "var #{@variable} = #{create};" end |
- (Object) declare_random(init, size = 8)
declare with a random variable name
66 67 68 69 70 |
# File 'lib/gm_plugin/mapping.rb', line 66 def declare_random(init,size = 8) s = init.clone 6.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr } declare(s) end |
- (Boolean) declared?
Checks if the MappinObject has been declared
73 74 75 |
# File 'lib/gm_plugin/mapping.rb', line 73 def declared? !@variable.nil? end |
- (Object) get_property(property)
Returns the code to get a property from the MappingObject
89 90 91 |
# File 'lib/gm_plugin/mapping.rb', line 89 def get_property(property) Variable.new("#{to_javascript}.#{MappingObject.javascriptify_method(property.to_s)}") end |
- (Object) set_property(property, value)
Assign the value to the property of the MappingObject
84 85 86 |
# File 'lib/gm_plugin/mapping.rb', line 84 def set_property(property, value) "#{to_javascript}.#{MappingObject.javascriptify_method(property.to_s)} = #{MappingObject.javascriptify_variable(value)}" end |
- (Object) to_javascript
Returns a Javascript code representing the object
94 95 96 97 98 99 100 |
# File 'lib/gm_plugin/mapping.rb', line 94 def to_javascript unless @variable.nil? @variable else create end end |