# File lib/mongo_mapper/plugins/associations/one_proxy.rb, line 18
        def replace(doc)
          load_target

          if !target.nil? && target != doc
            if target.persisted?
              case options[:dependent]
                when :delete  then target.delete
                when :destroy then target.destroy
                else
                  nullify_scope(target)
                  target.save
              end
            end
          end

          unless doc.nil?
            proxy_owner.save unless proxy_owner.persisted?
            doc = klass.new(doc) unless doc.is_a?(klass)
            apply_scope(doc)
            doc.save unless doc.persisted?
          end

          loaded
          @target = doc
        end