Class: Ropenlayer::Openlayer::Js

Inherits:
Object
  • Object
show all
Defined in:
lib/ropenlayer/openlayer/js.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initialize_string = '') ⇒ Js

Ropenlayer::Openlayer::Js

Just a simple JS constructor for objects methods calls and variable setting



10
11
12
# File 'lib/ropenlayer/openlayer/js.rb', line 10

def initialize(initialize_string = '')
  @body = initialize_string
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



5
6
7
# File 'lib/ropenlayer/openlayer/js.rb', line 5

def body
  @body
end

Class Method Details

.new_method(method, options = {}) ⇒ Object

create a new js method

Ropenlayer::Openlayer::Js.new_method("Element.create", "div", { :class => 'eyecandy', :id => 'my_id'}).to_s
#=> new Element.create("div", { "class":"eyecandy","id":"my_id"});


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ropenlayer/openlayer/js.rb', line 19

def self.new_method(method, options = {})
  js_object  = new
  args        = options[:args]        || []
  propierties = options[:propierties] || {}
  
  js_object.body =  "new #{ method }("
  js_object.body << "#{ args.join(', ') }" if args.any?
  
  if propierties.keys.any?
    
    js_object.body << ", " if args.any? 
    
    js_object.body << propierties.keys.inject("{") do |propierties_string, propierty|
                        propierties_string << "'#{ propierty }': #{ propierties[propierty] }"
                        propierties_string << ", " unless propierties.keys.last == propierty
                        propierties_string
                      end
    js_object.body << "}"               
    
  end
  js_object.body << ")"
  js_object
end

.new_var(name, value) ⇒ Object

create a new js global variable:

Ropenlayer::Js.new_var("test", "1").to_s
#=> var test = "1"


48
49
50
# File 'lib/ropenlayer/openlayer/js.rb', line 48

def self.new_var(name, value)
  new("var #{ name } = #{ value }")
end

Instance Method Details

#to_jsObject



57
58
59
# File 'lib/ropenlayer/openlayer/js.rb', line 57

def to_js
  "#{ @body };"
end

#to_sObject



53
54
55
# File 'lib/ropenlayer/openlayer/js.rb', line 53

def to_s
  @body
end