我怎么能干这个方法?

时间:2011-03-28 11:58:15

标签: ruby-on-rails ruby dry

  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

我怎样才能改进这种方法?

4 个答案:

答案 0 :(得分:4)

你可以把它分成四个来清理它:一个用于恢复,一个用于删除,一个用于下载,一个用于调用适当的一个并处理异常。

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

另外,还有几个注意事项:

  • 提出适当的例外,而不是零。
  • 不要拯救所有例外情况。拯救你正在筹集的人。

答案 1 :(得分:1)

试试这个

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

答案 2 :(得分:0)

 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

答案 3 :(得分:0)

你总是可以把你的超级方法变成一个小翻译:

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只是一个指导原则,事实并非如此 教条应不惜一切代价。