Class: MongoMapper::Plugins::Associations::EmbeddedCollection

Inherits:
Collection show all
Defined in:
lib/mongo_mapper/plugins/associations/proxy/embedded_collection.rb

Instance Attribute Summary

Attributes inherited from Proxy

#association, #proxy_owner, #target

Instance Method Summary collapse

Methods inherited from Collection

#[], #each, #empty?, #length, #read, #reset, #size, #to_a, #write

Methods inherited from Proxy

define_proxy_method, #initialize, #inspect, #loaded, #loaded?, #proxy_method, #proxy_respond_to?, #read, #reload, #replace, #reset, #respond_to?, #write

Constructor Details

This class inherits a constructor from MongoMapper::Plugins::Associations::Proxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MongoMapper::Plugins::Associations::Proxy

Instance Method Details

#<<(*docs) ⇒ Object Also known as: push, concat



23
24
25
26
27
28
29
# File 'lib/mongo_mapper/plugins/associations/proxy/embedded_collection.rb', line 23

def <<(*docs)
  load_target
  docs.each do |doc|
    assign_references(doc)
    target << doc
  end
end

#build(attributes = {}) ⇒ Object



6
7
8
9
10
11
# File 'lib/mongo_mapper/plugins/associations/proxy/embedded_collection.rb', line 6

def build(attributes={})
  doc = klass.new(attributes)
  assign_references(doc)
  self << doc
  doc
end

#countObject



18
19
20
21
# File 'lib/mongo_mapper/plugins/associations/proxy/embedded_collection.rb', line 18

def count
  load_target
  target.size
end

#find(id) ⇒ Object



13
14
15
16
# File 'lib/mongo_mapper/plugins/associations/proxy/embedded_collection.rb', line 13

def find(id)
  load_target
  target.detect { |item| item.id.to_s == id || item.id == id }
end

#save_to_collection(options = {}) ⇒ Object



33
34
35
# File 'lib/mongo_mapper/plugins/associations/proxy/embedded_collection.rb', line 33

def save_to_collection(options={})
  @target.each { |doc| doc.persist(options) } if @target
end