Sha256: 29d5e50f3edbc543522ec86da2d6be01cec3fa0fa6ab8efd41e49448ddea6881

Contents?: true

Size: 980 Bytes

Versions: 11

Compression:

Stored size: 980 Bytes

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Associations
      class EmbeddedCollection < Collection
        def build(attributes={})
          doc = klass.new(attributes)
          assign_references(doc)
          self << doc
          doc
        end

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

        def count
          load_target
          target.size
        end

        def <<(*docs)
          load_target
          docs.each do |doc|
            assign_references(doc)
            target << doc
          end
        end
        alias_method :push, :<<
        alias_method :concat, :<<

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

      private
        def assign_references(*docs)
          docs.each { |doc| doc._parent_document = proxy_owner }
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
mongo_mapper-0.17.0 lib/mongo_mapper/plugins/associations/proxy/embedded_collection.rb
mongo_mapper-0.16.0 lib/mongo_mapper/plugins/associations/proxy/embedded_collection.rb
mongo_mapper-0.15.6 lib/mongo_mapper/plugins/associations/proxy/embedded_collection.rb
mongo_mapper-0.15.5 lib/mongo_mapper/plugins/associations/proxy/embedded_collection.rb
mongo_mapper-0.15.4 lib/mongo_mapper/plugins/associations/proxy/embedded_collection.rb
mongo_mapper-0.15.3 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.15.2 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.15.1 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.15.0 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.14.0 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.14.0.rc1 lib/mongo_mapper/plugins/associations/embedded_collection.rb