Class: Ext::JavaScript
- Inherits:
-
Object
show all
- Defined in:
- lib/ext/java_script.rb
Defined Under Namespace
Classes: Var
Instance Method Summary
collapse
Constructor Details
Returns a new instance of JavaScript.
18
19
20
21
|
# File 'lib/ext/java_script.rb', line 18
def initialize
@vars = ActiveSupport::OrderedHash.new
@records = []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
62
63
64
65
66
67
68
69
70
|
# File 'lib/ext/java_script.rb', line 62
def method_missing(*args)
name = args.shift
obj = var(name)
if args.empty?
obj
else
record obj.__send__(*args)
end
end
|
Instance Method Details
#execute(&block) ⇒ Object
49
50
51
|
# File 'lib/ext/java_script.rb', line 49
def execute(&block)
instance_eval &block
end
|
#record(js) ⇒ Object
45
46
47
|
# File 'lib/ext/java_script.rb', line 45
def record(js)
@records << js
end
|
#to_function ⇒ Object
53
54
55
|
# File 'lib/ext/java_script.rb', line 53
def to_function
"function(){%s}" % to_s
end
|
#to_s ⇒ Object
57
58
59
|
# File 'lib/ext/java_script.rb', line 57
def to_s
@records.map{|js| "#{js};"}.join("\n")
end
|
#var(*args) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/ext/java_script.rb', line 23
def var(*args) name = args[0].to_s
object = args[1]
case args.size
when 1 value = @vars[name] or
raise NameError, "undefined local variable `#{name}'"
return value
when 2 if respond_to?(name)
raise ArgumentError, "#{name} is reserved name"
end
@vars[name] = Var.new(name, object)
record @vars[name]
else
raise ArgumentError, "args size should be 2 or 3. (#{args.inspect})"
end
end
|