Class: Ambition::Processor

Inherits:
SexpProcessor
  • Object
show all
Includes:
ActiveRecord::ConnectionAdapters::Quoting
Defined in:
lib/ambition/processor.rb

Direct Known Subclasses

OrderProcessor, WhereProcessor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



10
11
12
13
14
15
16
17
18
# File 'lib/ambition/processor.rb', line 10

def initialize
  super()
  @strict          = false
  @expected        = String
  @auto_shift_type = true
  @warn_on_default = false
  @default_method  = :process_error
  @includes        = []
end

Instance Attribute Details

#includesObject (readonly)

Returns the value of attribute includes.



7
8
9
# File 'lib/ambition/processor.rb', line 7

def includes
  @includes
end

#join_stringObject (readonly)

Returns the value of attribute join_string.



7
8
9
# File 'lib/ambition/processor.rb', line 7

def join_string
  @join_string
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/ambition/processor.rb', line 7

def key
  @key
end

#prefixObject (readonly)

Returns the value of attribute prefix.



7
8
9
# File 'lib/ambition/processor.rb', line 7

def prefix
  @prefix
end

Instance Method Details

#extract_includes(receiver, method) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/ambition/processor.rb', line 56

def extract_includes(receiver, method)
  return unless 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
end

#process_array(exp) ⇒ Object



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

def process_array(exp)
  arrayed = exp.map { |m| process(m) }
  exp.clear
  return arrayed.join(', ')
end

#process_dasgn_curr(exp) ⇒ Object



31
32
33
34
# File 'lib/ambition/processor.rb', line 31

def process_dasgn_curr(exp)
  @receiver = exp.shift
  return @receiver.to_s
end

#process_error(exp) ⇒ Object

Processing methods



22
23
24
# File 'lib/ambition/processor.rb', line 22

def process_error(exp)
  raise "Missing process method for sexp: #{exp.inspect}"
end

#process_proc(exp) ⇒ Object



26
27
28
29
# File 'lib/ambition/processor.rb', line 26

def process_proc(exp)
  receiver, body = process(exp.shift), exp.shift
  return process(body)
end

#sanitize(value) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/ambition/processor.rb', line 48

def sanitize(value)
  case value.to_s
  when 'true'  then '1'
  when 'false' then '0'
  else ActiveRecord::Base.connection.quote(value) rescue quote(value)
  end
end

#to_sObject

Helper methods



44
45
46
# File 'lib/ambition/processor.rb', line 44

def to_s
  process(@block.to_sexp).squeeze(' ')
end