Sha256: 9211ab65fa15e2e9ad28bbf0f21162d6c552b82ab81d78c337da271b9a4f0bd7

Contents?: true

Size: 1.08 KB

Versions: 21

Compression:

Stored size: 1.08 KB

Contents

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, :<<

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

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

Version data entries

21 entries across 21 versions & 4 rubygems

Version Path
drogus-mongo_mapper-0.6.10 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-rails3-0.7.0.1 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.27 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.26 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.25 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.24 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.23 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.22 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.19 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.18 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.17 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.16 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.15 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.12 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.11 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.10 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.7.0 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.9 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.8 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.2.5 lib/mongo_mapper/plugins/associations/embedded_collection.rb