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



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

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

#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



32
33
34
# File 'lib/mongoid/contextual/none.rb', line 32

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



47
48
49
50
51
52
53
54
# File 'lib/mongoid/contextual/none.rb', line 47

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



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

def exists?; false; end

#lastnil

Always returns nil.

Examples:

Get the last document.

context.last

Returns:

  • (nil)

    Always nil.

Since:

  • 4.0.0



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

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



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

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



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

def pluck(*args)
  []
end