Class: Mongoid::Contexts::Enumerable

Inherits:
Object
  • Object
show all
Includes:
Ids, Paging
Defined in:
lib/mongoid/contexts/enumerable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ids

#id_criteria

Methods included from Paging

#page, #paginate, #per_page

Constructor Details

#initialize(criteria) ⇒ Enumerable

Create the new enumerable context. This will need the selector and options from a Criteria and a documents array that is the underlying array of embedded documents from a has many association.

Example:

Mongoid::Contexts::Enumerable.new(criteria)



79
80
81
# File 'lib/mongoid/contexts/enumerable.rb', line 79

def initialize(criteria)
  @criteria = criteria
end

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



6
7
8
# File 'lib/mongoid/contexts/enumerable.rb', line 6

def criteria
  @criteria
end

Instance Method Details

#aggregateObject

Return aggregation counts of the grouped documents. This will count by the first field provided in the fields array.

Returns:

A Hash with field values as keys, count as values



17
18
19
20
21
# File 'lib/mongoid/contexts/enumerable.rb', line 17

def aggregate
  counts = {}
  group.each_pair { |key, value| counts[key] = value.size }
  counts
end

#avg(field) ⇒ Object

Get the average value for the supplied field.

Example:

context.avg(:age)

Returns:

A numeric value that is the average.



32
33
34
35
# File 'lib/mongoid/contexts/enumerable.rb', line 32

def avg(field)
  total = sum(field)
  total ? (total.to_f / count) : nil
end

#countObject

Gets the number of documents in the array. Delegates to size.



38
39
40
# File 'lib/mongoid/contexts/enumerable.rb', line 38

def count
  @count ||= documents.size
end

#distinct(field) ⇒ Object

Gets an array of distinct values for the supplied field across the entire array or the susbset given the criteria.

Example:

context.distinct(:title)



48
49
50
# File 'lib/mongoid/contexts/enumerable.rb', line 48

def distinct(field)
  execute.collect { |doc| doc.send(field) }.uniq
end

#execute(paginating = false) ⇒ Object

Enumerable implementation of execute. Returns matching documents for the selector, and adds options if supplied.

Returns:

An Array of documents that matched the selector.



58
59
60
# File 'lib/mongoid/contexts/enumerable.rb', line 58

def execute(paginating = false)
  limit(documents.select { |document| document.matches?(selector) })
end

#groupObject

Groups the documents by the first field supplied in the field options.

Returns:

A Hash with field values as keys, arrays of documents as values.



67
68
69
70
# File 'lib/mongoid/contexts/enumerable.rb', line 67

def group
  field = options[:fields].first
  documents.group_by { |doc| doc.send(field) }
end

#iterate(&block) ⇒ Object

Iterate over each Document in the results. This can take an optional block to pass to each argument in the results.

Example:

context.iterate { |doc| p doc }



89
90
91
# File 'lib/mongoid/contexts/enumerable.rb', line 89

def iterate(&block)
  execute.each(&block)
end

#max(field) ⇒ Object

Get the largest value for the field in all the documents.

Returns:

The numerical largest value.



98
99
100
# File 'lib/mongoid/contexts/enumerable.rb', line 98

def max(field)
  determine(field, :>=)
end

#min(field) ⇒ Object

Get the smallest value for the field in all the documents.

Returns:

The numerical smallest value.



107
108
109
# File 'lib/mongoid/contexts/enumerable.rb', line 107

def min(field)
  determine(field, :<=)
end

#sum(field) ⇒ Object

Get the sum of the field values for all the documents.

Returns:

The numerical sum of all the document field values.



123
124
125
126
127
128
# File 'lib/mongoid/contexts/enumerable.rb', line 123

def sum(field)
  sum = documents.inject(nil) do |memo, doc|
    value = doc.send(field)
    memo ? memo += value : value
  end
end