Class: Mongoid::Contextual::None

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Queryable
Defined in:
lib/mongoid/contextual/none.rb

Instance Attribute Summary collapse

Attributes included from Queryable

#collection, #collection The collection to query against., #criteria The criteria for the context., #klass The klass for the criteria.

Instance Method Summary collapse

Methods included from Queryable

#blank?

Constructor Details

#initialize(criteria) ⇒ None

Create the new null context.

Examples:

Create the new context.

Null.new(criteria)

Parameters:

Since:

  • 4.0.0



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

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

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



8
9
10
# File 'lib/mongoid/contextual/none.rb', line 8

def criteria
  @criteria
end

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/mongoid/contextual/none.rb', line 8

def klass
  @klass
end

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.

Since:

  • 4.0.0



20
21
22
# File 'lib/mongoid/contextual/none.rb', line 20

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

#eachEnumerator

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

Examples:

Iterate over the context.

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

Returns:

  • (Enumerator)

    The enumerator.

Since:

  • 4.0.0



35
36
37
38
39
40
41
42
# File 'lib/mongoid/contextual/none.rb', line 35

def each
  if block_given?
    [].each { |doc| yield(doc) }
    self
  else
    to_enum
  end
end

#exists?true, false

Do any documents exist for the context.

Examples:

Do any documents exist for the context.

context.exists?

Returns:

  • (true, false)

    If the count is more than zero.

Since:

  • 4.0.0



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

def exists?; false; end

#lastnil

Always returns nil.

Examples:

Get the last document.

context.last

Returns:

  • (nil)

    Always nil.

Since:

  • 4.0.0



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

def last; nil; end

#lengthInteger Also known as: size

Always returns zero.

Examples:

Get the length of matching documents.

context.length

Returns:

  • (Integer)

    Always zero.

Since:

  • 4.0.0



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

def length
  entries.length
end

#pluck(*args) ⇒ Array

Allow pluck for null context.

Examples:

Allow pluck for null context.

context.pluck(:name)

Parameters:

  • field (String, Symbol, Array)

    or fields to pluck.

Returns:

  • (Array)

    Emtpy Array



63
64
65
# File 'lib/mongoid/contextual/none.rb', line 63

def pluck(*args)
  []
end