Class: Mongoid::Contextual::Memory

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Aggregable::Memory, Queryable, Positional, Relations::Eager
Defined in:
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.

Attributes included from Relations::Eager

#eager_loaded

Instance Method Summary (collapse)

Methods included from Positional

#positionally

Methods included from Queryable

#blank?

Methods included from Relations::Eager

#eager_load, #eager_load_one, #eager_loadable?, #preload, #with_eager_loading

Methods included from Aggregable::Memory

#avg, #max, #min, #sum

Constructor Details

- (Memory) initialize(criteria)

Create the new in memory context.

Examples:

Create the new context.

Memory.new(criteria)

Parameters:

Since:

  • 3.0.0



147
148
149
150
151
152
153
154
155
156
# File 'lib/mongoid/contextual/memory.rb', line 147

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

- (Object) documents (readonly)

Returns the value of attribute documents



18
19
20
# File 'lib/mongoid/contextual/memory.rb', line 18

def documents
  @documents
end

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



18
# File 'lib/mongoid/contextual/memory.rb', line 18

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

- (Object) path (readonly)

Returns the value of attribute path



18
19
20
# File 'lib/mongoid/contextual/memory.rb', line 18

def path
  @path
end

- (Object) path The atomic path.(Theatomicpath.) (readonly)



18
# File 'lib/mongoid/contextual/memory.rb', line 18

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

- (Object) root (readonly)

Returns the value of attribute root



18
19
20
# File 'lib/mongoid/contextual/memory.rb', line 18

def root
  @root
end

- (Object) root The root document.(Therootdocument.) (readonly)



18
# File 'lib/mongoid/contextual/memory.rb', line 18

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

- (Object) selector (readonly)

Returns the value of attribute selector



18
19
20
# File 'lib/mongoid/contextual/memory.rb', line 18

def selector
  @selector
end

- (Object) selector The root document selector.(Therootdocumentselector.) (readonly)



18
# File 'lib/mongoid/contextual/memory.rb', line 18

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

Instance Method Details

- (true, false) ==(other)

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



30
31
32
33
# File 'lib/mongoid/contextual/memory.rb', line 30

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

- (nil) delete 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



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mongoid/contextual/memory.rb', line 43

def delete
  deleted = count
  removed = map do |doc|
    prepare_remove(doc)
    doc.as_document
  end
  unless removed.empty?
    collection.find(selector).update(
      positionally(selector, "$pullAll" => { path => removed })
    )
  end
  deleted
end

- (nil) destroy 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



66
67
68
69
70
71
72
73
# File 'lib/mongoid/contextual/memory.rb', line 66

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

- (Array<Object>) distinct(field)

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



86
87
88
# File 'lib/mongoid/contextual/memory.rb', line 86

def distinct(field)
  documents.map{ |doc| doc.send(field) }.uniq
end

- (Enumerator) each

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



101
102
103
104
105
106
107
108
109
110
# File 'lib/mongoid/contextual/memory.rb', line 101

def each
  if block_given?
    documents_for_iteration.each do |doc|
      yield(doc)
    end
    self
  else
    to_enum
  end
end

- (true, false) exists?

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



120
121
122
# File 'lib/mongoid/contextual/memory.rb', line 120

def exists?
  count > 0
end

- (Document) first Also known as: one

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

Examples:

Get the first document.

context.first

Returns:

Since:

  • 3.0.0



132
133
134
135
136
# File 'lib/mongoid/contextual/memory.rb', line 132

def first
  doc = documents.first
  eager_load_one(doc)
  doc
end

- (Document) last

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

Examples:

Get the last document.

context.last

Returns:

Since:

  • 3.0.0



166
167
168
169
170
# File 'lib/mongoid/contextual/memory.rb', line 166

def last
  doc = documents.last
  eager_load_one(doc)
  doc
end

- (Integer) length 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



180
181
182
# File 'lib/mongoid/contextual/memory.rb', line 180

def length
  documents.length
end

- (Mongo) limit(value)

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



195
196
197
198
# File 'lib/mongoid/contextual/memory.rb', line 195

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

- (Mongo) skip(value)

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



210
211
212
213
# File 'lib/mongoid/contextual/memory.rb', line 210

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

- (Mongo) sort(values)

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



226
227
228
# File 'lib/mongoid/contextual/memory.rb', line 226

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

- (nil, false) update(attributes = nil)

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



240
241
242
# File 'lib/mongoid/contextual/memory.rb', line 240

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

- (nil, false) update_all(attributes = nil)

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



254
255
256
# File 'lib/mongoid/contextual/memory.rb', line 254

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