Sha256: 0e8d61cd8c8d252c1fc76487f49c6a6f89c2e33f5a6f42924fdcf47969291a8e

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

module MongoMapper
  module Associations
    class ManyEmbeddedProxy < Proxy
      def replace(v)
        @_values = v.map { |e| e.kind_of?(EmbeddedDocument) ? e.attributes : e }
        reset
      end

      def build(attributes={})
        doc = @association.klass.new(attributes)
        assign_root_document(doc)
        self << doc
        doc
      end

      # TODO: test that both string and oid version work
      def find(id)
        load_target
        @target.detect { |item| item.id.to_s == id || item.id == id }
      end

      def <<(*docs)
        if load_target
          docs.each do |doc|
            assign_root_document(doc)
            @target << doc
          end
        end
      end
      alias_method :push, :<<
      alias_method :concat, :<<

      private
        def find_target
          (@_values || []).map do |e|
            child = @association.klass.new(e)
            assign_root_document(child)
            child
          end
        end

        def root_document
          @owner._root_document || @owner
        end

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

Version data entries

7 entries across 7 versions & 4 rubygems

Version Path
mongo_mapper-0.6.6 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.12.4 lib/mongo_mapper/associations/many_embedded_proxy.rb
danielharan-mongo_mapper-0.6.5 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.6.5 lib/mongo_mapper/associations/many_embedded_proxy.rb
webbynode-mongo_mapper-0.6.4 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.6.4 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.6.3 lib/mongo_mapper/associations/many_embedded_proxy.rb