0

我正在尝试将日期与多个条件进行比较,并根据它满足的条件返回特定的输出。我不喜欢这些ifelsif陈述:

 def ugly_method
    if first_active_entitlement.try(:created_at) == nil
      "No timestamp on first active entitlement"
    elsif first_active_entitlement == nil
      "No Active Entitlement"
    elsif last_suspended_entitlement == nil 
      first_time_or_renewal(first_active_entitlement, start_date, end_date)
    elsif first_active_entitlement.try(:created_at) > last_suspended_entitlement.try(:suspended_at) && first_active_entitlement.try(:created_at) > start_date && first_active_entitlement.created_at < end_date
      "Restart"
    else
      first_time_or_renewal(first_active_entitlement, start_date, end_date)
  end

有没有人不得不将日期与许多不同的条件进行比较?有什么明显的东西会更容易让我错过吗?

4

1 回答 1

0

我会将其分解为多种方法:

  • 验证first_active_entitlement
  • 验证last_suspended_entitlement

这些方法可以返回消息,也可以返回一个标志来检查对象是否存在。

然后,在您的方法中,您可以显示来自这些方法的消息,以防方法的标志设置为 false,如果不只是执行更新。

于 2013-07-03T19:24:39.513 回答