Class: Ambition::SelectProcessor

Inherits:
Processor show all
Defined in:
lib/ambition/select_processor.rb

Instance Attribute Summary

Attributes inherited from Processor

#includes, #join_string, #key, #prefix

Instance Method Summary collapse

Methods inherited from Processor

#active_connection?, #adapter_name, #extract_includes, #process, #process_array, #process_dasgn_curr, #process_proc, #quote_column_name, #rubify, #sanitize, #statement, #to_s, #value

Constructor Details

#initialize(owner, block) ⇒ SelectProcessor

Returns a new instance of SelectProcessor.



3
4
5
6
7
8
9
10
# File 'lib/ambition/select_processor.rb', line 3

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

Processor helper methods



98
99
100
101
102
103
104
# File 'lib/ambition/select_processor.rb', line 98

def joined_expressions(with, exp)
  clauses = []
  while clause = exp.shift
    clauses << clause
  end
  "(" + clauses.map { |c| process(c) }.join(" #{with} ") + ")"
end

#negate(method, target = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ambition/select_processor.rb', line 106

def negate(method, target = nil)
  if Array(target).last == [:nil]
    return 'IS NOT'
  end

  case method
  when :== 
    '<>'
  when :=~
    '!~'
  else 
    raise "Not implemented: #{method.inspect}"
  end
end

#process_and(exp) ⇒ Object

Sexp Processing Methods



14
15
16
# File 'lib/ambition/select_processor.rb', line 14

def process_and(exp)
  joined_expressions 'AND', exp
end

#process_attrasgn(exp) ⇒ Object



92
93
94
# File 'lib/ambition/select_processor.rb', line 92

def process_attrasgn(exp)
  raise "Assignment not supported.  Maybe you meant ==?"
end

#process_call(exp) ⇒ Object



36
37
38
39
40
# File 'lib/ambition/select_processor.rb', line 36

def process_call(exp)
  receiver, method, other = *exp

  translation(receiver, method, other)
end

#process_dvar(exp) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/ambition/select_processor.rb', line 67

def process_dvar(exp)
  target = exp.shift
  if target == @receiver
    @table_name
  else
    value(target.to_s[0..-1])
  end
end

#process_false(exp) ⇒ Object



54
55
56
# File 'lib/ambition/select_processor.rb', line 54

def process_false(exp)
  sanitize 'false'
end

#process_gvar(exp) ⇒ Object



88
89
90
# File 'lib/ambition/select_processor.rb', line 88

def process_gvar(exp)
  value(exp.shift.to_s)
end

#process_ivar(exp) ⇒ Object



76
77
78
# File 'lib/ambition/select_processor.rb', line 76

def process_ivar(exp)
  value(exp.shift.to_s[0..-1])
end

#process_lit(exp) ⇒ Object



42
43
44
# File 'lib/ambition/select_processor.rb', line 42

def process_lit(exp)
  exp.shift.to_s
end

#process_lvar(exp) ⇒ Object



80
81
82
# File 'lib/ambition/select_processor.rb', line 80

def process_lvar(exp)
  value(exp.shift.to_s)
end

#process_match3(exp) ⇒ Object



62
63
64
65
# File 'lib/ambition/select_processor.rb', line 62

def process_match3(exp)
  regexp, target = exp.shift.last, process(exp.shift)
  "#{target} #{statement(:regexp, regexp)} #{sanitize(regexp)}"
end

#process_nil(exp) ⇒ Object



50
51
52
# File 'lib/ambition/select_processor.rb', line 50

def process_nil(exp)
  'NULL'
end

#process_not(exp) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ambition/select_processor.rb', line 22

def process_not(exp)
  type, receiver, method, other = *exp.first

  case type
  when :call
    translation(receiver, negate(method, other), other)
  when :match3
    regexp = receiver.last
    "#{process(method)} #{statement(:negated_regexp, regexp)} #{sanitize(regexp)}"
  else
    process_error(exp)
  end
end

#process_or(exp) ⇒ Object



18
19
20
# File 'lib/ambition/select_processor.rb', line 18

def process_or(exp)
  joined_expressions 'OR', exp
end

#process_str(exp) ⇒ Object



46
47
48
# File 'lib/ambition/select_processor.rb', line 46

def process_str(exp)
  sanitize exp.shift
end

#process_true(exp) ⇒ Object



58
59
60
# File 'lib/ambition/select_processor.rb', line 58

def process_true(exp)
  sanitize 'true'
end

#process_vcall(exp) ⇒ Object



84
85
86
# File 'lib/ambition/select_processor.rb', line 84

def process_vcall(exp)
  value(exp.shift.to_s)
end

#translation(receiver, method, other = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ambition/select_processor.rb', line 121

def translation(receiver, method, other = nil)
  case method.to_s
  when 'IS NOT'
    "#{process(receiver)} IS NOT #{process(other)}"
  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
    extract_includes(receiver, method) || "#{process(receiver)}.#{quote_column_name(method)}"
  end
end