Sha256: a77cbe832dad33550449697006464443bcba4c7bb2eb4345070b13372179ffed

Contents?: true

Size: 1.04 KB

Versions: 17

Compression:

Stored size: 1.04 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
            end
          end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
mongo_mapper-unstable-2010.1.27 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.26 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.25 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.22 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.21 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.20 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.19 lib/mongo_mapper/plugins/associations/embedded_collection.rb
jmonteiro-mongo_mapper-0.1.7 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.18 lib/mongo_mapper/plugins/associations/embedded_collection.rb
jmonteiro-mongo_mapper-0.1.6 lib/mongo_mapper/plugins/associations/embedded_collection.rb
jmonteiro-mongo_mapper-0.1.5 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.17 lib/mongo_mapper/plugins/associations/embedded_collection.rb
jmonteiro-mongo_mapper-0.1.4 lib/mongo_mapper/plugins/associations/embedded_collection.rb
jmonteiro-mongo_mapper-0.1.2 lib/mongo_mapper/plugins/associations/embedded_collection.rb
jmonteiro-mongo_mapper-0.1.1 lib/mongo_mapper/plugins/associations/embedded_collection.rb
jmonteiro-mongo_mapper-0.1.0 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-unstable-2010.1.12 lib/mongo_mapper/plugins/associations/embedded_collection.rb