PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/plugins/ym4r_gm/lib/gm_plugin/mapping.rb

https://gitlab.com/sandorczettner/carpopularizer
Ruby | 128 lines | 118 code | 4 blank | 6 comment | 4 complexity | 47f5a5729e4754976f93edccee400270 MD5 | raw file
  1. module Ym4r
  2. module GmPlugin
  3. #The module where all the Ruby-to-JavaScript conversion takes place. It is included by all the classes in the YM4R library.
  4. module MappingObject
  5. #The name of the variable in JavaScript space.
  6. attr_reader :variable
  7. #Creates javascript code for missing methods + takes care of listeners
  8. def method_missing(name,*args)
  9. str_name = name.to_s
  10. if str_name =~ /^on_(.*)/
  11. if args.length != 1
  12. raise ArgumentError("Only 1 argument is allowed on on_ methods");
  13. else
  14. Variable.new("GEvent.addListener(#{to_javascript},\"#{MappingObject.javascriptify_method($1)}\",#{args[0]})")
  15. end
  16. else
  17. args.collect! do |arg|
  18. MappingObject.javascriptify_variable(arg)
  19. end
  20. Variable.new("#{to_javascript}.#{MappingObject.javascriptify_method(str_name)}(#{args.join(",")})")
  21. end
  22. end
  23. #Creates javascript code for array or hash indexing
  24. def [](index) #index could be an integer or string
  25. return Variable.new("#{to_javascript}[#{MappingObject.javascriptify_variable(index)}]")
  26. end
  27. #Transforms a Ruby object into a JavaScript string : MAppingObject, String, Array, Hash and general case (using to_s)
  28. def self.javascriptify_variable(arg)
  29. if arg.is_a?(MappingObject)
  30. arg.to_javascript
  31. elsif arg.is_a?(String)
  32. "\"#{MappingObject.escape_javascript(arg)}\""
  33. elsif arg.is_a?(Array)
  34. "[" + arg.collect{ |a| MappingObject.javascriptify_variable(a)}.join(",") + "]"
  35. elsif arg.is_a?(Hash)
  36. "{" + arg.to_a.collect do |v|
  37. "#{MappingObject.javascriptify_method(v[0].to_s)} : #{MappingObject.javascriptify_variable(v[1])}"
  38. end.join(",") + "}"
  39. elsif arg.nil?
  40. "undefined"
  41. else
  42. arg.to_s
  43. end
  44. end
  45. #Escape string to be used in JavaScript. Lifted from rails.
  46. def self.escape_javascript(javascript)
  47. javascript.gsub(/\r\n|\n|\r/, "\\n").gsub("\"") { |m| "\\#{m}" }
  48. end
  49. #Transform a ruby-type method name (like add_overlay) to a JavaScript-style one (like addOverlay).
  50. def self.javascriptify_method(method_name)
  51. method_name.gsub(/_(\w)/){|s| $1.upcase}
  52. end
  53. #Declares a Mapping Object bound to a JavaScript variable of name +variable+.
  54. def declare(variable)
  55. @variable = variable
  56. "var #{@variable} = #{create};"
  57. end
  58. #declare with a random variable name
  59. def declare_random(init,size = 8)
  60. s = init.clone
  61. 6.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
  62. declare(s)
  63. end
  64. #Checks if the MappinObject has been declared
  65. def declared?
  66. !@variable.nil?
  67. end
  68. #Binds a Mapping object to a previously declared JavaScript variable of name +variable+.
  69. def assign_to(variable)
  70. @variable = variable
  71. "#{@variable} = #{create};"
  72. end
  73. #Assign the +value+ to the +property+ of the MappingObject
  74. def set_property(property, value)
  75. "#{to_javascript}.#{MappingObject.javascriptify_method(property.to_s)} = #{MappingObject.javascriptify_variable(value)}"
  76. end
  77. #Returns the code to get a +property+ from the MappingObject
  78. def get_property(property)
  79. Variable.new("#{to_javascript}.#{MappingObject.javascriptify_method(property.to_s)}")
  80. end
  81. #Returns a Javascript code representing the object
  82. def to_javascript
  83. unless @variable.nil?
  84. @variable
  85. else
  86. create
  87. end
  88. end
  89. #Creates a Mapping Object in JavaScript.
  90. #To be implemented by subclasses if needed
  91. def create
  92. end
  93. end
  94. #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.
  95. class Variable
  96. include MappingObject
  97. def initialize(variable)
  98. @variable = variable
  99. end
  100. #Returns the javascript expression contained in the object.
  101. def create
  102. @variable
  103. end
  104. #Returns the expression inside the Variable followed by a ";"
  105. def to_s
  106. @variable + ";"
  107. end
  108. UNDEFINED = Variable.new("undefined")
  109. end
  110. end
  111. end