Sha256: 97e556ad986544fe02345e673c3dbc92e220596197010c49122273dbaec8552e

Contents?: true

Size: 1.18 KB

Versions: 19

Compression:

Stored size: 1.18 KB

Contents

module MongoMapper
  module Associations
    class ManyEmbeddedProxy < Collection
      def replace(values)
        @_values = values.map do |v|
          v.kind_of?(EmbeddedDocument) ? v.attributes : v
        end
        reset
      end

      def build(attributes={})
        doc = 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)
        load_target
        docs.each do |doc|
          assign_root_document(doc)
          target << doc
        end
      end
      alias_method :push, :<<
      alias_method :concat, :<<

      private
        def find_target
          (@_values || []).map do |v|
            child = klass.new(v)
            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

19 entries across 19 versions & 4 rubygems

Version Path
mrkurt-mongo_mapper-0.6.11 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2010.1.6 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2010.1.5 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2010.1.4 lib/mongo_mapper/associations/many_embedded_proxy.rb
tpitale-mongo_mapper-0.6.10 lib/mongo_mapper/associations/many_embedded_proxy.rb
mrkurt-mongo_mapper-0.6.10 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.6.10 lib/mongo_mapper/associations/many_embedded_proxy.rb
mrkurt-mongo_mapper-0.6.9 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.6.9 lib/mongo_mapper/associations/many_embedded_proxy.rb
mrkurt-mongo_mapper-0.6.8 lib/mongo_mapper/associations/many_embedded_proxy.rb
tpitale-mongo_mapper-0.6.9 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.12.30 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.12.29 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.12.23 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.12.22 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.12.16 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.6.8 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.12.14 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.6.7 lib/mongo_mapper/associations/many_embedded_proxy.rb