Class: ActiveRecordCalculator::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_calculator/operation.rb

Instance Method Summary collapse

Constructor Details

#initialize(op, column, as, options) ⇒ Operation

Returns a new instance of Operation.



4
5
6
7
8
9
# File 'lib/active_record_calculator/operation.rb', line 4

def initialize(op, column, as, options)
  @op = op
  @column = column
  @as = as
  @options = options
end

Instance Method Details

#build_select(klass) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_record_calculator/operation.rb', line 11

def build_select(klass)
  mock = klass.send(:construct_calculation_sql, @op, @column, @options)
  all, sel, cond = if mock =~ /\s+WHERE\s+/
    mock.match(/^SELECT\s+(.*)\s+AS.*\s+WHERE\s?(.*)/).to_a
  else  
    mock.match(/^SELECT\s+(.*)\s+AS/).to_a
  end
  return "#{sel} AS #{@as}" unless cond
  i = sel.index('(')
  start = sel[0..i]
  mid = "CASE #{cond} WHEN 1 THEN #{sel[i+1..-2]} ELSE #{default} END"
  fin = ") AS #{@as}"
  "#{start}#{mid}#{fin}"
end

#inspectObject



26
27
28
# File 'lib/active_record_calculator/operation.rb', line 26

def inspect
  "#{@as} => #{@op}"
end

#nameObject



30
31
32
# File 'lib/active_record_calculator/operation.rb', line 30

def name
  @as.to_s
end

#opObject



34
35
36
# File 'lib/active_record_calculator/operation.rb', line 34

def op
  @op
end