Class: Rbjs::Expression
- Inherits:
-
Object
show all
- Defined in:
- lib/rbjs.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, view_context = nil, *args, &block) ⇒ Expression
Returns a new instance of Expression.
63
64
65
66
67
68
69
|
# File 'lib/rbjs.rb', line 63
def initialize name, view_context = nil, *args, &block
@child_expressions = []
@name = name.to_s
@_view_context = view_context
args << block if block_given?
@arguments = args.map{|arg| to_argument(arg)}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
71
72
73
74
75
76
|
# File 'lib/rbjs.rb', line 71
def method_missing name, *args, &block
expression = Expression.new name.to_s.gsub('!', '()'), @_view_context, *args, &block
expression.parent_expression = self
@child_expressions << expression
expression
end
|
Instance Attribute Details
#child_expressions ⇒ Object
Returns the value of attribute child_expressions.
60
61
62
|
# File 'lib/rbjs.rb', line 60
def child_expressions
@child_expressions
end
|
#is_argument ⇒ Object
Returns the value of attribute is_argument.
61
62
63
|
# File 'lib/rbjs.rb', line 61
def is_argument
@is_argument
end
|
#parent_expression ⇒ Object
Returns the value of attribute parent_expression.
59
60
61
|
# File 'lib/rbjs.rb', line 59
def parent_expression
@parent_expression
end
|
Instance Method Details
#argument_list ⇒ Object
98
99
100
101
|
# File 'lib/rbjs.rb', line 98
def argument_list
return '' if @arguments.empty?
'(' + @arguments.join(', ') + ')'
end
|
#last_childs ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/rbjs.rb', line 103
def last_childs
if @child_expressions.length > 0
@child_expressions.map(&:last_childs).flatten
else
[self]
end
end
|
#to_argument(arg) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/rbjs.rb', line 111
def to_argument arg
if arg.is_a?(Expression)
arg.is_argument = true
arg.to_s
elsif arg.is_a?(ArgumentProxy)
arg.to_s
elsif arg.is_a?(Array)
'['+arg.map{|val|to_argument(val)}.join(', ')+']'
elsif arg.is_a?(Hash)
'{'+arg.map{|key, val|to_argument(key)+': '+to_argument(val)}.join(',')+'}'
elsif arg.is_a?(Proc)
root = Root.new(@_view_context, &arg)
function_parameters = []
function_parameter_names = []
for param in arg.parameters
function_parameter_names << param[1]
function_parameters << ArgumentProxy.new(root, param[1])
end
"function(#{function_parameter_names.join ', '}) {\n#{root.evaluate function_parameters}}"
elsif arg.is_a?(Regexp)
arg.inspect
else
arg.to_json
end
end
|
#to_ary ⇒ Object
94
95
96
|
# File 'lib/rbjs.rb', line 94
def to_ary
nil
end
|
#to_s ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/rbjs.rb', line 78
def to_s
if ['+','-','*','/'].include?(@name)
@parent_expression.to_s + @name + @arguments.first
elsif @name == '[]='
@parent_expression.to_s + '[' + @arguments.first + ']= ' + @arguments.last
elsif @name == '[]'
@parent_expression.to_s + '[' + @arguments.first + ']'
elsif @parent_expression
parent_str = @parent_expression.to_s
parent_str += parent_str == 'var' ? ' ' : '.'
parent_str + @name + argument_list
else
@name + argument_list
end
end
|