2016-12-01 53 views
2

我必須創建邏輯來設置基於多個條件的一天的狀態。我從創建多個if else語句開始,但它感覺不對。請幫助正確的方法。在軌道模型中簡化多個if else條件

def set_status_of_day(late_policy,early_departure_policy) 
     if late_policy.warning_on_late == "Half Day" && early_departure_policy.warning_on_late == "Half Day" 
      self.status_of_day = "Absent" 
     elsif late_policy.warning_on_late == "Half Day" && early_departure_policy.warning_on_late == "Present" 
      self.status_of_day = "Half Day" 
     elsif late_policy.warning_on_late == "Half Day" && early_departure_policy.warning_on_late == "Early Departure" 
      self.status_of_day = "Half Day" 
     elsif late_policy.warning_on_late == "Late" && early_departure_policy.warning_on_late == "Early Departure" 
      self.status_of_day = "Half Day" 
     elsif late_policy.warning_on_late == "Present" && early_departure_policy.warning_on_late == "Present" 
      self.status_of_day = "Present" 
     . 
     . 
     . 
     . 
     . 
     . 
     . 
     end    
end 

感謝

回答

2
def set_status_of_day(late_policy, early_departure_policy) 
    case [late_policy.warning_on_late, early_departure_policy.warning_on_late] 
    when ["Half Day", "Half Day"] then "Absent" 
    when ["Half Day", "Present"], ["Half Day", "Early Departure"], ["Late", "Early Departure"] then "Half Day" 
    when ["Present", "Present"] then "Present" 
    . 
    . 
    . 
    end 
end 

我添加了第二時線像卡里Swoveland的意見建議。 when子句中的逗號就像一個或連接詞一​​樣工作。 請參閱case以獲取有關mor信息的文檔。

+0

我不知道我們能做到這一點還有:o –

+1

我建議你添加一個'時/ then'條款,既要完成(沒有'...')並說明一個有用的結構:當[[半天],[現在]],[「半天」,「早出發」],[「半天」,「晚」], –

1

只是有點更大的解決方案,但也許這將是有益的

KEYS = { 
    "Half Day" => 0, 
    "Present" => 1, 
    "Early Departure" => 2, 
    "Late" => 3 
} 

STATUSES = [ 
    { keys: [[0, 0]], value: "Absent" }, 
    { keys: [[0, 2], [2, 3], [0, 1]], value: "Half Day" }, 
    { keys: [[1, 1]], value: "Present" } 
] 

def status(late_warning, early_warning) 
    result = STATUSES.find { |status| status[:keys].include? [KEYS[late_warning], KEYS[early_warning]] } || { value: 'Unknown'} 
    result[:value] 
end 

# For example: 
# status("Half Day", "Half Day") 
#=> "Absent" 
# 
# status("Half Day", "Half D") 
#=> "Unknown" 


def set_status_of_day(late_policy,early_departure_policy) 
    self.status_of_day = status(late_policy.warning_on_late, early_departure_policy.warning_on_late)    
end 
1

你可以指定值(0爲天,1一天結束的開始,...),並計算長度(接近0是「缺席」,接近1是「現在」)。

@morning_hash = { 
    "Present" => 0.0, 
    "Late"  => 0.25, 
    "Half Day" => 0.5, 
} 

@afternoon_hash = { 
    "Half Day" => 0.5, 
    "Early" => 0.75, 
    "Present" => 1.0, 
} 

def duration_description(morning, afternoon) 
    duration = @afternoon_hash[afternoon][email protected]_hash[morning] 
    case duration 
    when 0...0.25 then "Absent" 
    when 0.25..0.75 then "Half Day" 
    when 0.75..1.0 then "Present" 
    end 
end 

puts duration_description("Half Day", "Half Day") == "Absent" 
puts duration_description("Half Day", "Present") == "Half Day" 
puts duration_description("Half Day", "Early") == "Half Day" 
puts duration_description("Late", "Early")  == "Half Day" 
puts duration_description("Present", "Present") == "Present" 

#=> true true true true true