Module: MapLayers::JsWrapper

Included in:
JsClass, JsExpr, Map
Defined in:
lib/map_layers/js_wrapper.rb

Overview

The module where all the Ruby-to-JavaScript conversion takes place. Based on Ym4r::GmPlugin::MappingObject from Guilhem Vellut

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Creates javascript code for missing methods + takes care of listeners



10
11
12
13
14
15
16
# File 'lib/map_layers/js_wrapper.rb', line 10

def method_missing(name,*args)
  str_name = name.to_s
  args.collect! do |arg|
    JsWrapper.javascriptify_variable(arg)
  end
  JsExpr.new("#{to_javascript}.#{JsWrapper.javascriptify_method(str_name)}(#{args.join(",")})")
end

Instance Attribute Details

#variableObject (readonly)

The name of the variable in JavaScript space.



7
8
9
# File 'lib/map_layers/js_wrapper.rb', line 7

def variable
  @variable
end

Class Method Details

.escape_javascript(javascript) ⇒ Object

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



51
52
53
# File 'lib/map_layers/js_wrapper.rb', line 51

def self.escape_javascript(javascript)
  javascript.gsub(/\r\n|\n|\r/, "\\n").gsub("\"") { |m| "\\#{m}" }
end

.javascriptify_method(method_name) ⇒ Object

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



56
57
58
# File 'lib/map_layers/js_wrapper.rb', line 56

def self.javascriptify_method(method_name)
  method_name.gsub(/_(\w)/){|s| $1.upcase}
end

.javascriptify_variable(arg) ⇒ Object

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/map_layers/js_wrapper.rb', line 32

def self.javascriptify_variable(arg)
  if arg.is_a?(JsWrapper)
    arg.to_javascript
  elsif arg.is_a?(String)
    "\"#{JsWrapper.escape_javascript(arg)}\""
  elsif arg.is_a?(Array)
    "[" + arg.collect{ |a| JsWrapper.javascriptify_variable(a)}.join(",") + "]"
  elsif arg.is_a?(Hash)
    "{" + arg.to_a.collect do |v|
      "#{JsWrapper.javascriptify_method(v[0].to_s)} : #{JsWrapper.javascriptify_variable(v[1])}"
    end.join(",") + "}"
  elsif arg.nil?
    "undefined"
  else
    arg.to_s
  end
end

Instance Method Details

#[](index) ⇒ Object

Creates javascript code for array or hash indexing



27
28
29
# File 'lib/map_layers/js_wrapper.rb', line 27

def [](index) #index could be an integer or string
  return JsExpr.new("#{to_javascript}[#{JsWrapper.javascriptify_variable(index)}]")
end

#assign_to(variable) ⇒ Object

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



79
80
81
82
# File 'lib/map_layers/js_wrapper.rb', line 79

def assign_to(variable)
  @variable = variable
  "#{@variable} = #{create};"
end

#createObject

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



110
111
# File 'lib/map_layers/js_wrapper.rb', line 110

def create
end

#declare(variable) ⇒ Object

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



61
62
63
64
# File 'lib/map_layers/js_wrapper.rb', line 61

def declare(variable)
  @variable = variable
  "var #{@variable} = #{create};"
end

#declare_random(init, size = 8) ⇒ Object

declare with a random variable name



67
68
69
70
71
# File 'lib/map_layers/js_wrapper.rb', line 67

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

#declared?Boolean

Checks if the MappinObject has been declared

Returns:

  • (Boolean)


74
75
76
# File 'lib/map_layers/js_wrapper.rb', line 74

def declared?
  !@variable.nil?
end

#get_property(property) ⇒ Object

Returns the code to get a property from the JsWrapper



90
91
92
# File 'lib/map_layers/js_wrapper.rb', line 90

def get_property(property)
  JsExpr.new("#{to_javascript}.#{JsWrapper.javascriptify_method(property.to_s)}")
end

#javascriptify_method_call(name, *args) ⇒ Object

Creates javascript code for method calls



19
20
21
22
23
24
# File 'lib/map_layers/js_wrapper.rb', line 19

def javascriptify_method_call(name,*args)
  args.collect! do |arg|
    JsWrapper.javascriptify_variable(arg)
  end
  JsExpr.new("#{to_javascript}.#{JsWrapper.javascriptify_method(name.to_s)}(#{args.join(",")})")
end

#set_property(property, value) ⇒ Object

Assign the value to the property of the JsWrapper



85
86
87
# File 'lib/map_layers/js_wrapper.rb', line 85

def set_property(property, value)
  "#{to_javascript}.#{JsWrapper.javascriptify_method(property.to_s)} = #{JsWrapper.javascriptify_variable(value)}"
end

#to_javascriptObject

Returns a Javascript code representing the object



95
96
97
98
99
100
101
# File 'lib/map_layers/js_wrapper.rb', line 95

def to_javascript
  unless @variable.nil?
    @variable
  else
    create
  end
end

#to_json(options = {}) ⇒ Object

To cheat JavaScriptGenerator::GeneratorMethods::javascript_object_for



104
105
106
# File 'lib/map_layers/js_wrapper.rb', line 104

def to_json(options = {})
  to_javascript
end