Class: Mongoid::Contextual::None

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

Methods included from Aggregable::None

#aggregates, #avg, #min

Constructor Details

#initialize(criteria) ⇒ None

Create the new null context.

Examples:

Create the new context.

Null.new(criteria)

Parameters:



104
105
106
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 104

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

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



14
15
16
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 14

def criteria
  @criteria
end

#klassObject (readonly)

Returns the value of attribute klass.



14
15
16
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 14

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.



44
45
46
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 44

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:

  • _field (String, Symbol)

    The name of the field.

Returns:

  • (Array)

    An empty Array.



56
57
58
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 56

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.



69
70
71
72
73
74
75
76
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 69

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

#exists?false

Do any documents exist for the context.

Examples:

Do any documents exist in the null context.

context.exists?

Returns:

  • (false)

    Always false.



84
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 84

def exists?; false; end

#lastnil

Always returns nil.

Examples:

Get the last document in null context.

context.last

Returns:

  • (nil)

    Always nil.



114
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 114

def last; nil; end

#lengthInteger Also known as: size

Always returns zero.

Examples:

Get the length of null context.

context.length

Returns:

  • (Integer)

    Always zero.



122
123
124
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 122

def length
  Mongoid.broken_aggregables ? 0 : entries.length
end

#new_sumObject



16
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 16

alias :new_sum :sum

#old_sumObject



9
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 9

alias :old_sum :sum

#pluck(*args) ⇒ Array

Pluck the field values in null context.

Examples:

Get the values for null context.

context.pluck(:name)

Parameters:

  • args (String, Symbol, Array)

    Field or fields to pluck.

Returns:

  • (Array)

    An empty Array.



94
95
96
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 94

def pluck(*args)
  []
end

#sum(_field = nil) ⇒ Integer | Symbol

Get the sum in the null context.

Examples:

Get the sum of null context.

context.sum(_field)

Parameters:

  • _field (Symbol) (defaults to: nil)

    The field to sum.

Returns:

  • (Integer | Symbol)

    If Mongoid.broken_aggregables is set to false, this will always be zero. Otherwise, it will return the field name as a symbol.



28
29
30
31
32
33
34
# File 'build/mongoid-7.4/lib/mongoid/contextual/none.rb', line 28

def sum(_field = nil)
  if Mongoid.broken_aggregables
    old_sum(_field)
  else
    new_sum(_field)
  end
end