Class: ActiveRecordCalculator::CalculatorProxy

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

Instance Method Summary collapse

Constructor Details

#initialize(klass, finder_options = {}) ⇒ CalculatorProxy

Returns a new instance of CalculatorProxy.



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

def initialize(klass, finder_options = {})
  @klass = klass
  @operations = []
  @columns = []
  @group_operations = []
  find(finder_options)
end

Instance Method Details

#avg(column_name, as, options = {}) ⇒ Object Also known as: average



25
26
27
# File 'lib/active_record_calculator/calculator_proxy.rb', line 25

def avg(column_name, as, options = {})
  add_operation(:avg, column_name, as, options)
end

#calculateObject



60
61
62
63
64
65
66
67
68
# File 'lib/active_record_calculator/calculator_proxy.rb', line 60

def calculate
  result = select_all(statement)
  result.collect do |row|
    @operations.each do |op|
      row[op.name] = type_cast(row[op.name])
    end
    row
  end
end

#cnt(column_name, as, options = {}) ⇒ Object Also known as: count



16
17
18
# File 'lib/active_record_calculator/calculator_proxy.rb', line 16

def cnt(column_name, as, options = {})
  add_operation(:count, column_name, as, options)
end

#col(column_name, as = nil) ⇒ Object Also known as: column



11
12
13
# File 'lib/active_record_calculator/calculator_proxy.rb', line 11

def col(column_name, as = nil)
  add_column(column_name, as)
end

#columnsObject



44
45
46
# File 'lib/active_record_calculator/calculator_proxy.rb', line 44

def columns
  @columns
end

#connectionObject



70
71
72
# File 'lib/active_record_calculator/calculator_proxy.rb', line 70

def connection
  @klass.connection
end

#find(finder_options = {}) ⇒ Object



78
79
80
81
# File 'lib/active_record_calculator/calculator_proxy.rb', line 78

def find(finder_options = {})
  finder_options.symbolize_keys!
  @finder_options = finder_options.except(:select, :include, :from, :readonly, :lock)
end

#group_operationsObject



52
53
54
# File 'lib/active_record_calculator/calculator_proxy.rb', line 52

def group_operations
  @group_operations
end

#max(column_name, as, options = {}) ⇒ Object Also known as: maximum



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

def max(column_name, as, options = {})
  add_operation(:max, column_name, as, options)
end

#min(column_name, as, options = {}) ⇒ Object Also known as: minimum



35
36
37
# File 'lib/active_record_calculator/calculator_proxy.rb', line 35

def min(column_name, as, options = {})
  add_operation(:min, column_name, as, options)
end

#operationsObject



48
49
50
# File 'lib/active_record_calculator/calculator_proxy.rb', line 48

def operations
  @operations
end

#select_all(query) ⇒ Object



74
75
76
# File 'lib/active_record_calculator/calculator_proxy.rb', line 74

def select_all(query)
  connection.select_all(query)
end

#statementObject



83
84
85
86
# File 'lib/active_record_calculator/calculator_proxy.rb', line 83

def statement
  add_group_operations
  construct_finder_sql.gsub(/^SELECT\s+\*/i, select)
end

#sum(column_name, as, options = {}) ⇒ Object



21
22
23
# File 'lib/active_record_calculator/calculator_proxy.rb', line 21

def sum(column_name, as, options = {})
  add_operation(:sum, column_name, as, options)
end

#tableObject



40
41
42
# File 'lib/active_record_calculator/calculator_proxy.rb', line 40

def table
  @klass.table_name
end

#update_keyObject



56
57
58
# File 'lib/active_record_calculator/calculator_proxy.rb', line 56

def update_key
  @group_operations.first ? @group_operations.first.name : nil
end