Module: ArResultCalculations::Calculations::InstanceMethods

Defined in:
lib/ar_result_calculations/ar_result_calculations.rb

Instance Method Summary collapse

Instance Method Details

#count(column = nil) ⇒ Object

Return the count of a column from an active record result set Calls super() if not a result set. nil values are not counted

column:  The column name to count


35
36
37
38
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 35

def count(column = nil)
  return super() unless column && first && first.class.respond_to?(:descends_from_active_record?)         
  inject( 0 ) { |sum, x| x[column].nil? ? sum : sum + 1 }
end

#make_numeric(column) ⇒ Object Also known as: coerce_numeric

Force a column to be numeric. Useful if you have derived columns from a query that is not part of the base model.

column:  The column name to sum

returns self so you can compose other methods.


93
94
95
96
97
98
99
100
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 93

def make_numeric(column)
  return self unless column && first && first.class.respond_to?(:descends_from_active_record?)
  each do |row|
    next if is_numeric?(row[column])
    row[column] = row[column] =~ /[-+]?[0-9]+(\.[0-9]+)/ ? row[column].to_f : row[column].to_i
  end
  self
end

#max(column = nil) ⇒ Object Also known as: maximum

Return the max of a column from an active record result set Calls super() if not a result set

column:  The column name to max


44
45
46
47
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 44

def max(column = nil)
  return super() unless column && first && first.class.respond_to?(:descends_from_active_record?)  
  map(&column.to_sym).max
end

#mean(column = nil) ⇒ Object Also known as: avg, average

Return the average of a column from an active record result set Calls super() if not a result set

column:  The column name to average


24
25
26
27
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 24

def mean(column = nil)
  return super() unless column && first && first.class.respond_to?(:descends_from_active_record?)
  (length > 0) ? sum(column) / length : 0
end

#min(column = nil) ⇒ Object Also known as: minimum

Return the min of a column from an active record result set Calls super() if not a result set

column:  The column name to sum


54
55
56
57
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 54

def min(column = nil)
  return super() unless column && first && first.class.respond_to?(:descends_from_active_record?)  
  map(&column.to_sym).min
end

#regression(column = nil) ⇒ Object

Return a regression (OLS) of a column from an active record result set Calls super() if not a result set

column:  The column name to regress


64
65
66
67
68
69
70
71
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 64

def regression(column = nil)
  return nil unless first
  unless is_numeric?(first)
    raise ArgumentError, "Regression needs an array of ActiveRecord objects" unless column && first && first.class.respond_to?(:descends_from_active_record?)  
    series = map { |x| x[column] }
  end
  Array::LinearRegression.new(series || self).fit
end

#slope(column = nil) ⇒ Object Also known as: trend

Return the slope of a regression on a column from an active record result set Calls super() if not a result set

column:  The column name to regress


77
78
79
80
81
82
83
84
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 77

def slope(column = nil)
  return nil unless first
  unless is_numeric?(first)
    column ||= first_numeric_column
    series = map { |x| x[column] }
  end
  Array::LinearRegression.new(series || self).slope
end

#sum(column = nil) ⇒ Object

Return the sum of a column from an active record result set Calls super() if not a result set

column:  The column name to sum


15
16
17
18
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 15

def sum(column = nil)
  return super() unless column && first && first.class.respond_to?(:descends_from_active_record?)
  inject( 0 ) { |sum, x| x[column].nil? ? sum : sum + x[column] }
end