Module: DataMapper::Aggregates::Functions
- Includes:
- DataMapper::Assertions
- Included in:
- Collection, Model
- Defined in:
- lib/dm-aggregates/functions.rb
Instance Method Summary collapse
-
#aggregate(*args) ⇒ Array, ...
Perform aggregate queries.
-
#avg(*args) ⇒ Integer
Get the average value of a property.
-
#count(*args) ⇒ Integer
Count results (given the conditions).
-
#max(*args) ⇒ Integer
Get the highest value of a property.
-
#min(*args) ⇒ Integer
Get the lowest value of a property.
-
#sum(*args) ⇒ Integer
Get the total value of a property.
Instance Method Details
#aggregate(*args) ⇒ Array, ...
Perform aggregate queries
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/dm-aggregates/functions.rb', line 153 def aggregate(*args) query = args.last.kind_of?(Hash) ? args.pop : {} query[:fields] ||= [] query[:fields] |= args query[:fields].map! { |f| normalize_field(f) } raise ArgumentError, 'query[:fields] must not be empty' if query[:fields].empty? unless query.key?(:order) # the current collection/model is already sorted by attributes # and since we are projecting away some of the attributes, # and then performing aggregate functions on the remainder, # we need to honor the existing order, as if it were already # materialized, and we are looping over the rows in order. directions = direction_map query[:order] = [] # use the current query order for each property if available query[:fields].each do |property| next unless property.kind_of?(Property) query[:order] << directions.fetch(property, property) end end query = scoped_query(query) if query.fields.any? { |p| p.kind_of?(Property) } query.repository.aggregate(query.update(:unique => true)) else query.repository.aggregate(query).first # only return one row end end |
#avg(*args) ⇒ Integer
Get the average value of a property
103 104 105 106 107 108 109 110 |
# File 'lib/dm-aggregates/functions.rb', line 103 def avg(*args) query = args.last.kind_of?(Hash) ? args.pop : {} property_name = args.first assert_property_type property_name, ::Integer, ::Float, ::BigDecimal aggregate(query.merge(:fields => [ property_name.avg ])) end |
#count(*args) ⇒ Integer
Count results (given the conditions)
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dm-aggregates/functions.rb', line 32 def count(*args) query = args.last.kind_of?(Hash) ? args.pop : {} property_name = args.first if property_name assert_kind_of 'property', property_by_name(property_name), Property end aggregate(query.merge(:fields => [ property_name ? property_name.count : :all.count ])).to_i end |
#max(*args) ⇒ Integer
Get the highest value of a property
80 81 82 83 84 85 86 87 |
# File 'lib/dm-aggregates/functions.rb', line 80 def max(*args) query = args.last.kind_of?(Hash) ? args.pop : {} property_name = args.first assert_property_type property_name, ::Integer, ::Float, ::BigDecimal, ::DateTime, ::Date, ::Time aggregate(query.merge(:fields => [ property_name.max ])) end |
#min(*args) ⇒ Integer
Get the lowest value of a property
57 58 59 60 61 62 63 64 |
# File 'lib/dm-aggregates/functions.rb', line 57 def min(*args) query = args.last.kind_of?(Hash) ? args.pop : {} property_name = args.first assert_property_type property_name, ::Integer, ::Float, ::BigDecimal, ::DateTime, ::Date, ::Time aggregate(query.merge(:fields => [ property_name.min ])) end |
#sum(*args) ⇒ Integer
Get the total value of a property
126 127 128 129 130 131 132 133 |
# File 'lib/dm-aggregates/functions.rb', line 126 def sum(*args) query = args.last.kind_of?(::Hash) ? args.pop : {} property_name = args.first assert_property_type property_name, ::Integer, ::Float, ::BigDecimal aggregate(query.merge(:fields => [ property_name.sum ])) end |