Module: CouchFoo::Calculations::ClassMethods
- Defined in:
- lib/couch_foo/calculations.rb
Instance Method Summary collapse
-
#average(attribute_name, options = {}) ⇒ Object
Calculates the average value on a given property.
-
#calculate(operation, attribute_name, options = {}) ⇒ Object
This calculates aggregate values in the given property.
-
#count(options = {}) ⇒ Object
Count operates using two different approaches.
-
#maximum(attribute_name, options = {}) ⇒ Object
Calculates the maximum value on a given property.
-
#minimum(attribute_name, options = {}) ⇒ Object
Calculates the minimum value on a given property.
-
#sum(attribute_name, options = {}) ⇒ Object
Calculates the sum of values on a given property.
Instance Method Details
#average(attribute_name, options = {}) ⇒ Object
Calculates the average value on a given property. The value is returned as a float. See calculate
for examples with options.
Person.average('age')
36 37 38 |
# File 'lib/couch_foo/calculations.rb', line 36 def average(attribute_name, = {}) calculate(:avg, attribute_name, ) end |
#calculate(operation, attribute_name, options = {}) ⇒ Object
This calculates aggregate values in the given property. Methods for count, sum, average, minimum, and maximum have been added as shortcuts. Options such as :conditions
, :order
, :count
and :distinct
can be passed to customize the query.
Options:
-
:conditions
: A conditions hash like { :user_name => username }. See conditions in the intro. -
:include
: Named associations that should be loaded at the same time. See eager loading under Associations. -
:order
: An element to order on, eg :order => :user_name -
:distinct
: Set this to true to remove repeating elements
Examples:
Person.calculate(:count, :all) # The same as Person.count
Person.average(:age) # Find the average of people
Person.minimum(:age, :conditions => {:last_name => 'Drake'}) # Finds the minimum age for everyone with a last name other than 'Drake'
Person.sum("2 * age")
78 79 80 81 82 83 84 |
# File 'lib/couch_foo/calculations.rb', line 78 def calculate(operation, attribute_name, = {}) (operation, ) catch :invalid_query do return execute_simple_calculation(operation, attribute_name, ) end 0 end |
#count(options = {}) ⇒ Object
Count operates using two different approaches.
-
Count all: By not passing any parameters to count, it will return a count of all the rows for the model.
-
Count using options will find the row count matched by the options used.
The second approach, count using options, accepts an option hash as the only parameter. The options are:
-
:conditions
: A conditions hash like { :user_name => username }. See conditions in the intro. -
:include
: Named associations that should be loaded at the same time. See eager loading under Associations. -
:order
: An element to order on, eg :order => :user_name -
:distinct
: Set this to true to remove repeating elements
Examples for counting all:
Person.count # returns the total count of all people
Examples for count with options:
Person.count(:conditions => {age = 26})
Note: Person.count(:all)
will not work because it will use :all
as the condition. Use Person.count instead.
29 30 31 |
# File 'lib/couch_foo/calculations.rb', line 29 def count( = {}) calculate(:count, nil, ) end |
#maximum(attribute_name, options = {}) ⇒ Object
Calculates the maximum value on a given property. The value is returned with the same data type of the property. See calculate
for examples with options.
Person.maximum('age')
50 51 52 |
# File 'lib/couch_foo/calculations.rb', line 50 def maximum(attribute_name, = {}) calculate(:max, attribute_name, ) end |
#minimum(attribute_name, options = {}) ⇒ Object
Calculates the minimum value on a given property. The value is returned with the same data type of the property. See calculate
for examples with options.
Person.minimum('age')
43 44 45 |
# File 'lib/couch_foo/calculations.rb', line 43 def minimum(attribute_name, = {}) calculate(:min, attribute_name, ) end |
#sum(attribute_name, options = {}) ⇒ Object
Calculates the sum of values on a given property. The value is returned with the same data type of the property. See calculate
for examples with options.
Person.sum('age')
57 58 59 |
# File 'lib/couch_foo/calculations.rb', line 57 def sum(attribute_name, = {}) calculate(:sum, attribute_name, ) end |