class Mongoid::Contextual::None

Context object used for performing bulk query and persistence operations on a null set. The method interface of this class is consistent with Mongoid::Contextual::Mongo.

Attributes

criteria[R]
klass[R]

Public Class Methods

new(criteria) click to toggle source

Create the new null context.

@example Create the new context.

Null.new(criteria)

@param [ Criteria ] criteria The criteria.

# File lib/mongoid/contextual/none.rb, line 120
def initialize(criteria)
  @criteria, @klass = criteria, criteria.klass
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.

# File lib/mongoid/contextual/none.rb, line 27
def ==(other)
  other.is_a?(None)
end
distinct(_field) click to toggle source

Get the distinct field values in null context.

@example Get the distinct values in null context.

context.distinct(:name)

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

@return [ Array ] An empty Array.

# File lib/mongoid/contextual/none.rb, line 39
def distinct(_field)
  []
end
each() { |doc| ... } click to toggle source

Iterate over the null context. There are no documents to iterate over in this case.

@example Iterate over the null context.

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

@return [ Enumerator ] The enumerator.

# File lib/mongoid/contextual/none.rb, line 52
def each
  if block_given?
    [].each { |doc| yield(doc) }
    self
  else
    to_enum
  end
end
exists?(id_or_conditions = :none) click to toggle source

Do any documents exist for the context.

@example Do any documents exist in the null context.

context.exists?

@example Do any documents exist for given _id.

context.exists?(BSON::ObjectId(...))

@example Do any documents exist for given conditions.

context.exists?(name: "...")

@param [ Hash | Object | false ] id_or_conditions an _id to

search for, a hash of conditions, nil or false.

@return [ false ] Always false.

# File lib/mongoid/contextual/none.rb, line 76
def exists?(id_or_conditions = :none); false; end
fifth() click to toggle source

Always returns nil.

@example Get the fifth document in null context.

context.fifth

@return [ nil ] Always nil.

# File lib/mongoid/contextual/none.rb, line 256
def fifth
  nil
end
fifth!() click to toggle source

Always raises an error.

@example Get the fifth document in null context.

context.fifth!

@raise [ Mongoid::Errors::DocumentNotFound ] always raises.

# File lib/mongoid/contextual/none.rb, line 266
def fifth!
  raise_document_not_found_error
end
find_first(limit = nil)
Alias for: first
first(limit = nil) click to toggle source

Always returns nil.

@example Get the first document in null context.

context.first

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

@return [ [] | nil ] Empty array or nil.

# File lib/mongoid/contextual/none.rb, line 132
def first(limit = nil)
  [] unless limit.nil?
end
Also aliased as: find_first, one
first!() click to toggle source

Always raises an error.

@example Get the first document in null context.

context.first!

@raise [ Mongoid::Errors::DocumentNotFound ] always raises.

# File lib/mongoid/contextual/none.rb, line 142
def first!
  raise_document_not_found_error
end
fourth() click to toggle source

Always returns nil.

@example Get the fourth document in null context.

context.fourth

@return [ nil ] Always nil.

# File lib/mongoid/contextual/none.rb, line 236
def fourth
  nil
end
fourth!() click to toggle source

Always raises an error.

@example Get the fourth document in null context.

context.fourth!

@raise [ Mongoid::Errors::DocumentNotFound ] always raises.

# File lib/mongoid/contextual/none.rb, line 246
def fourth!
  raise_document_not_found_error
end
last(limit = nil) click to toggle source

Always returns nil.

@example Get the last document in null context.

context.last

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

@return [ [] | nil ] Empty array or nil.

# File lib/mongoid/contextual/none.rb, line 154
def last(limit = nil)
  [] unless limit.nil?
end
last!() click to toggle source

Always raises an error.

@example Get the last document in null context.

context.last!

@raise [ Mongoid::Errors::DocumentNotFound ] always raises.

# File lib/mongoid/contextual/none.rb, line 164
def last!
  raise_document_not_found_error
end
length() click to toggle source

Always returns zero.

@example Get the length of null context.

context.length

@return [ Integer ] Always zero.

# File lib/mongoid/contextual/none.rb, line 316
def length
  entries.length
end
Also aliased as: size
one(limit = nil)
Alias for: first
pick(*_fields) click to toggle source

Pick the field values in null context.

@example Get the value for null context.

context.pick(:name)

@param [ [ String | Symbol ]… ] *_fields Field(s) to pick.

@return [ nil ] Always return nil.

# File lib/mongoid/contextual/none.rb, line 98
def pick(*_fields)
  nil
end
pluck(*_fields) click to toggle source

Pluck the field values in null context.

@example Get the values for null context.

context.pluck(:name)

@param [ [ String | Symbol ]… ] *_fields Field(s) to pluck.

@return [ Array ] An empty Array.

# File lib/mongoid/contextual/none.rb, line 86
def pluck(*_fields)
  []
end
second() click to toggle source

Always returns nil.

@example Get the second document in null context.

context.second

@return [ nil ] Always nil.

# File lib/mongoid/contextual/none.rb, line 196
def second
  nil
end
second!() click to toggle source

Always raises an error.

@example Get the second document in null context.

context.second!

@raise [ Mongoid::Errors::DocumentNotFound ] always raises.

# File lib/mongoid/contextual/none.rb, line 206
def second!
  raise_document_not_found_error
end
second_to_last() click to toggle source

Always returns nil.

@example Get the second to last document in null context.

context.second_to_last

@return [ nil ] Always nil.

# File lib/mongoid/contextual/none.rb, line 276
def second_to_last
  nil
end
second_to_last!() click to toggle source

Always raises an error.

@example Get the second to last document in null context.

context.second_to_last!

@raise [ Mongoid::Errors::DocumentNotFound ] always raises.

# File lib/mongoid/contextual/none.rb, line 286
def second_to_last!
  raise_document_not_found_error
end
size()
Alias for: length
take(limit = nil) click to toggle source

Returns nil or empty array.

@example Take a document in null context.

context.take

@param [ Integer | nil ] limit The number of documents to take or nil.

@return [ [] | nil ] Empty array or nil.

# File lib/mongoid/contextual/none.rb, line 176
def take(limit = nil)
  limit ? [] : nil
end
take!() click to toggle source

Always raises an error.

@example Take a document in null context.

context.take!

@raise [ Mongoid::Errors::DocumentNotFound ] always raises.

# File lib/mongoid/contextual/none.rb, line 186
def take!
  raise_document_not_found_error
end
tally(_field) click to toggle source

Tally the field values in null context.

@example Get the values for null context.

context.tally(:name)

@param [ String | Symbol ] _field Field to tally.

@return [ Hash ] An empty Hash.

# File lib/mongoid/contextual/none.rb, line 110
def tally(_field)
  {}
end
third() click to toggle source

Always returns nil.

@example Get the third document in null context.

context.third

@return [ nil ] Always nil.

# File lib/mongoid/contextual/none.rb, line 216
def third
  nil
end
third!() click to toggle source

Always raises an error.

@example Get the third document in null context.

context.third!

@raise [ Mongoid::Errors::DocumentNotFound ] always raises.

# File lib/mongoid/contextual/none.rb, line 226
def third!
  raise_document_not_found_error
end
third_to_last() click to toggle source

Always returns nil.

@example Get the third to last document in null context.

context.third_to_last

@return [ nil ] Always nil.

# File lib/mongoid/contextual/none.rb, line 296
def third_to_last
  nil
end
third_to_last!() click to toggle source

Always raises an error.

@example Get the third to last document in null context.

context.third_to_last!

@raise [ Mongoid::Errors::DocumentNotFound ] always raises.

# File lib/mongoid/contextual/none.rb, line 306
def third_to_last!
  raise_document_not_found_error
end

Private Instance Methods

raise_document_not_found_error() click to toggle source
# File lib/mongoid/contextual/none.rb, line 326
def raise_document_not_found_error
  raise Errors::DocumentNotFound.new(klass, nil, nil)
end