123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Contextual::None

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Queryable, Aggregable::None, Enumerable
Inherits: Object
Defined in: lib/mongoid/contextual/none.rb

Overview

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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Aggregable::None - Included

#aggregates

Get all the aggregate values for the provided field in null context.

#avg

Always returns nil.

#max
#min

Always returns nil.

#sum

Always returns zero.

Constructor Details

.new(criteria) ⇒ None

Create the new null context.

Examples:

Create the new context.

Null.new(criteria)

Parameters:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 120

def initialize(criteria)
  @criteria, @klass = criteria, criteria.klass
end

Instance Attribute Details

#criteria (readonly)

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 17

attr_reader :criteria, :klass

#klass (readonly)

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 17

attr_reader :criteria, :klass

Instance Method Details

#==(other) ⇒ true | false

Check if the context is equal to the other object.

Examples:

Check equality.

context == []

Parameters:

  • other (Array)

    The other array.

Returns:

  • (true | false)

    If the objects are equal.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 27

def ==(other)
  other.is_a?(None)
end

#distinct(_field) ⇒ Array

Get the distinct field values in null context.

Examples:

Get the distinct values in null context.

context.distinct(:name)

Parameters:

Returns:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 39

def distinct(_field)
  []
end

#eachEnumerator

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

Examples:

Iterate over the null context.

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

Returns:

  • (Enumerator)

    The enumerator.

[ GitHub ]

  
# 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) ⇒ false

Do any documents exist for the context.

Examples:

Do any documents exist in the null context.

context.exists?

Do any documents exist for given _id.

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

Do any documents exist for given conditions.

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

Parameters:

  • id_or_conditions (Hash | Object | false) (defaults to: :none)

    an _id to search for, a hash of conditions, nil or false.

Returns:

  • (false)

    Always false.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 76

def exists?(id_or_conditions = :none); false; end

#fifthnil

Always returns nil.

Examples:

Get the fifth document in null context.

context.fifth

Returns:

  • (nil)

    Always nil.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 256

def fifth
  nil
end

#fifth!

Always raises an error.

Examples:

Get the fifth document in null context.

context.fifth!

Raises:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 266

def fifth!
  raise_document_not_found_error
end

#find_first(limit = nil)

Alias for #first.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 321

alias :find_first :first

#first(limit = nil) ⇒ [] | nil Also known as: #find_first, #one

Always returns nil.

Examples:

Get the first document in null context.

context.first

Parameters:

  • limit (Integer) (defaults to: nil)

    The number of documents to return.

Returns:

  • ([] | nil)

    Empty array or nil.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 132

def first(limit = nil)
  [] unless limit.nil?
end

#first!

Always raises an error.

Examples:

Get the first document in null context.

context.first!

Raises:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 142

def first!
  raise_document_not_found_error
end

#fourthnil

Always returns nil.

Examples:

Get the fourth document in null context.

context.fourth

Returns:

  • (nil)

    Always nil.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 236

def fourth
  nil
end

#fourth!

Always raises an error.

Examples:

Get the fourth document in null context.

context.fourth!

Raises:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 246

def fourth!
  raise_document_not_found_error
end

#last(limit = nil) ⇒ [] | nil

Always returns nil.

Examples:

Get the last document in null context.

context.last

Parameters:

  • limit (Integer) (defaults to: nil)

    The number of documents to return.

Returns:

  • ([] | nil)

    Empty array or nil.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 154

def last(limit = nil)
  [] unless limit.nil?
end

#last!

Always raises an error.

Examples:

Get the last document in null context.

context.last!

Raises:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 164

def last!
  raise_document_not_found_error
end

#lengthInteger Also known as: #size

Always returns zero.

Examples:

Get the length of null context.

context.length

Returns:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 316

def length
  entries.length
end

#one(limit = nil)

Alias for #first.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 322

alias :one :first

#pick(*_fields) ⇒ nil

Pick the field values in null context.

Examples:

Get the value for null context.

context.pick(:name)

Parameters:

Returns:

  • (nil)

    Always return nil.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 98

def pick(*_fields)
  nil
end

#pluck(*_fields) ⇒ Array

Pluck the field values in null context.

Examples:

Get the values for null context.

context.pluck(:name)

Parameters:

Returns:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 86

def pluck(*_fields)
  []
end

#raise_document_not_found_error (private)

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 326

def raise_document_not_found_error
  raise Errors::DocumentNotFound.new(klass, nil, nil)
end

#secondnil

Always returns nil.

Examples:

Get the second document in null context.

context.second

Returns:

  • (nil)

    Always nil.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 196

def second
  nil
end

#second!

Always raises an error.

Examples:

Get the second document in null context.

context.second!

Raises:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 206

def second!
  raise_document_not_found_error
end

#second_to_lastnil

Always returns nil.

Examples:

Get the second to last document in null context.

context.second_to_last

Returns:

  • (nil)

    Always nil.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 276

def second_to_last
  nil
end

#second_to_last!

Always raises an error.

Examples:

Get the second to last document in null context.

context.second_to_last!

Raises:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 286

def second_to_last!
  raise_document_not_found_error
end

#size

Alias for #length.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 319

alias :size :length

#take(limit = nil) ⇒ [] | nil

Returns nil or empty array.

Examples:

Take a document in null context.

context.take

Parameters:

  • limit (Integer | nil) (defaults to: nil)

    The number of documents to take or nil.

Returns:

  • ([] | nil)

    Empty array or nil.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 176

def take(limit = nil)
  limit ? [] : nil
end

#take!

Always raises an error.

Examples:

Take a document in null context.

context.take!

Raises:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 186

def take!
  raise_document_not_found_error
end

#tally(_field) ⇒ Hash

Tally the field values in null context.

Examples:

Get the values for null context.

context.tally(:name)

Parameters:

Returns:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 110

def tally(_field)
  {}
end

#thirdnil

Always returns nil.

Examples:

Get the third document in null context.

context.third

Returns:

  • (nil)

    Always nil.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 216

def third
  nil
end

#third!

Always raises an error.

Examples:

Get the third document in null context.

context.third!

Raises:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 226

def third!
  raise_document_not_found_error
end

#third_to_lastnil

Always returns nil.

Examples:

Get the third to last document in null context.

context.third_to_last

Returns:

  • (nil)

    Always nil.

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 296

def third_to_last
  nil
end

#third_to_last!

Always raises an error.

Examples:

Get the third to last document in null context.

context.third_to_last!

Raises:

[ GitHub ]

  
# File 'lib/mongoid/contextual/none.rb', line 306

def third_to_last!
  raise_document_not_found_error
end