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


39
40
41
42
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 39

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.


116
117
118
119
120
121
122
123
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 116

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


48
49
50
51
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 48

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
28
29
30
31
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 24

def mean(column = nil)
  total = if column && first && first.class.respond_to?(:descends_from_active_record?)
    sum(column)
  else
    sum
  end
  (length > 0) ? total / 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


58
59
60
61
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 58

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


68
69
70
71
72
73
74
75
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 68

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

#sample_variance(column = nil) ⇒ Object

Return the variance of a column from an active record result set (or self)

column:  The column name to regress


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

def sample_variance(column = nil)
  data = column ? map(&column.to_sym) : self
  return nil unless data.first
  avg = data.average
  sum = data.inject(0) {|acc, i| acc + (i - avg)**2 }
  1 / data.length.to_f * sum
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


81
82
83
84
85
86
87
88
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 81

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

#standard_deviation(column = nil) ⇒ Object

Return the standard deviation of a column in an active record result set (or self)

column:  The column of which you want the standard deviation


105
106
107
108
# File 'lib/ar_result_calculations/ar_result_calculations.rb', line 105

def standard_deviation(column = nil)
  return nil unless variance = sample_variance(column)
  Math.sqrt(variance)
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