2013-07-03 36 views
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 

有沒有人曾來比較日期以許多不同的條件是什麼?任何明顯的事情會更容易,我失蹤了?

回答

0

我將它分解爲多個方法:

  • 驗證first_active_entitlement
  • 驗證last_suspended_entitlement

這些方法可以返回的消息,也許一個標誌,檢查對象的存在。

然後,在方法中,如果方法的標誌設置爲false,則可以顯示來自這些方法的消息,如果不是隻執行更新。

+0

謝謝你糾正我的錯誤@theTinMan –

+0

有沒有必要感謝我們澄清。這是爲了幫助社區。 –