Class: Ambition::WhereProcessor
- Inherits:
-
Processor
- Object
- SexpProcessor
- Processor
- Ambition::WhereProcessor
show all
- Defined in:
- lib/ambition/lib/ambition/where.rb
Instance Attribute Summary collapse
Attributes inherited from Processor
#join_string, #key, #prefix
Instance Method Summary
collapse
Methods inherited from Processor
#process_array, #process_dasgn_curr, #process_error, #process_proc, #sanitize, #to_s
Constructor Details
Returns a new instance of WhereProcessor.
21
22
23
24
25
26
27
28
29
|
# File 'lib/ambition/lib/ambition/where.rb', line 21
def initialize(owner, block)
super()
@receiver = nil
@owner = owner
@table_name = owner.table_name
@block = block
@key = :conditions
@includes = []
end
|
Instance Attribute Details
#includes ⇒ Object
Returns the value of attribute includes.
19
20
21
|
# File 'lib/ambition/lib/ambition/where.rb', line 19
def includes
@includes
end
|
Instance Method Details
#build_condition(receiver, method, other) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/ambition/lib/ambition/where.rb', line 151
def build_condition(receiver, method, other)
if receiver.first == :call && receiver[1].last == @receiver
if reflection = @owner.reflections[receiver.last]
@includes << reflection.name unless @includes.include? reflection.name
"#{reflection.table_name}.#{method}"
else
raise "No reflection `#{receiver.last}' found on #{@owner}"
end
else
"#{process(receiver)}.`#{method}` #{process(other)}"
end
end
|
#joined_expressions(with, exp) ⇒ Object
111
112
113
114
115
116
117
|
# File 'lib/ambition/lib/ambition/where.rb', line 111
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
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/ambition/lib/ambition/where.rb', line 123
def negate(method)
case method
when :==
'<>'
when :=~
'!~'
else
raise "Not implemented: #{method}"
end
end
|
#process_and(exp) ⇒ Object
33
34
35
|
# File 'lib/ambition/lib/ambition/where.rb', line 33
def process_and(exp)
joined_expressions 'AND', exp
end
|
#process_attrasgn(exp) ⇒ Object
104
105
106
107
|
# File 'lib/ambition/lib/ambition/where.rb', line 104
def process_attrasgn(exp)
exp.clear
raise "Assignment not supported. Maybe you meant ==?"
end
|
#process_call(exp) ⇒ Object
47
48
49
50
51
52
|
# File 'lib/ambition/lib/ambition/where.rb', line 47
def process_call(exp)
receiver, method, other = *exp
exp.clear
return translation(receiver, method, other)
end
|
#process_dvar(exp) ⇒ Object
79
80
81
82
83
84
85
86
|
# File 'lib/ambition/lib/ambition/where.rb', line 79
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
66
67
68
|
# File 'lib/ambition/lib/ambition/where.rb', line 66
def process_false(exp)
sanitize 'false'
end
|
#process_gvar(exp) ⇒ Object
100
101
102
|
# File 'lib/ambition/lib/ambition/where.rb', line 100
def process_gvar(exp)
value(exp.shift.to_s)
end
|
#process_ivar(exp) ⇒ Object
88
89
90
|
# File 'lib/ambition/lib/ambition/where.rb', line 88
def process_ivar(exp)
value(exp.shift.to_s[0..-1])
end
|
#process_lit(exp) ⇒ Object
54
55
56
|
# File 'lib/ambition/lib/ambition/where.rb', line 54
def process_lit(exp)
exp.shift.to_s
end
|
#process_lvar(exp) ⇒ Object
92
93
94
|
# File 'lib/ambition/lib/ambition/where.rb', line 92
def process_lvar(exp)
value(exp.shift.to_s)
end
|
#process_match3(exp) ⇒ Object
74
75
76
77
|
# File 'lib/ambition/lib/ambition/where.rb', line 74
def process_match3(exp)
regexp, target = exp.shift.last.inspect.gsub('/',''), process(exp.shift)
"#{target} REGEXP '#{regexp}'"
end
|
#process_nil(exp) ⇒ Object
62
63
64
|
# File 'lib/ambition/lib/ambition/where.rb', line 62
def process_nil(exp)
'NULL'
end
|
#process_not(exp) ⇒ Object
41
42
43
44
45
|
# File 'lib/ambition/lib/ambition/where.rb', line 41
def process_not(exp)
_, receiver, method, other = *exp.first
exp.clear
return translation(receiver, negate(method), other)
end
|
#process_or(exp) ⇒ Object
37
38
39
|
# File 'lib/ambition/lib/ambition/where.rb', line 37
def process_or(exp)
joined_expressions 'OR', exp
end
|
#process_str(exp) ⇒ Object
58
59
60
|
# File 'lib/ambition/lib/ambition/where.rb', line 58
def process_str(exp)
sanitize exp.shift
end
|
#process_true(exp) ⇒ Object
70
71
72
|
# File 'lib/ambition/lib/ambition/where.rb', line 70
def process_true(exp)
sanitize 'true'
end
|
#process_vcall(exp) ⇒ Object
96
97
98
|
# File 'lib/ambition/lib/ambition/where.rb', line 96
def process_vcall(exp)
value(exp.shift.to_s)
end
|
#translation(receiver, method, other) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/ambition/lib/ambition/where.rb', line 134
def translation(receiver, method, other)
case method.to_s
when '=='
"#{process(receiver)} = #{process(other)}"
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)}"
else
build_condition(receiver, method, other)
end
end
|
#value(variable) ⇒ Object
119
120
121
|
# File 'lib/ambition/lib/ambition/where.rb', line 119
def value(variable)
sanitize eval(variable, @block)
end
|