2

I have to create logic for setting status of day which is based on multiple conditions. I started out by creating multiple if else statement but it does not feel right. Please help with right approach.

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

Thanks

3 Answers 3

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

I added the second when-line like Cary Swoveland suggested in the comments. The commas in the when clause work like an or-conjuction. See case in the docs for mor information.

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't know we can do this as well :O
I suggest you add one more when/then clause, both to be complete (no ...) and to illustrate a useful construct: when ["Half Day", "Present"], ["Half Day", "Early Departure"], ["Half Day", "Late"] then "Half Day"
1

Just a bit bigger solution, but maybe it would be helpful

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

Comments

1

You could assign values (0 for start of the day, 1 for end of the day, ...) and calculate the length of the day (close to 0 is "Absent", close to 1 is "Present").

@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]-@morning_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

~

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.