Class: ActiveRecordCalculator::CalculatorProxy
- Inherits:
-
Object
- Object
- ActiveRecordCalculator::CalculatorProxy
- Defined in:
- lib/active_record_calculator/calculator_proxy.rb
Instance Method Summary collapse
- #avg(column_name, as, options = {}) ⇒ Object (also: #average)
- #calculate ⇒ Object
- #cnt(column_name, as, options = {}) ⇒ Object (also: #count)
- #col(column_name, as = nil) ⇒ Object (also: #column)
- #columns ⇒ Object
- #connection ⇒ Object
- #find(finder_options = {}) ⇒ Object
- #group_operations ⇒ Object
-
#initialize(klass, finder_options = {}) ⇒ CalculatorProxy
constructor
A new instance of CalculatorProxy.
- #max(column_name, as, options = {}) ⇒ Object (also: #maximum)
- #min(column_name, as, options = {}) ⇒ Object (also: #minimum)
- #operations ⇒ Object
- #select_all(query) ⇒ Object
- #statement ⇒ Object
- #sum(column_name, as, options = {}) ⇒ Object
- #table ⇒ Object
- #update_key ⇒ Object
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, = {}) @klass = klass @operations = [] @columns = [] @group_operations = [] find() 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, = {}) add_operation(:avg, column_name, as, ) end |
#calculate ⇒ Object
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, = {}) add_operation(:count, column_name, as, ) 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 |
#columns ⇒ Object
44 45 46 |
# File 'lib/active_record_calculator/calculator_proxy.rb', line 44 def columns @columns end |
#connection ⇒ Object
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( = {}) .symbolize_keys! @finder_options = .except(:select, :include, :from, :readonly, :lock) end |
#group_operations ⇒ Object
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, = {}) add_operation(:max, column_name, as, ) 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, = {}) add_operation(:min, column_name, as, ) end |
#operations ⇒ Object
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 |
#statement ⇒ Object
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, = {}) add_operation(:sum, column_name, as, ) end |
#table ⇒ Object
40 41 42 |
# File 'lib/active_record_calculator/calculator_proxy.rb', line 40 def table @klass.table_name end |
#update_key ⇒ Object
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 |