Sha256: 613d9fad4710b7cd74fbf824b8ff3e9cc10e2589e33931159eba07a38fa670fd

Contents?: true

Size: 988 Bytes

Versions: 15

Compression:

Stored size: 988 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

15 entries across 15 versions & 3 rubygems

Version Path
mongo_mapper-0.13.1 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.13.0 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.13.0.beta2 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.13.0.beta1 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.12.0 lib/mongo_mapper/plugins/associations/embedded_collection.rb
lookout-mongo_mapper-0.11.3 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.11.2 lib/mongo_mapper/plugins/associations/embedded_collection.rb
jamieorc-mongo_mapper-0.11.1.1 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.11.1 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.11.0 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.10.1 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.10.0 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.9.2 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.9.1 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper-0.9.0 lib/mongo_mapper/plugins/associations/embedded_collection.rb