Sha256: 4131286316a409d0293a86fca52b2118cc3a0aa4918d20c28629a018b6925600

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

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

      def find(id)
        load_target
        @target.detect { |item| 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

6 entries across 6 versions & 2 rubygems

Version Path
mongo_mapper-unstable-2009.11.8 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.11.6 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.8.2 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.8.1 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.11.2 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.10.31 lib/mongo_mapper/associations/many_embedded_proxy.rb