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

Included in:
Memory
Defined in:
build/mongoid-master/lib/mongoid/contextual/aggregable/memory.rb

Overview

Contains behavior for aggregating values in memory.

Instance Method Summary collapse

Instance Method Details

#aggregates(field) ⇒ Hash

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

Parameters:

  • field (String, Symbol)

    The field name.

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.



17
18
19
20
21
# File 'build/mongoid-master/lib/mongoid/contextual/aggregable/memory.rb', line 17

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

#avg(field) ⇒ Float

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:

  • (Float)

    The average.



31
32
33
# File 'build/mongoid-master/lib/mongoid/contextual/aggregable/memory.rb', line 31

def avg(field)
  count > 0 ? sum(field).to_f / count.to_f : nil
end

#max(field = nil) ⇒ Float, Document

Get the max value of the provided field. If provided a block, will return the 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:

  • (Float, Document)

    The max value or document with the max value.



51
52
53
# File 'build/mongoid-master/lib/mongoid/contextual/aggregable/memory.rb', line 51

def max(field = nil)
  block_given? ? super() : aggregate_by(field, :max_by)
end

#min(field = nil) ⇒ Float, Document

Get the min value of the provided field. If provided a block, will return the 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:

  • (Float, Document)

    The min value or document with the min value.



71
72
73
# File 'build/mongoid-master/lib/mongoid/contextual/aggregable/memory.rb', line 71

def min(field = nil)
  block_given? ? super() : aggregate_by(field, :min_by)
end

#sum(field = nil) ⇒ Float

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) (defaults to: nil)

    The field to sum.

Returns:

  • (Float)

    The sum value.



87
88
89
90
91
92
93
# File 'build/mongoid-master/lib/mongoid/contextual/aggregable/memory.rb', line 87

def sum(field = nil)
  if block_given?
    super()
  else
    count > 0 ? super(0) { |doc| doc.public_send(field) } : 0
  end
end