Class: Mongoid::Contextual::None

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Queryable
Defined in:
build/mongoid-7.3/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



89
90
91
# File 'build/mongoid-7.3/lib/mongoid/contextual/none.rb', line 89

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

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



10
11
12
# File 'build/mongoid-7.3/lib/mongoid/contextual/none.rb', line 10

def criteria
  @criteria
end

#klassObject (readonly)

Returns the value of attribute klass.



10
11
12
# File 'build/mongoid-7.3/lib/mongoid/contextual/none.rb', line 10

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



22
23
24
# File 'build/mongoid-7.3/lib/mongoid/contextual/none.rb', line 22

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

#distinct(field) ⇒ Array

Allow distinct for null context.

Examples:

Get the distinct values.

context.distinct(:name)

Parameters:

  • field (String, Symbol)

    the name of the field.

Returns:

  • (Array)

    Empty Array



34
35
36
# File 'build/mongoid-7.3/lib/mongoid/contextual/none.rb', line 34

def distinct(field)
  []
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



49
50
51
52
53
54
55
56
# File 'build/mongoid-7.3/lib/mongoid/contextual/none.rb', line 49

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



66
# File 'build/mongoid-7.3/lib/mongoid/contextual/none.rb', line 66

def exists?; false; end

#lastnil

Always returns nil.

Examples:

Get the last document.

context.last

Returns:

  • (nil)

    Always nil.

Since:

  • 4.0.0



101
# File 'build/mongoid-7.3/lib/mongoid/contextual/none.rb', line 101

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



111
112
113
# File 'build/mongoid-7.3/lib/mongoid/contextual/none.rb', line 111

def length
  entries.length
end

#pluck(*args) ⇒ Array

Allow pluck for null context.

Examples:

Allow pluck for null context.

context.pluck(:name)

Parameters:

  • args (String, Symbol, Array)

    Field or fields to pluck.

Returns:

  • (Array)

    Emtpy Array



77
78
79
# File 'build/mongoid-7.3/lib/mongoid/contextual/none.rb', line 77

def pluck(*args)
  []
end