Class: Mongoid::Relations::Targets::Enumerable

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mongoid/relations/targets/enumerable.rb

Overview

This class is the wrapper for all relational associations that have a target that can be a criteria or array of loaded documents. This handles both cases or a combination of the two.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Enumerable

Initialize the new enumerable either with a criteria or an array.

Examples:

Initialize the enumerable with a criteria.

Enumberable.new(Post.where(:person_id => id))

Initialize the enumerable with an array.

Enumerable.new([ post ])

Parameters:

Since:

  • 2.1.0



206
207
208
209
210
211
212
# File 'lib/mongoid/relations/targets/enumerable.rb', line 206

def initialize(target)
  if target.is_a?(Criteria)
    @added, @loaded, @unloaded = [], [], target
  else
    @added, @executed, @loaded = [], true, target
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



372
373
374
# File 'lib/mongoid/relations/targets/enumerable.rb', line 372

def method_missing(name, *args, &block)
  entries.send(name, *args, &block)
end

Instance Attribute Details

#addedObject

The three main instance variables are collections of documents.



17
18
19
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17

def added
  @added
end

#added Documents that have been appended.(Documentsthathavebeenappended.) ⇒ Object

The three main instance variables are collections of documents.



17
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17

attr_accessor :added, :loaded, :unloaded

#loadedObject

The three main instance variables are collections of documents.



17
18
19
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17

def loaded
  @loaded
end

#loaded Persisted documents that have been loaded.(Persisteddocumentsthathavebeenloaded.) ⇒ Object

The three main instance variables are collections of documents.



17
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17

attr_accessor :added, :loaded, :unloaded

#unloadedObject

The three main instance variables are collections of documents.



17
18
19
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17

def unloaded
  @unloaded
end

#unloaded A criteria representing persisted docs.(Acriteriarepresentingpersisteddocs.) ⇒ Object

The three main instance variables are collections of documents.



17
# File 'lib/mongoid/relations/targets/enumerable.rb', line 17

attr_accessor :added, :loaded, :unloaded

Instance Method Details

#<<(document) ⇒ Document Also known as: push

Append a document to the enumerable.

Examples:

Append the document.

enumerable << document

Parameters:

  • document (Document)

    The document to append.

Returns:

Since:

  • 2.1.0



46
47
48
# File 'lib/mongoid/relations/targets/enumerable.rb', line 46

def <<(document)
  added.push(document)
end

#==(other) ⇒ true, false

Check if the enumerable is equal to the other object.

Examples:

Check equality.

enumerable == []

Parameters:

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.1.0



31
32
33
34
# File 'lib/mongoid/relations/targets/enumerable.rb', line 31

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

#as_json(options = {}) ⇒ Hash

Send #as_json to the entries, without encoding.

Examples:

Get the enumerable as json.

enumerable.as_json

Parameters:

  • options (Hash) (defaults to: {})

    Optional parameters.

Returns:

  • (Hash)

    The entries all loaded as a hash.

Since:

  • 2.2.0



352
353
354
# File 'lib/mongoid/relations/targets/enumerable.rb', line 352

def as_json(options = {})
  entries.as_json(options)
end

#clearArray<Document>

Clears out all the documents in this enumerable. If passed a block it will yield to each document that is in memory.

Examples:

Clear out the enumerable.

enumerable.clear

Clear out the enumerable with a block.

enumerable.clear do |doc|
  doc.unbind
end

Returns:

Since:

  • 2.1.0



65
66
67
68
69
70
# File 'lib/mongoid/relations/targets/enumerable.rb', line 65

def clear
  if block_given?
    in_memory { |doc| yield(doc) }
  end
  loaded.clear and added.clear
end

#cloneArray<Document>

Note:

This loads all documents into memory.

Clones each document in the enumerable.

Examples:

Clone the enumerable.

enumerable.clone

Returns:

Since:

  • 2.1.6



82
83
84
# File 'lib/mongoid/relations/targets/enumerable.rb', line 82

def clone
  collect { |doc| doc.clone }
end

#delete(document) ⇒ Document

Delete the supplied document from the enumerable.

Examples:

Delete the document.

enumerable.delete(document)

Parameters:

  • document (Document)

    The document to delete.

Returns:

Since:

  • 2.1.0



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mongoid/relations/targets/enumerable.rb', line 96

def delete(document)
  (loaded.delete(document) || added.delete(document)).tap do |doc|
    unless doc
      if unloaded && unloaded.where(:_id => document.id).exists?
        yield(document) if block_given?
        return document
      end
    end
    yield(doc) if block_given?
  end
end

#delete_if(&block) ⇒ Array<Document>

Note:

This operation loads all documents from the database.

Deletes every document in the enumerable for where the block returns true.

Examples:

Delete all matching documents.

enumerable.delete_if do |doc|
  dod.id == id
end

Returns:

Since:

  • 2.1.0



121
122
123
124
125
126
127
# File 'lib/mongoid/relations/targets/enumerable.rb', line 121

def delete_if(&block)
  load_all!
  tap do
    loaded.delete_if(&block)
    added.delete_if(&block)
  end
end

#eachtrue

Iterating over this enumerable has to handle a few different scenarios.

If the enumerable has its criteria loaded into memory then it yields to all the loaded docs and all the added docs.

If the enumerable has not loaded the criteria then it iterates over the cursor while loading the documents and then iterates over the added docs.

Examples:

Iterate over the enumerable.

enumerable.each do |doc|
  puts doc
end

Returns:

  • (true)

    That the enumerable is now loaded.

Since:

  • 2.1.0



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mongoid/relations/targets/enumerable.rb', line 147

def each
  if loaded?
    loaded.each do |doc|
      yield(doc)
    end
  else
    unloaded.each do |doc|
      document = added.delete_one(doc) || loaded.delete_one(doc) || doc
      yield(document)
      loaded.push(document)
    end
  end
  added.each do |doc|
    yield(doc)
  end
  @executed = true
end

#empty?true, false

Is the enumerable empty? Will determine if the count is zero based on whether or not it is loaded.

Examples:

Is the enumerable empty?

enumerable.empty?

Returns:

  • (true, false)

    If the enumerable is empty.

Since:

  • 2.1.0



174
175
176
177
178
179
180
# File 'lib/mongoid/relations/targets/enumerable.rb', line 174

def empty?
  if loaded?
    in_memory.count == 0
  else
    unloaded.count + added.count == 0
  end
end

#firstDocument

Get the first document in the enumerable. Will check the persisted documents first. Does not load the entire enumerable.

Examples:

Get the first document.

enumerable.first

Returns:

  • (Document)

    The first document found.

Since:

  • 2.1.0



191
192
193
# File 'lib/mongoid/relations/targets/enumerable.rb', line 191

def first
  added.first || (loaded? ? loaded.first : unloaded.first)
end

#in_memoryArray<Document>

Note:

When passed a block it yields to each document.

Return all the documents in the enumerable that have been loaded or added.

Examples:

Get the in memory docs.

enumerable.in_memory

Returns:

Since:

  • 2.1.0



238
239
240
241
242
# File 'lib/mongoid/relations/targets/enumerable.rb', line 238

def in_memory
  (loaded + added).tap do |docs|
    docs.each { |doc| yield(doc) } if block_given?
  end
end

#inspectString

Inspection will just inspect the entries for nice array-style printing.

Examples:

Inspect the enumerable.

enumerable.inspect

Returns:

  • (String)

    The inspected enum.

Since:

  • 2.1.0



223
224
225
# File 'lib/mongoid/relations/targets/enumerable.rb', line 223

def inspect
  entries.inspect
end

#lastDocument

Get the last document in the enumerable. Will check the new documents first. Does not load the entire enumerable.

Examples:

Get the last document.

enumerable.last

Returns:

  • (Document)

    The last document found.

Since:

  • 2.1.0



253
254
255
# File 'lib/mongoid/relations/targets/enumerable.rb', line 253

def last
  added.last || (loaded? ? loaded.last : unloaded.last)
end

#loaded?true, false

Has the enumerable been loaded? This will be true if the criteria has been executed or we manually load the entire thing.

Examples:

Is the enumerable loaded?

enumerable.loaded?

Returns:

  • (true, false)

    If the enumerable has been loaded.

Since:

  • 2.1.0



276
277
278
# File 'lib/mongoid/relations/targets/enumerable.rb', line 276

def loaded?
  !!@executed
end

#resetfalse

Reset the enumerable back to it’s persisted state.

Examples:

Reset the enumerable.

enumerable.reset

Returns:

  • (false)

    Always false.

Since:

  • 2.1.0



288
289
290
291
# File 'lib/mongoid/relations/targets/enumerable.rb', line 288

def reset
  loaded.clear and added.clear
  @executed = false
end

#respond_to?(name, include_private = false) ⇒ true, false

Does this enumerable respond to the provided method?

Examples:

Does the enumerable respond to the method?

enumerable.respond_to?(:sum)

Parameters:

  • name (String, Symbol)

    The name of the method.

  • include_private (true, false) (defaults to: false)

    Whether to include private methods.

Returns:

  • (true, false)

    Whether the enumerable responds.

Since:

  • 2.1.0



305
306
307
# File 'lib/mongoid/relations/targets/enumerable.rb', line 305

def respond_to?(name, include_private = false)
  [].respond_to?(name, include_private) || super
end

#sizeInteger Also known as: length

Gets the total size of this enumerable. This is a combination of all the persisted and unpersisted documents.

Examples:

Get the size.

enumerable.size

Returns:

  • (Integer)

    The size of the enumerable.

Since:

  • 2.1.0



318
319
320
321
322
323
324
325
# File 'lib/mongoid/relations/targets/enumerable.rb', line 318

def size
  count = (unloaded ? unloaded.count : loaded.count)
  if count.zero?
    count + added.count
  else
    count + added.count{ |d| d.new_record? }
  end
end

#to_json(options = {}) ⇒ String

Send #to_json to the entries.

Examples:

Get the enumerable as json.

enumerable.to_json

Parameters:

  • options (Hash) (defaults to: {})

    Optional parameters.

Returns:

  • (String)

    The entries all loaded as a string.

Since:

  • 2.2.0



338
339
340
# File 'lib/mongoid/relations/targets/enumerable.rb', line 338

def to_json(options = {})
  entries.to_json(options)
end

#uniqArray<Document>

Note:

This operation loads all documents from the database.

Return all the unique documents in the enumerable.

Examples:

Get all the unique documents.

enumerable.uniq

Returns:

Since:

  • 2.1.0



366
367
368
# File 'lib/mongoid/relations/targets/enumerable.rb', line 366

def uniq
  entries.uniq
end