Class: Mongoid::Contextual::None

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



137
138
139
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 137

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-master/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-master/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-master/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-master/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-master/lib/mongoid/contextual/none.rb', line 69

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.



93
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 93

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.



273
274
275
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 273

def fifth
  nil
end

#fifth!Object

Always raises an error.

Examples:

Get the fifth document in null context.

context.fifth!


283
284
285
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 283

def fifth!
  raise_document_not_found_error
end

#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.



149
150
151
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 149

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

#first!Object

Always raises an error.

Examples:

Get the first document in null context.

context.first!


159
160
161
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 159

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.



253
254
255
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 253

def fourth
  nil
end

#fourth!Object

Always raises an error.

Examples:

Get the fourth document in null context.

context.fourth!


263
264
265
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 263

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.



171
172
173
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 171

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

#last!Object

Always raises an error.

Examples:

Get the last document in null context.

context.last!


181
182
183
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 181

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:

  • (Integer)

    Always zero.



333
334
335
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 333

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

#new_sumObject



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

alias :new_sum :sum

#old_sumObject



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

alias :old_sum :sum

#pick(*_fields) ⇒ nil

Pick the field values in null context.

Examples:

Get the value for null context.

context.pick(:name)

Parameters:

  • *_fields ([ String | Symbol ]...)

    Field(s) to pick.

Returns:

  • (nil)

    Always return nil.



115
116
117
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 115

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:

  • *_fields ([ String | Symbol ]...)

    Field(s) to pluck.

Returns:

  • (Array)

    An empty Array.



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

def pluck(*_fields)
  []
end

#secondnil

Always returns nil.

Examples:

Get the second document in null context.

context.second

Returns:

  • (nil)

    Always nil.



213
214
215
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 213

def second
  nil
end

#second!Object

Always raises an error.

Examples:

Get the second document in null context.

context.second!


223
224
225
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 223

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.



293
294
295
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 293

def second_to_last
  nil
end

#second_to_last!Object

Always raises an error.

Examples:

Get the second to last document in null context.

context.second_to_last!


303
304
305
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 303

def second_to_last!
  raise_document_not_found_error
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-master/lib/mongoid/contextual/none.rb', line 28

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

#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.



193
194
195
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 193

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

#take!Object

Always raises an error.

Examples:

Take a document in null context.

context.take!


203
204
205
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 203

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:

  • _field (String | Symbol)

    Field to tally.

Returns:

  • (Hash)

    An empty Hash.



127
128
129
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 127

def tally(_field)
  {}
end

#thirdnil

Always returns nil.

Examples:

Get the third document in null context.

context.third

Returns:

  • (nil)

    Always nil.



233
234
235
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 233

def third
  nil
end

#third!Object

Always raises an error.

Examples:

Get the third document in null context.

context.third!


243
244
245
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 243

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.



313
314
315
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 313

def third_to_last
  nil
end

#third_to_last!Object

Always raises an error.

Examples:

Get the third to last document in null context.

context.third_to_last!


323
324
325
# File 'build/mongoid-master/lib/mongoid/contextual/none.rb', line 323

def third_to_last!
  raise_document_not_found_error
end