Parent

Class/Module Index [+]

Quicksearch

Mongoid::Contexts::Enumerable

Attributes

collection[RW]
criteria[RW]

Public Instance Methods

aggregate() click to toggle source

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

@example Aggregate on a field.

person.addresses.only(:street).aggregate

@return [ Hash ] Field values as keys, count as values

# File lib/mongoid/contexts/enumerable.rb, line 20
def aggregate
  {}.tap do |counts|
    group.each_pair { |key, value| counts[key] = value.size }
  end
end
avg(field) click to toggle source

Get the average value for the supplied field.

@example Get the average.

context.avg(:age)

@return [ Numeric ] A numeric value that is the average.

# File lib/mongoid/contexts/enumerable.rb, line 32
def avg(field)
  total = sum(field)
  total ? (total.to_f / count) : nil
end
count() click to toggle source

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

@example Get the count.

context.count

@return [ Integer ] The count of documents.

# File lib/mongoid/contexts/enumerable.rb, line 43
def count
  @count ||= execute.size
end
Also aliased as: length, size
delete() click to toggle source
Alias for: delete_all
delete_all() click to toggle source

Delete all the documents in the database matching the selector.

@example Delete the documents.

context.delete_all

@return [ Integer ] The number of documents deleted.

@since 2.0.0.rc.1

# File lib/mongoid/contexts/enumerable.rb, line 57
def delete_all
  atomically(:$pull) do
    set_collection
    count.tap do
      filter.each { |doc| doc.delete }
    end
  end
end
Also aliased as: delete
destroy() click to toggle source
Alias for: destroy_all
destroy_all() click to toggle source

Destroy all the documents in the database matching the selector.

@example Destroy the documents.

context.destroy_all

@return [ Integer ] The number of documents destroyed.

@since 2.0.0.rc.1

# File lib/mongoid/contexts/enumerable.rb, line 75
def destroy_all
  atomically(:$pull) do
    set_collection
    count.tap do
      filter.each { |doc| doc.destroy }
    end
  end
end
Also aliased as: destroy
distinct(field) click to toggle source

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

@example Get the list of distinct values.

context.distinct(:title)

@return [ Array<String> ] The distinct values.

# File lib/mongoid/contexts/enumerable.rb, line 92
def distinct(field)
  execute.collect { |doc| doc.send(field) }.uniq
end
execute() click to toggle source

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

@example Execute the context.

context.execute

@return [ Array<Document> ] Documents that matched the selector.

# File lib/mongoid/contexts/enumerable.rb, line 103
def execute
  limit(sort(filter)) || []
end
group() click to toggle source

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

@example Group the context.

context.group

@return [ Hash ] Field values as keys, arrays of documents as values.

# File lib/mongoid/contexts/enumerable.rb, line 113
def group
  field = field_list.first
  execute.group_by { |doc| doc.send(field) }
end
iterate(&block) click to toggle source

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

@example Iterate over the documents.

context.iterate { |doc| p doc }
# File lib/mongoid/contexts/enumerable.rb, line 135
def iterate(&block)
  execute.each(&block)
end
length() click to toggle source
Alias for: count
max(field) click to toggle source

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

@example Get the max value.

context.max(:age)

@return [ Numeric ] The numerical largest value.

# File lib/mongoid/contexts/enumerable.rb, line 145
def max(field)
  determine(field, :>=)
end
min(field) click to toggle source

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

@example Get the minimum value.

context.min(:age)

@return [ Numeric ] The numerical smallest value.

# File lib/mongoid/contexts/enumerable.rb, line 155
def min(field)
  determine(field, :<=)
end
shift() click to toggle source

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

@example Shift the documents.

context.shift

@return [ Document ] The first document in the array.

# File lib/mongoid/contexts/enumerable.rb, line 174
def shift
  first.tap do |document|
    self.criteria = criteria.skip((options[:skip] || 0) + 1)
  end
end
size() click to toggle source
Alias for: count
sum(field) click to toggle source

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

@example Get the sum of the field.

context.sum(:cost)

@return [ Numeric ] The numerical sum of all the document field values.

# File lib/mongoid/contexts/enumerable.rb, line 186
def sum(field)
  sum = execute.inject(nil) do |memo, doc|
    value = doc.send(field) || 0
    memo ? memo += value : value
  end
end
update(attributes = nil) click to toggle source
Alias for: update_all
update_all(attributes = nil) click to toggle source

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.

@example Update all matching documents.

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

@param [ Hash ] attributes The sets to perform.

@since 2.0.0.rc.6

# File lib/mongoid/contexts/enumerable.rb, line 203
def update_all(attributes = nil)
  iterate do |doc|
    doc.update_attributes(attributes || {})
  end
end
Also aliased as: update

Protected Instance Methods

determine(field, operator) click to toggle source

If the field exists, perform the comparison and set if true.

@example Compare.

context.determine

@return [ Array<Document> ] The matching documents.

# File lib/mongoid/contexts/enumerable.rb, line 228
def determine(field, operator)
  matching = documents.inject(nil) do |memo, doc|
    value = doc.send(field) || 0
    (memo && memo.send(operator, value)) ? memo : value
  end
end
filter() click to toggle source

Filters the documents against the criteria’s selector

@example Filter the documents.

context.filter

@return [ Array ] The documents filtered.

# File lib/mongoid/contexts/enumerable.rb, line 218
def filter
  documents.select { |document| document.matches?(selector) }
end
limit(documents) click to toggle source

Limits the result set if skip and limit options.

@example Limit the results.

context.limit(documents)

@return [ Array<Document> ] The limited documents.

# File lib/mongoid/contexts/enumerable.rb, line 241
def limit(documents)
  skip, limit = options[:skip], options[:limit]
  if skip && limit
    return documents.slice(skip, limit)
  elsif limit
    return documents.first(limit)
  elsif skip
    return documents.slice(skip..-1)
  end
  documents
end
root() click to toggle source
# File lib/mongoid/contexts/enumerable.rb, line 253
def root
  @root ||= documents.first.try(:_root)
end
root_class() click to toggle source
# File lib/mongoid/contexts/enumerable.rb, line 257
def root_class
  @root_class ||= root ? root.class : nil
end
set_collection() click to toggle source

Set the collection to the collection of the root document.

@example Set the collection.

context.set_collection

@return [ Collection ] The root collection.

# File lib/mongoid/contexts/enumerable.rb, line 267
def set_collection
  @collection = root.collection if root && !root.embedded?
end
sort(documents) click to toggle source

Sorts the result set if sort options have been set.

@example Sort the documents.

context.sort(documents)

@return [ Array<Document> ] The sorted documents.

# File lib/mongoid/contexts/enumerable.rb, line 277
def sort(documents)
  return documents if options[:sort].blank?
  documents.sort_by do |document|
    options[:sort].map do |key, direction|
      Sort.new(document.read_attribute(key), direction)
    end
  end
end

Public Class Methods

new(criteria) click to toggle source

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 Create a new context.

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

@param [ Criteria ] criteria The criteria for the context.

# File lib/mongoid/contexts/enumerable.rb, line 126
def initialize(criteria)
  @criteria = criteria
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.