Class: Mack::JavaScript::ScriptGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/mack-javascript/helpers/script_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScriptGenerator

Returns a new instance of ScriptGenerator.



4
5
6
# File 'lib/mack-javascript/helpers/script_generator.rb', line 4

def initialize
  @lines = ''
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



8
9
10
# File 'lib/mack-javascript/helpers/script_generator.rb', line 8

def method_missing(sym, *args)
  self << self.class.framework.send(sym, *args)
end

Class Method Details

.frameworkObject



41
42
43
44
45
# File 'lib/mack-javascript/helpers/script_generator.rb', line 41

def framework
  ivar_cache('framework_constant') do
    "Mack::JavaScript::Framework::#{framework_name}".constantize
  end
end

.framework=(args) ⇒ Object



47
48
49
50
# File 'lib/mack-javascript/helpers/script_generator.rb', line 47

def framework=(args)
  @framework_constant = nil
  @@framework_name = args.camelcase
end

Instance Method Details

#<<(s) ⇒ Object



12
13
14
# File 'lib/mack-javascript/helpers/script_generator.rb', line 12

def <<(s)
  @lines << s + ";"
end

#alert(message) ⇒ Object



20
21
22
# File 'lib/mack-javascript/helpers/script_generator.rb', line 20

def alert(message)
  self << "alert('#{message}')"
end

#assign(variable, value) ⇒ Object



31
32
33
# File 'lib/mack-javascript/helpers/script_generator.rb', line 31

def assign(variable, value)
  self << "#{variable} = #{value.to_json}"
end

#call(*args, &block) ⇒ Object



24
25
26
27
28
29
# File 'lib/mack-javascript/helpers/script_generator.rb', line 24

def call(*args, &block)
  s = args.shift + '('
  a = []
  args.each {|arg| a << arg.to_json}
  self << s + a.join(',') + ')'
end

#delay(seconds = 1, &block) ⇒ Object



35
36
37
# File 'lib/mack-javascript/helpers/script_generator.rb', line 35

def delay(seconds = 1, &block)
  self << "setTimeout(function() {\n\n" + yield(Mack::JavaScript::ScriptGenerator.new) + "}, #{(seconds * 1000).to_i})"
end

#to_sObject



16
17
18
# File 'lib/mack-javascript/helpers/script_generator.rb', line 16

def to_s
  @lines
end