Parent

Included Modules

Class/Module Index [+]

Quicksearch

Mongoid::Contextual::Memory

Attributes

collection[R]

@attribute [r] collection The root collection. @attribute [r] criteria The criteria for the context. @attribute [r] klass The criteria class. @attribute [r] root The root document. @attribute [r] path The atomic path. @attribute [r] selector The root document selector. @attribute [r] matching The in memory documents that match the selector.

criteria[R]

@attribute [r] collection The root collection. @attribute [r] criteria The criteria for the context. @attribute [r] klass The criteria class. @attribute [r] root The root document. @attribute [r] path The atomic path. @attribute [r] selector The root document selector. @attribute [r] matching The in memory documents that match the selector.

documents[R]

@attribute [r] collection The root collection. @attribute [r] criteria The criteria for the context. @attribute [r] klass The criteria class. @attribute [r] root The root document. @attribute [r] path The atomic path. @attribute [r] selector The root document selector. @attribute [r] matching The in memory documents that match the selector.

klass[R]

@attribute [r] collection The root collection. @attribute [r] criteria The criteria for the context. @attribute [r] klass The criteria class. @attribute [r] root The root document. @attribute [r] path The atomic path. @attribute [r] selector The root document selector. @attribute [r] matching The in memory documents that match the selector.

path[R]

@attribute [r] collection The root collection. @attribute [r] criteria The criteria for the context. @attribute [r] klass The criteria class. @attribute [r] root The root document. @attribute [r] path The atomic path. @attribute [r] selector The root document selector. @attribute [r] matching The in memory documents that match the selector.

root[R]

@attribute [r] collection The root collection. @attribute [r] criteria The criteria for the context. @attribute [r] klass The criteria class. @attribute [r] root The root document. @attribute [r] path The atomic path. @attribute [r] selector The root document selector. @attribute [r] matching The in memory documents that match the selector.

selector[R]

@attribute [r] collection The root collection. @attribute [r] criteria The criteria for the context. @attribute [r] klass The criteria class. @attribute [r] root The root document. @attribute [r] path The atomic path. @attribute [r] selector The root document selector. @attribute [r] matching The in memory documents that match the selector.

Public Class Methods

new(criteria) click to toggle source

Create the new in memory context.

@example Create the new context.

Memory.new(criteria)

@param [ Criteria ] The criteria.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 160
def initialize(criteria)
  @criteria, @klass = criteria, criteria.klass
  @documents = criteria.documents.select do |doc|
    @root ||= doc._root
    @collection ||= root.collection
    doc.matches?(criteria.selector)
  end
  apply_sorting
  apply_options
end

Public Instance Methods

==(other) click to toggle source

Check if the context is equal to the other object.

@example Check equality.

context == []

@param [ Array ] other The other array.

@return [ true, false ] If the objects are equal.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 35
def ==(other)
  return false unless other.respond_to?(:entries)
  entries == other.entries
end
blank?() click to toggle source

Is the enumerable of matching documents empty?

@example Is the context empty?

context.blank?

@return [ true, false ] If the context is empty.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 48
def blank?
  count == 0
end
Also aliased as: empty?
delete() click to toggle source

Delete all documents in the database that match the selector.

@example Delete all the documents.

context.delete

@return [ nil ] Nil.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 61
def delete
  deleted = count
  removed = map do |doc|
    prepare_remove(doc)
    doc.as_document
  end
  unless removed.empty?
    collection.find(selector).update("$pullAll" => { path => removed })
  end
  deleted
end
Also aliased as: delete_all
delete_all() click to toggle source
Alias for: delete
destroy() click to toggle source

Destroy all documents in the database that match the selector.

@example Destroy all the documents.

context.destroy

@return [ nil ] Nil.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 82
def destroy
  deleted = count
  each do |doc|
    documents.delete_one(doc)
    doc.destroy
  end
  deleted
end
Also aliased as: destroy_all
destroy_all() click to toggle source
Alias for: destroy
distinct(field) click to toggle source

Get the distinct values in the db for the provided field.

@example Get the distinct values.

context.distinct(:name)

@param [ String, Symbol ] field The name of the field.

@return [ Array<Object> ] The distinct values for the field.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 102
def distinct(field)
  documents.map{ |doc| doc.send(field) }.uniq
end
each() click to toggle source

Iterate over the context. If provided a block, yield to a Mongoid document for each, otherwise return an enum.

@example Iterate over the context.

context.each do |doc|
  puts doc.name
end

@return [ Enumerator ] The enumerator.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 117
def each
  if block_given?
    (documents[skipping || 0, limiting || documents.length] || []).each do |doc|
      yield doc
    end
  else
    to_enum
  end
end
empty?() click to toggle source
Alias for: blank?
exists?() click to toggle source

Do any documents exist for the context.

@example Do any documents exist for the context.

context.exists?

@return [ true, false ] If the count is more than zero.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 135
def exists?
  count > 0
end
first() click to toggle source

Get the first document in the database for the criteria's selector.

@example Get the first document.

context.first

@return [ Document ] The first document.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 147
def first
  documents.first
end
Also aliased as: one
last() click to toggle source

Get the last document in the database for the criteria's selector.

@example Get the last document.

context.last

@return [ Document ] The last document.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 179
def last
  documents.last
end
length() click to toggle source

Get the length of matching documents in the context.

@example Get the length of matching documents.

context.length

@return [ Integer ] The matching length.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 191
def length
  documents.length
end
Also aliased as: size
limit(value) click to toggle source

Limits the number of documents that are returned.

@example Limit the documents.

context.limit(20)

@param [ Integer ] value The number of documents to return.

@return [ Mongo ] The context.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 206
def limit(value)
  self.limiting = value
  self
end
one() click to toggle source
Alias for: first
size() click to toggle source
Alias for: length
skip(value) click to toggle source

Skips the provided number of documents.

@example Skip the documents.

context.skip(20)

@param [ Integer ] value The number of documents to skip.

@return [ Mongo ] The context.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 221
def skip(value)
  self.skipping = value
  self
end
sort(values) click to toggle source

Sorts the documents by the provided spec.

@example Sort the documents.

context.sort(name: -1, title: 1)

@param [ Hash ] values The sorting values as field/direction(1/-1)

pairs.

@return [ Mongo ] The context.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 237
def sort(values)
  in_place_sort(values) and self
end
update(attributes = nil) click to toggle source

Update the first matching document atomically.

@example Update the matching document.

context.update(name: "Smiths")

@param [ Hash ] attributes The new attributes for the document.

@return [ nil, false ] False if no attributes were provided.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 251
def update(attributes = nil)
  update_documents(attributes, [ first ])
end
update_all(attributes = nil) click to toggle source

Update all the matching documents atomically.

@example Update all the matching documents.

context.update_all(name: "Smiths")

@param [ Hash ] attributes The new attributes for each document.

@return [ nil, false ] False if no attributes were provided.

@since 3.0.0

# File lib/mongoid/contextual/memory.rb, line 265
def update_all(attributes = nil)
  update_documents(attributes, entries)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.