Class: Ambition::WhereProcessor
Instance Attribute Summary
Attributes inherited from Processor
#includes, #join_string, #key, #prefix
Instance Method Summary
collapse
Methods inherited from Processor
#extract_includes, #process_array, #process_dasgn_curr, #process_error, #process_proc, #sanitize, #to_s
Constructor Details
Returns a new instance of WhereProcessor.
13
14
15
16
17
18
19
20
|
# File 'lib/ambition/where.rb', line 13
def initialize(owner, block)
super()
@receiver = nil
@owner = owner
@table_name = owner.table_name
@block = block
@key = :conditions
end
|
Instance Method Details
#joined_expressions(with, exp) ⇒ Object
102
103
104
105
106
107
108
|
# File 'lib/ambition/where.rb', line 102
def joined_expressions(with, exp)
clauses = []
while clause = exp.shift
clauses << clause
end
return "(" + clauses.map { |c| process(c) }.join(" #{with} ") + ")"
end
|
#negate(method) ⇒ Object
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/ambition/where.rb', line 114
def negate(method)
case method
when :==
'<>'
when :=~
'!~'
else
raise "Not implemented: #{method}"
end
end
|
#process_and(exp) ⇒ Object
24
25
26
|
# File 'lib/ambition/where.rb', line 24
def process_and(exp)
joined_expressions 'AND', exp
end
|
#process_attrasgn(exp) ⇒ Object
95
96
97
98
|
# File 'lib/ambition/where.rb', line 95
def process_attrasgn(exp)
exp.clear
raise "Assignment not supported. Maybe you meant ==?"
end
|
#process_call(exp) ⇒ Object
38
39
40
41
42
43
|
# File 'lib/ambition/where.rb', line 38
def process_call(exp)
receiver, method, other = *exp
exp.clear
return translation(receiver, method, other)
end
|
#process_dvar(exp) ⇒ Object
70
71
72
73
74
75
76
77
|
# File 'lib/ambition/where.rb', line 70
def process_dvar(exp)
target = exp.shift
if target == @receiver
return @table_name
else
return value(target.to_s[0..-1])
end
end
|
#process_false(exp) ⇒ Object
57
58
59
|
# File 'lib/ambition/where.rb', line 57
def process_false(exp)
sanitize 'false'
end
|
#process_gvar(exp) ⇒ Object
91
92
93
|
# File 'lib/ambition/where.rb', line 91
def process_gvar(exp)
value(exp.shift.to_s)
end
|
#process_ivar(exp) ⇒ Object
79
80
81
|
# File 'lib/ambition/where.rb', line 79
def process_ivar(exp)
value(exp.shift.to_s[0..-1])
end
|
#process_lit(exp) ⇒ Object
45
46
47
|
# File 'lib/ambition/where.rb', line 45
def process_lit(exp)
exp.shift.to_s
end
|
#process_lvar(exp) ⇒ Object
83
84
85
|
# File 'lib/ambition/where.rb', line 83
def process_lvar(exp)
value(exp.shift.to_s)
end
|
#process_match3(exp) ⇒ Object
65
66
67
68
|
# File 'lib/ambition/where.rb', line 65
def process_match3(exp)
regexp, target = exp.shift.last.inspect.gsub(/\/([^\/]+)\/\S*/, '\1'), process(exp.shift)
"#{target} REGEXP '#{regexp}'"
end
|
#process_nil(exp) ⇒ Object
53
54
55
|
# File 'lib/ambition/where.rb', line 53
def process_nil(exp)
'NULL'
end
|
#process_not(exp) ⇒ Object
32
33
34
35
36
|
# File 'lib/ambition/where.rb', line 32
def process_not(exp)
_, receiver, method, other = *exp.first
exp.clear
return translation(receiver, negate(method), other)
end
|
#process_or(exp) ⇒ Object
28
29
30
|
# File 'lib/ambition/where.rb', line 28
def process_or(exp)
joined_expressions 'OR', exp
end
|
#process_str(exp) ⇒ Object
49
50
51
|
# File 'lib/ambition/where.rb', line 49
def process_str(exp)
sanitize exp.shift
end
|
#process_true(exp) ⇒ Object
61
62
63
|
# File 'lib/ambition/where.rb', line 61
def process_true(exp)
sanitize 'true'
end
|
#process_vcall(exp) ⇒ Object
87
88
89
|
# File 'lib/ambition/where.rb', line 87
def process_vcall(exp)
value(exp.shift.to_s)
end
|
#translation(receiver, method, other) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/ambition/where.rb', line 125
def translation(receiver, method, other)
case method.to_s
when '=='
case other_value = process(other)
when "NULL"
"#{process(receiver)} is #{other_value}"
else
"#{process(receiver)} = #{other_value}"
end
when '<>', '>', '<'
"#{process(receiver)} #{method} #{process(other)}"
when 'include?'
"#{process(other)} IN (#{process(receiver)})"
when '=~'
"#{process(receiver)} LIKE #{process(other)}"
when '!~'
"#{process(receiver)} NOT LIKE #{process(other)}"
when 'upcase'
"UPPER(#{process(receiver)})"
when 'downcase'
"LOWER(#{process(receiver)})"
else
(receiver, method) || "#{process(receiver)}.`#{method}`"
end
end
|
#value(variable) ⇒ Object
110
111
112
|
# File 'lib/ambition/where.rb', line 110
def value(variable)
sanitize eval(variable, @block)
end
|