Class: Mongoid::Contexts::Enumerable

Inherits:
Object
  • Object
show all
Includes:
Relations::Embedded::Atomic
Defined in:
lib/mongoid/contexts/enumerable.rb,
lib/mongoid/contexts/enumerable/sort.rb

Defined Under Namespace

Classes: Sort

Constant Summary

Constants included from Relations::Embedded::Atomic

Relations::Embedded::Atomic::MODIFIERS

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Examples:

Create a new context.

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

Parameters:

  • criteria (Criteria)

    The criteria for the context.



121
122
123
# File 'lib/mongoid/contexts/enumerable.rb', line 121

def initialize(criteria)
  @criteria = criteria
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



9
10
11
# File 'lib/mongoid/contexts/enumerable.rb', line 9

def collection
  @collection
end

#criteriaObject

Returns the value of attribute criteria.



9
10
11
# File 'lib/mongoid/contexts/enumerable.rb', line 9

def criteria
  @criteria
end

Instance Method Details

#aggregateHash

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

Examples:

Aggregate on a field.

person.addresses.only(:street).aggregate

Returns:

  • (Hash)

    Field values as keys, count as values



21
22
23
24
25
# File 'lib/mongoid/contexts/enumerable.rb', line 21

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

#avg(field) ⇒ Numeric

Get the average value for the supplied field.

Examples:

Get the average.

context.avg(:age)

Returns:

  • (Numeric)

    A numeric value that is the average.



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

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

#countInteger

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

Examples:

Get the count.

context.count

Returns:

  • (Integer)

    The count of documents.



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

def count
  @count ||= filter.size
end

#delete_allInteger Also known as: delete

Delete all the documents in the database matching the selector.

Examples:

Delete the documents.

context.delete_all

Returns:

  • (Integer)

    The number of documents deleted.

Since:

  • 2.0.0.rc.1



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

def delete_all
  atomically(:$pull) do
    set_collection
    count.tap { filter.each(&:delete) }
  end
end

#destroy_allInteger Also known as: destroy

Destroy all the documents in the database matching the selector.

Examples:

Destroy the documents.

context.destroy_all

Returns:

  • (Integer)

    The number of documents destroyed.

Since:

  • 2.0.0.rc.1



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

def destroy_all
  atomically(:$pull) do
    set_collection
    count.tap { filter.each(&:destroy) }
  end
end

#distinct(field) ⇒ Array<String>

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

Examples:

Get the list of distinct values.

context.distinct(:title)

Returns:



87
88
89
# File 'lib/mongoid/contexts/enumerable.rb', line 87

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

#executeArray<Document>

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

Examples:

Execute the context.

context.execute

Returns:



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

def execute
  limit(sort(filter)) || []
end

#groupHash

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

Examples:

Group the context.

context.group

Returns:

  • (Hash)

    Field values as keys, arrays of documents as values.



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

def group
  field = field_list.first
  execute.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.

Examples:

Iterate over the documents.

context.iterate { |doc| p doc }


130
131
132
# File 'lib/mongoid/contexts/enumerable.rb', line 130

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

#max(field) ⇒ Numeric

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

Examples:

Get the max value.

context.max(:age)

Returns:

  • (Numeric)

    The numerical largest value.



140
141
142
# File 'lib/mongoid/contexts/enumerable.rb', line 140

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

#min(field) ⇒ Numeric

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

Examples:

Get the minimum value.

context.min(:age)

Returns:

  • (Numeric)

    The numerical smallest value.



150
151
152
# File 'lib/mongoid/contexts/enumerable.rb', line 150

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

#shiftDocument

Get one document and tell the criteria to skip this record on successive calls.

Examples:

Shift the documents.

context.shift

Returns:

  • (Document)

    The first document in the array.



169
170
171
172
173
# File 'lib/mongoid/contexts/enumerable.rb', line 169

def shift
  first.tap do |document|
    self.criteria = criteria.skip((options[:skip] || 0) + 1)
  end
end

#sum(field) ⇒ Numeric

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

Examples:

Get the sum of the field.

context.sum(:cost)

Returns:

  • (Numeric)

    The numerical sum of all the document field values.



181
182
183
184
185
186
# File 'lib/mongoid/contexts/enumerable.rb', line 181

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

#update_all(attributes = nil) ⇒ Object Also known as: update

Very basic update that will perform a simple atomic $set of the attributes provided in the hash. Can be expanded to later for more robust functionality.

Examples:

Update all matching documents.

context.update_all(:title => "Sir")

Parameters:

  • attributes (Hash) (defaults to: nil)

    The sets to perform.

Since:

  • 2.0.0.rc.6



198
199
200
201
202
# File 'lib/mongoid/contexts/enumerable.rb', line 198

def update_all(attributes = nil)
  iterate do |doc|
    doc.update_attributes(attributes || {})
  end
end