Class: ActiveRecordCalculator::Operation
- Inherits:
-
Object
- Object
- ActiveRecordCalculator::Operation
- Defined in:
- lib/active_record_calculator/operation.rb
Instance Method Summary collapse
- #build_select(klass) ⇒ Object
-
#initialize(op, column, as, options) ⇒ Operation
constructor
A new instance of Operation.
- #inspect ⇒ Object
- #name ⇒ Object
- #op ⇒ Object
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, ) @op = op @column = column @as = as @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 |
#inspect ⇒ Object
26 27 28 |
# File 'lib/active_record_calculator/operation.rb', line 26 def inspect "#{@as} => #{@op}" end |
#name ⇒ Object
30 31 32 |
# File 'lib/active_record_calculator/operation.rb', line 30 def name @as.to_s end |
#op ⇒ Object
34 35 36 |
# File 'lib/active_record_calculator/operation.rb', line 34 def op @op end |