这种方法如何烘干呢?

发布于 2024-10-26 17:00:15 字数 858 浏览 0 评论 0原文

  def restore_download_delete_file
    begin
      case params[:submit]
      when "restore"
        restore_status = restore_file(params[:file_names])
        raise if restore_status != 0
        flash[:notice] = "File Successfully Restored."
        redirect_to :action => "database_settings"
      when "download"
        download_status = download_file(params[:file_names])
        raise if download_status != 0
      when "delete"
        delete_status = delete_file(params[:file_names])
        raise if delete_status != 0
        flash[:notice] = "File Successfully Deleted."
        redirect_to :action => "database_settings"
      end
    rescue Exception => e
      flash[:error] = "Error with #{params[:submit]}! Please retry."
      redirect_to :action => "database_settings"
    end
  end

我该如何改进这个方法?

  def restore_download_delete_file
    begin
      case params[:submit]
      when "restore"
        restore_status = restore_file(params[:file_names])
        raise if restore_status != 0
        flash[:notice] = "File Successfully Restored."
        redirect_to :action => "database_settings"
      when "download"
        download_status = download_file(params[:file_names])
        raise if download_status != 0
      when "delete"
        delete_status = delete_file(params[:file_names])
        raise if delete_status != 0
        flash[:notice] = "File Successfully Deleted."
        redirect_to :action => "database_settings"
      end
    rescue Exception => e
      flash[:error] = "Error with #{params[:submit]}! Please retry."
      redirect_to :action => "database_settings"
    end
  end

How could I improve this method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美男兮 2024-11-02 17:00:15

您可以通过将其分为四份来清理:一份用于恢复,一份用于删除,一份用于下载,一份用于调用适当的并处理异常。

def restore_download_delete_file
  begin
    self.send "#{params[:submit]}"
  rescue Exception => e
    flash[:error] = "Error with #{params[:submit]}! Please retry."
    redirect_to :action => "database_settings"
  end
end

def restore
  restore_status = restore_file(params[:file_names])
  raise if restore_status != 0
  flash[:notice] = "File Successfully Restored."
  redirect_to :action => "database_settings"
end

def download
  download_status = download_file(params[:file_names])
  raise if download_status != 0
end

def delete
  delete_status = delete_file(params[:file_names])
  raise if delete_status != 0
  flash[:notice] = "File Successfully Deleted."
  redirect_to :action => "database_settings"
end

另外,还有一些注意事项:

  • 引发适当的异常,而不是零。
  • 不要拯救所有异常。相反,拯救你正在抚养的人。

You can clean it up by dividing it into four: one for restore, one for delete, one for download, and one for calling the appropiate one and handling the exceptions.

def restore_download_delete_file
  begin
    self.send "#{params[:submit]}"
  rescue Exception => e
    flash[:error] = "Error with #{params[:submit]}! Please retry."
    redirect_to :action => "database_settings"
  end
end

def restore
  restore_status = restore_file(params[:file_names])
  raise if restore_status != 0
  flash[:notice] = "File Successfully Restored."
  redirect_to :action => "database_settings"
end

def download
  download_status = download_file(params[:file_names])
  raise if download_status != 0
end

def delete
  delete_status = delete_file(params[:file_names])
  raise if delete_status != 0
  flash[:notice] = "File Successfully Deleted."
  redirect_to :action => "database_settings"
end

Also, a couple considerations:

  • Raise proper exceptions, not nil.
  • Don't rescue all exceptions. Rescue the ones you are raising instead.
无名指的心愿 2024-11-02 17:00:15

尝试这样

begin
  status = send("#{params[:submit]}_file", params[:file_names])
  raise unless status == 0
  if params[:submit] == 'restore' || params[:submit] == 'delete'
    flash[:notice] = "File Successfully #{params[:submit].capitalize}d"
  end
rescue Exception => e
  flash[:error] = "Error with #{params[:submit]}! Please retry."
ensure
  redirect_to :action => "database_settings" unless params[:submit] == 'download'
end

Try like this

begin
  status = send("#{params[:submit]}_file", params[:file_names])
  raise unless status == 0
  if params[:submit] == 'restore' || params[:submit] == 'delete'
    flash[:notice] = "File Successfully #{params[:submit].capitalize}d"
  end
rescue Exception => e
  flash[:error] = "Error with #{params[:submit]}! Please retry."
ensure
  redirect_to :action => "database_settings" unless params[:submit] == 'download'
end
超可爱的懒熊 2024-11-02 17:00:15
 def restore_download_delete_file
    submit = params[:submit]
    if not restore_file(params[:file_names]).zero?
      flash[:error] = "Error with #{submit}! Please retry."
    elsif submit != "download"
      flash[:notice] = "File Successfully #{submit.capitalize}d."
    else
      return
    end
    redirect_to :action => "database_settings"
 end
 def restore_download_delete_file
    submit = params[:submit]
    if not restore_file(params[:file_names]).zero?
      flash[:error] = "Error with #{submit}! Please retry."
    elsif submit != "download"
      flash[:notice] = "File Successfully #{submit.capitalize}d."
    else
      return
    end
    redirect_to :action => "database_settings"
 end
芯好空 2024-11-02 17:00:15

你总是可以把你的大方法变成一个小解释器:

private
FILE_CMDS = {
    'delete' => {
        :action   => lambda { |names| delete_file(names) },
        :notice   => 'File Successfully Deleted.',
        :redirect => 'database_settings',
    },
    'download' => {
        :action => lambda { |names| download_file(names) },
    },
    'restore' => {
        :action   => lamba { |names| restore_file(names) },
        :notice   => 'File Successfully Restored.',
        :redirect => 'database_settings',
    },
}

public
def restore_download_delete_file
    begin
        cmd    = FILE_CMDS[params[:submit]]
        status = cmd[:action].call(params[:file_names])
        raise if(status != 0)
        flash[:notice] = cmd[:notice] if(cmd[:notice])
        redirect_to :action => cmd[:redirect] if(cmd[:redirect])
    rescue Exception => e
        flash[:error] = "Error with #{params[:submit]}! Please retry."
        redirect_to :action => "database_settings"
    end
end

但我认为 egarcia 的方法最有意义;真的有
无需将所有这些东西混入一种方法中。共同点
确实非常小,所以一个控制器中有四种方法
更多意义:一种行动,一种方法。 DRY 只是一个指导方针,它不是
不惜一切代价必须遵循的教条。

You can always turn your mega-method into a little interpreter:

private
FILE_CMDS = {
    'delete' => {
        :action   => lambda { |names| delete_file(names) },
        :notice   => 'File Successfully Deleted.',
        :redirect => 'database_settings',
    },
    'download' => {
        :action => lambda { |names| download_file(names) },
    },
    'restore' => {
        :action   => lamba { |names| restore_file(names) },
        :notice   => 'File Successfully Restored.',
        :redirect => 'database_settings',
    },
}

public
def restore_download_delete_file
    begin
        cmd    = FILE_CMDS[params[:submit]]
        status = cmd[:action].call(params[:file_names])
        raise if(status != 0)
        flash[:notice] = cmd[:notice] if(cmd[:notice])
        redirect_to :action => cmd[:redirect] if(cmd[:redirect])
    rescue Exception => e
        flash[:error] = "Error with #{params[:submit]}! Please retry."
        redirect_to :action => "database_settings"
    end
end

But I think egarcia's approach makes the most sense; there's really
no need to mash all this stuff into one method. The commonalities
really are quite minimal so four methods in one controller makes
more sense: one action, one method. DRY is just a guideline, it isn't
dogma to be followed at all costs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文