class ActiveMongoid::Associations::Targets::Enumerable
Attributes
added[RW]
loaded[RW]
unloaded[RW]
Public Class Methods
new(target)
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 85 def initialize(target) if target.is_a?(::Mongoid::Criteria) || target.is_a?(::ActiveRecord::Relation) @added, @loaded, @unloaded = [], [], target else @added, @executed, @loaded = [], true, target end end
Public Instance Methods
<<(document)
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 18 def <<(document) added.push(document) end
Also aliased as: push
==(other)
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 13 def ==(other) return false unless other.respond_to?(:entries) entries == other.entries end
as_json(options = {})
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 136 def as_json(options = {}) entries.as_json(options) end
clear() { |doc| ... }
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 23 def clear if block_given? in_memory { |doc| yield(doc) } end loaded.clear and added.clear end
clone()
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 30 def clone collect { |doc| doc.clone } end
delete(document) { |document| ... }
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 34 def delete(document) (loaded.delete_one(document) || added.delete_one(document)).tap do |doc| unless doc key = document.is_a?(::Mongoid::Document) ? :_id : :id if unloaded && unloaded.where(key => document.id).exists? yield(document) if block_given? return document end end yield(doc) if block_given? end end
delete_if(&block)
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 47 def delete_if(&block) load_all! tap do loaded.delete_if(&block) added.delete_if(&block) end end
each() { |doc| ... }
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 55 def each if loaded? loaded.each do |doc| yield(doc) end else unloaded.each do |doc| document = added.delete(doc) || loaded.delete(doc) || doc yield(document) loaded.push(document) end end added.each do |doc| yield(doc) end @executed = true end
empty?()
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 73 def empty? if loaded? in_memory.count == 0 else unloaded.count + added.count == 0 end end
first()
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 81 def first added.first || (loaded? ? loaded.first : unloaded.first) end
in_memory() { |doc| ... }
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 97 def in_memory (loaded + added).tap do |docs| docs.each { |doc| yield(doc) } if block_given? end end
inspect()
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 93 def inspect entries.inspect end
last()
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 103 def last added.last || (loaded? ? loaded.last : unloaded.last) end
loaded?()
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 109 def loaded? !!@executed end
reset()
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 113 def reset loaded.clear and added.clear @executed = false end
respond_to?(name, include_private = false)
click to toggle source
Calls superclass method
# File lib/active_mongoid/associations/targets/enumerable.rb, line 118 def respond_to?(name, include_private = false) [].respond_to?(name, include_private) || super end
size()
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 122 def size count = (unloaded ? unloaded.count : loaded.count) if count.zero? count + added.count else count + added.count{ |d| d.new_record? } end end
Also aliased as: length
to_json(options = {})
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 132 def to_json(options = {}) entries.to_json(options) end
uniq()
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 140 def uniq entries.uniq end
where(opts = :chain, *rest)
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 144 def where(opts = :chain, *rest) if unloaded.is_a?(::Mongoid::Criteria) || unloaded.is_a?(::ActiveRecord::Relation) unloaded.where(opts, *rest) else raise NoMethodError end end
Private Instance Methods
method_missing(name, *args, &block)
click to toggle source
# File lib/active_mongoid/associations/targets/enumerable.rb, line 154 def method_missing(name, *args, &block) entries.send(name, *args, &block) end