123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Contextual::Aggregable::Memory

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongoid/contextual/aggregable/memory.rb

Overview

Contains behavior for aggregating values in memory.

Instance Method Summary

Instance Method Details

#aggregate_by(field, method) ⇒ Numeric | nil (private)

This method is for internal use only.

Aggregate by the provided field and method.

Examples:

Aggregate by the field and method.

aggregable.aggregate_by(:likes, :min_by)

Parameters:

  • field (Symbol)

    The field to aggregate on.

  • method (Symbol)

    The method (min_by or max_by).

Returns:

  • (Numeric | nil)

    The aggregate.

[ GitHub ]

  
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 116

def aggregate_by(field, method)
  return nil unless any?

  map { |doc| doc.public_send(field) }.compact.public_send(method)
end

#aggregates(field) ⇒ Hash

Get all the aggregate values for the provided field. Provided for interface consistency with Mongo.

Parameters:

Returns:

  • (Hash)

    A Hash containing the aggregate values. If no documents are present, then returned ::Hash will have count, sum of 0 and max, min, avg of nil.

[ GitHub ]

  
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 18

def aggregates(field)
  %w(count sum avg min max).each_with_object({}) do |method, hash|
    hash[method] = send(method, field)
  end
end

#avg(field) ⇒ Numeric

Get the average value of the provided field.

Examples:

Get the average of a single field.

aggregable.avg(:likes)

Parameters:

  • field (Symbol)

    The field to average.

Returns:

  • (Numeric)

    The average.

[ GitHub ]

  
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 32

def avg(field)
  total = count { |doc| !doc.send(field).nil? }
  return nil unless total > 0

  total = total.to_f if total.is_a?(Integer)
  sum(field) / total
end

#max(field = nil) ⇒ Numeric | Document

Get the max value of the provided field. If provided a block, will return the ::Mongoid::Document with the greatest value for the field, in accordance with Ruby’s enumerable API.

Examples:

Get the max of a single field.

aggregable.max(:likes)

Get the document with the max value.

aggregable.max do |a, b|
  a.likes <=> b.likes
end

Parameters:

  • field (Symbol) (defaults to: nil)

    The field to max.

Returns:

  • (Numeric | Document)

    The max value or document with the max value.

[ GitHub ]

  
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 56

def max(field = nil)
  return super() if block_given?

  aggregate_by(field, :max)
end

#min(field = nil) ⇒ Numeric | Document

Get the min value of the provided field. If provided a block, will return the ::Mongoid::Document with the smallest value for the field, in accordance with Ruby’s enumerable API.

Examples:

Get the min of a single field.

aggregable.min(:likes)

Get the document with the min value.

aggregable.min do |a, b|
  a.likes <=> b.likes
end

Parameters:

  • field (Symbol) (defaults to: nil)

    The field to min.

Returns:

  • (Numeric | Document)

    The min value or document with the min value.

[ GitHub ]

  
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 78

def min(field = nil)
  return super() if block_given?

  aggregate_by(field, :min)
end

#sum(field = nil) ⇒ Numeric

Get the sum value of the provided field. If provided a block, will return the sum in accordance with Ruby’s enumerable API.

Examples:

Get the sum of a single field.

aggregable.sum(:likes)

Get the sum for the provided block.

aggregable.sum(&:likes)

Parameters:

  • field (Symbol | Numeric) (defaults to: nil)

    The field to sum, or the intial value of the sum when a block is given.

Returns:

  • (Numeric)

    The sum value.

[ GitHub ]

  
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 97

def sum(field = nil)
  return super(field || 0) if block_given?

  aggregate_by(field, :sum) || 0
end