Sha256: e499da2f011b7b2674d366ed924df386484cfdc546d8dc7125ae1ea82e9803e0

Contents?: true

Size: 1.09 KB

Versions: 10

Compression:

Stored size: 1.09 KB

Contents

module MongoMapper
  module Plugins
    module Associations
      class EmbeddedCollection < Collection
        def build(attributes={})
          doc = klass.new(attributes)
          assign_root_document(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_root_document(doc)
            target << doc
          end
        end
        alias_method :push, :<<
        alias_method :concat, :<<

        private
          def _root_document
            if owner.respond_to?(:_root_document)
              owner._root_document
            else
              owner
            end
          end

          def assign_root_document(*docs)
            docs.each do |doc|
              doc._root_document = _root_document
              doc._parent_document = owner
            end
          end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
novelys_mongo_mapper-0.6.12 lib/novelys_mongo_mapper/plugins/associations/embedded_collection.rb
novelys_mongo_mapper-0.6.11 lib/novelys_mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.3 lib/mongo_mapper/plugins/associations/embedded_collection.rb
novelys_mongo_mapper-0.6.10 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.2 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.1 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.31 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.30 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.29 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.28 lib/mongo_mapper/plugins/associations/embedded_collection.rb