Sha256: d9fe7c87b9c0ad9f77502bdc82ebce00f8487360d697e287907aa59445c0c25f

Contents?: true

Size: 861 Bytes

Versions: 3

Compression:

Stored size: 861 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, :<<

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

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
mongo_mapper_ign-0.7.7 lib/mongo_mapper/plugins/associations/embedded_collection.rb
pwnash-mongo_mapper-0.7.6 lib/mongo_mapper/plugins/associations/embedded_collection.rb
mongo_mapper_ign-0.7.6 lib/mongo_mapper/plugins/associations/embedded_collection.rb