Sha256: 717dd5bfbaef701e6cced32910530c26f7479d09493bcf117ea71872766e05f9

Contents?: true

Size: 1.52 KB

Versions: 24

Compression:

Stored size: 1.52 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(opts={})
        owner = @owner
        child = @association.klass.new(opts)
        assign_parent_reference(child)
        child._root_document = owner
        self << child
        child
      end
      
      def find(opts)
        case opts
        when :all
          self
        when String
          if load_target
            child = @target.detect {|item| item.id == opts}
            assign_parent_reference(child)
            child
          end
        end
      end

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

      protected
        def find_target
          (@_values || []).map do |e|
            child = @association.klass.new(e)
            assign_parent_reference(child)
            child
          end
        end
        
      private
      
        def assign_parent_reference(child)
          return unless child && @owner
          return if @owner.class.name.blank?
          owner = @owner
          child.class_eval do
            define_method(owner.class.name.underscore) do
              owner
            end
          end
        end
      
    end
  end
end

Version data entries

24 entries across 24 versions & 3 rubygems

Version Path
djsun-mongo_mapper-0.5.6.6 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.6.5 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.5.8 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.5.7 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.6.4 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.6.3 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.6.2 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.6.1 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.5.6 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.5.3 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.10.16 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.5.2 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.5.5 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.10.12 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.4.1 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.3.1 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.5.4 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-0.5.3 lib/mongo_mapper/associations/many_embedded_proxy.rb
mongo_mapper-unstable-2009.10.11 lib/mongo_mapper/associations/many_embedded_proxy.rb
djsun-mongo_mapper-0.5.2.1 lib/mongo_mapper/associations/many_embedded_proxy.rb