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)



55
56
57
# File 'lib/mongoid/contexts/enumerable.rb', line 55

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

#countObject

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



24
25
26
# File 'lib/mongoid/contexts/enumerable.rb', line 24

def count
  @count ||= documents.size
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.



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

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.



43
44
45
46
# File 'lib/mongoid/contexts/enumerable.rb', line 43

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 }



65
66
67
# File 'lib/mongoid/contexts/enumerable.rb', line 65

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.



74
75
76
# File 'lib/mongoid/contexts/enumerable.rb', line 74

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.



83
84
85
# File 'lib/mongoid/contexts/enumerable.rb', line 83

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.



99
100
101
102
103
104
# File 'lib/mongoid/contexts/enumerable.rb', line 99

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