Class: Mongoid::Contextual::Memory

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

#avg, #max, #min, #sum

Constructor Details

#initialize(criteria) ⇒ Memory

Create the new in memory context.

Examples:

Create the new context.

Memory.new(criteria)

Parameters:

Since:

  • 3.0.0



149
150
151
152
153
154
155
156
157
158
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 149

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.



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

def documents
  @documents
end

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



20
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 20

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

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

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



20
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 20

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

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

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



20
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 20

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

#selectorObject (readonly)

Returns the value of attribute selector.



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

def selector
  @selector
end

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



20
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 20

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.

Since:

  • 3.0.0



32
33
34
35
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 32

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.

Since:

  • 3.0.0



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 45

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.

Since:

  • 3.0.0



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

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.

Since:

  • 3.0.0



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

def distinct(field)
  documents.map{ |doc| doc.send(field) }.uniq
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.

Since:

  • 3.0.0



104
105
106
107
108
109
110
111
112
113
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 104

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.

Since:

  • 3.0.0



123
124
125
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 123

def exists?
  count > 0
end

#first(*args) ⇒ 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

Returns:

Since:

  • 3.0.0



135
136
137
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 135

def first(*args)
  eager_load([documents.first]).first
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.

Since:

  • 7.0.0



170
171
172
173
174
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 170

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

#lastDocument

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

Examples:

Get the last document.

context.last

Returns:

Since:

  • 3.0.0



184
185
186
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 184

def last
  eager_load([documents.last]).first
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.

Since:

  • 3.0.0



196
197
198
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 196

def length
  documents.length
end

#limit(value) ⇒ Mongo

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:

  • (Mongo)

    The context.

Since:

  • 3.0.0



211
212
213
214
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 211

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

#pluck(*fields) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 216

def pluck(*fields)
  fields = Array.wrap(fields)
  documents.map do |doc|
    if fields.size == 1
      doc[fields.first]
    else
      fields.map { |n| doc[n] }.compact
    end
  end.compact
end

#skip(value) ⇒ Mongo

Skips the provided number of documents.

Examples:

Skip the documents.

context.skip(20)

Parameters:

  • value (Integer)

    The number of documents to skip.

Returns:

  • (Mongo)

    The context.

Since:

  • 3.0.0



237
238
239
240
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 237

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

#sort(values) ⇒ Mongo

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:

  • (Mongo)

    The context.

Since:

  • 3.0.0



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

def sort(values)
  in_place_sort(values) and self
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.

Since:

  • 3.0.0



267
268
269
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 267

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.

Since:

  • 3.0.0



281
282
283
# File 'build/mongoid-7.3/lib/mongoid/contextual/memory.rb', line 281

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