Class: Mongoid::Contextual::Memory

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Association::EagerLoadable, Aggregable::Memory, Queryable, Positional
Defined in:
build/mongoid-master/lib/mongoid/contextual/memory.rb

Instance Attribute Summary collapse

Attributes included from Queryable

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

Instance Method Summary collapse

Methods included from Positional

#positionally

Methods included from Queryable

#blank?

Methods included from Association::EagerLoadable

#eager_load, #eager_loadable?, #preload

Methods included from Aggregable::Memory

#aggregates, #avg, #max, #min, #sum

Constructor Details

#initialize(criteria) ⇒ Memory

Create the new in memory context.

Examples:

Create the new context.

Memory.new(criteria)

Parameters:



156
157
158
159
160
161
162
163
164
165
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 156

def initialize(criteria)
  @criteria, @klass = criteria, criteria.klass
  @documents = criteria.documents.select do |doc|
    @root ||= doc._root
    @collection ||= root.collection
    doc._matches?(criteria.selector)
  end
  apply_sorting
  apply_options
end

Instance Attribute Details

#documentsObject (readonly)

Returns the value of attribute documents.



19
20
21
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 19

def documents
  @documents
end

#matching The in memory documents that match the selector.(The) ⇒ Object (readonly)



19
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 19

attr_reader :documents, :path, :root, :selector

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 19

def path
  @path
end

#path The atomic path.(Theatomicpath.) ⇒ Object (readonly)



19
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 19

attr_reader :documents, :path, :root, :selector

#rootObject (readonly)

Returns the value of attribute root.



19
20
21
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 19

def root
  @root
end

#root The root document.(Therootdocument.) ⇒ Object (readonly)



19
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 19

attr_reader :documents, :path, :root, :selector

#selectorObject (readonly)

Returns the value of attribute selector.



19
20
21
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 19

def selector
  @selector
end

#selector The root document selector.(Therootdocumentselector.) ⇒ Object (readonly)



19
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 19

attr_reader :documents, :path, :root, :selector

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.



29
30
31
32
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 29

def ==(other)
  return false unless other.respond_to?(:entries)
  entries == other.entries
end

#deletenil Also known as: delete_all

Delete all documents in the database that match the selector.

Examples:

Delete all the documents.

context.delete

Returns:

  • (nil)

    Nil.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 40

def delete
  deleted = count
  removed = map do |doc|
    prepare_remove(doc)
    doc.send(:as_attributes)
  end
  unless removed.empty?
    collection.find(selector).update_one(
      positionally(selector, "$pullAll" => { path => removed }),
      session: _session
    )
  end
  deleted
end

#destroynil Also known as: destroy_all

Destroy all documents in the database that match the selector.

Examples:

Destroy all the documents.

context.destroy

Returns:

  • (nil)

    Nil.



62
63
64
65
66
67
68
69
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 62

def destroy
  deleted = count
  each do |doc|
    documents.delete_one(doc)
    doc.destroy
  end
  deleted
end

#distinct(field) ⇒ Array<Object>

Get the distinct values in the db for the provided field.

Examples:

Get the distinct values.

context.distinct(:name)

Parameters:

  • field (String | Symbol)

    The name of the field.

Returns:

  • (Array<Object>)

    The distinct values for the field.



80
81
82
83
84
85
86
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 80

def distinct(field)
  if Mongoid.legacy_pluck_distinct
    documents.map{ |doc| doc.send(field) }.uniq
  else
    pluck(field).uniq
  end
end

#eachEnumerator

Iterate over the context. If provided a block, yield to a Mongoid document for each, otherwise return an enum.

Examples:

Iterate over the context.

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

Returns:

  • (Enumerator)

    The enumerator.



97
98
99
100
101
102
103
104
105
106
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 97

def each
  if block_given?
    documents_for_iteration.each do |doc|
      yield(doc)
    end
    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.



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

def exists?
  any?
end

#fifthDocument

Get the fifth document in the database for the criteria's selector.

Examples:

Get the fifth document.

context.fifth

Parameters:

  • limit (Integer)

    The number of documents to return.

Returns:



449
450
451
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 449

def fifth
  eager_load([documents.fifth]).first
end

#fifth!Document

Get the fifth document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the fifth document.

context.fifth!

Returns:



463
464
465
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 463

def fifth!
  fifth || raise_document_not_found_error
end

#first(limit = nil) ⇒ Document Also known as: one, find_first

Get the first document in the database for the criteria's selector.

Examples:

Get the first document.

context.first

Parameters:

  • limit (Integer) (defaults to: nil)

    The number of documents to return.

Returns:



126
127
128
129
130
131
132
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 126

def first(limit = nil)
  if limit
    eager_load(documents.first(limit))
  else
    eager_load([documents.first]).first
  end
end

#first!Document

Get the first document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the first document.

context.first!

Returns:



146
147
148
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 146

def first!
  first || raise_document_not_found_error
end

#fourthDocument

Get the fourth document in the database for the criteria's selector.

Examples:

Get the fourth document.

context.fourth

Parameters:

  • limit (Integer)

    The number of documents to return.

Returns:



423
424
425
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 423

def fourth
  eager_load([documents.fourth]).first
end

#fourth!Document

Get the fourth document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the fourth document.

context.fourth!

Returns:



437
438
439
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 437

def fourth!
  fourth || raise_document_not_found_error
end

#inc(incs) ⇒ Enumerator

Increment a value on all documents.

Examples:

Perform the increment.

context.inc(likes: 10)

Parameters:

  • incs (Hash)

    The operations.

Returns:

  • (Enumerator)

    The enumerator.



175
176
177
178
179
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 175

def inc(incs)
  each do |document|
    document.inc(incs)
  end
end

#last(limit = nil) ⇒ Document

Get the last document in the database for the criteria's selector.

Examples:

Get the last document.

context.last

Parameters:

  • limit (Integer) (defaults to: nil)

    The number of documents to return.

Returns:



189
190
191
192
193
194
195
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 189

def last(limit = nil)
  if limit
    eager_load(documents.last(limit))
  else
    eager_load([documents.last]).first
  end
end

#last!Document

Get the last document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the last document.

context.last!

Returns:



207
208
209
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 207

def last!
  last || raise_document_not_found_error
end

#lengthInteger Also known as: size

Get the length of matching documents in the context.

Examples:

Get the length of matching documents.

context.length

Returns:

  • (Integer)

    The matching length.



217
218
219
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 217

def length
  documents.length
end

#limit(value) ⇒ Memory

Limits the number of documents that are returned.

Examples:

Limit the documents.

context.limit(20)

Parameters:

  • value (Integer)

    The number of documents to return.

Returns:



230
231
232
233
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 230

def limit(value)
  self.limiting = value
  self
end

#pick(*fields) ⇒ Object | Array<Object>

Pick the field values in memory.

Examples:

Get the values in memory.

context.pick(:name)

Parameters:

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

    Field(s) to pick.

Returns:

  • (Object | Array<Object>)

    The picked values.



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

def pick(*fields)
  if doc = documents.first
    pluck_from_doc(doc, *fields)
  end
end

#pluck(*fields) ⇒ Array<Object> | Array<Array<Object>>

Pluck the field values in memory.

Examples:

Get the values in memory.

context.pluck(:name)

Parameters:

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

    Field(s) to pluck.

Returns:

  • (Array<Object> | Array<Array<Object>>)

    The plucked values.



243
244
245
246
247
248
249
250
251
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 243

def pluck(*fields)
  if Mongoid.legacy_pluck_distinct
    documents.pluck(*fields)
  else
    documents.map do |doc|
      pluck_from_doc(doc, *fields)
    end
  end
end

#secondDocument

Get the second document in the database for the criteria's selector.

Examples:

Get the second document.

context.second

Parameters:

  • limit (Integer)

    The number of documents to return.

Returns:



371
372
373
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 371

def second
  eager_load([documents.second]).first
end

#second!Document

Get the second document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the second document.

context.second!

Returns:



385
386
387
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 385

def second!
  second || raise_document_not_found_error
end

#second_to_lastDocument

Get the second to last document in the database for the criteria's selector.

Examples:

Get the second to last document.

context.second_to_last

Parameters:

  • limit (Integer)

    The number of documents to return.

Returns:

  • (Document)

    The second to last document.



475
476
477
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 475

def second_to_last
  eager_load([documents.second_to_last]).first
end

#second_to_last!Document

Get the second to last document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the second to last document.

context.second_to_last!

Returns:

  • (Document)

    The second to last document.



489
490
491
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 489

def second_to_last!
  second_to_last || raise_document_not_found_error
end

#skip(value) ⇒ Memory

Skips the provided number of documents.

Examples:

Skip the documents.

context.skip(20)

Parameters:

  • value (Integer)

    The number of documents to skip.

Returns:



321
322
323
324
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 321

def skip(value)
  self.skipping = value
  self
end

#sort(values) ⇒ Memory

Sorts the documents by the provided spec.

Examples:

Sort the documents.

context.sort(name: -1, title: 1)

Parameters:

  • values (Hash)

    The sorting values as field/direction(1/-1) pairs.

Returns:



335
336
337
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 335

def sort(values)
  in_place_sort(values) and self
end

#take(limit = nil) ⇒ Document

Take the given number of documents from the database.

Examples:

Take a document.

context.take

Parameters:

  • limit (Integer | nil) (defaults to: nil)

    The number of documents to take or nil.

Returns:



291
292
293
294
295
296
297
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 291

def take(limit = nil)
  if limit
    eager_load(documents.take(limit))
  else
    eager_load([documents.first]).first
  end
end

#take!Document

Take the given number of documents from the database or raise an error if none are found.

Examples:

Take a document.

context.take

Returns:



309
310
311
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 309

def take!
  take || raise_document_not_found_error
end

#tally(field) ⇒ Hash

Tally the field values in memory.

Examples:

Get the counts of values in memory.

context.tally(:name)

Parameters:

  • field (String | Symbol)

    Field to tally.

Returns:

  • (Hash)

    The hash of counts.



275
276
277
278
279
280
281
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 275

def tally(field)
  return documents.each_with_object({}) do |d, acc|
    v = retrieve_value_at_path(d, field)
    acc[v] ||= 0
    acc[v] += 1
  end
end

#thirdDocument

Get the third document in the database for the criteria's selector.

Examples:

Get the third document.

context.third

Parameters:

  • limit (Integer)

    The number of documents to return.

Returns:



397
398
399
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 397

def third
  eager_load([documents.third]).first
end

#third!Document

Get the third document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the third document.

context.third!

Returns:



411
412
413
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 411

def third!
  third || raise_document_not_found_error
end

#third_to_lastDocument

Get the third to last document in the database for the criteria's selector.

Examples:

Get the third to last document.

context.third_to_last

Parameters:

  • limit (Integer)

    The number of documents to return.

Returns:

  • (Document)

    The third to last document.



501
502
503
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 501

def third_to_last
  eager_load([documents.third_to_last]).first
end

#third_to_last!Document

Get the third to last document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the third to last document.

context.third_to_last!

Returns:

  • (Document)

    The third to last document.



515
516
517
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 515

def third_to_last!
  third_to_last || raise_document_not_found_error
end

#update(attributes = nil) ⇒ nil | false

Update the first matching document atomically.

Examples:

Update the matching document.

context.update(name: "Smiths")

Parameters:

  • attributes (Hash) (defaults to: nil)

    The new attributes for the document.

Returns:

  • (nil | false)

    False if no attributes were provided.



347
348
349
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 347

def update(attributes = nil)
  update_documents(attributes, [ first ])
end

#update_all(attributes = nil) ⇒ nil | false

Update all the matching documents atomically.

Examples:

Update all the matching documents.

context.update_all(name: "Smiths")

Parameters:

  • attributes (Hash) (defaults to: nil)

    The new attributes for each document.

Returns:

  • (nil | false)

    False if no attributes were provided.



359
360
361
# File 'build/mongoid-master/lib/mongoid/contextual/memory.rb', line 359

def update_all(attributes = nil)
  update_documents(attributes, entries)
end