Sunday, 18 August 2013

Create an array of users based on each user's weekday and time attributes

Create an array of users based on each user's weekday and time attributes

I have the following code which creates an array of users depending on the
"global" weekdays and times.
def current_time
# server_time = Time.now
server_time = Time.local(2013,8,19,8,30) # for time simulation
local_offset = (2 * 60 * 60)
local_time = server_time + local_offset
end
def hours_mon_to_thurs?(date)
["Monday", "Tuesday", "Wednesday",
"Thursday"].include?(date.strftime("%A")) &&
("08:30"..."17:00").include?(date.strftime("%H:%M"))
end
def hours_fri?(date)
["Friday"].include?(date.strftime("%A")) &&
("08:30"..."15:00").include?(date.strftime("%H:%M"))
end
def hours_lunch?(date)
!("12:00"..."13:00").include?(date.strftime("%H:%M"))
end
if hours_mon_to_thurs?(current_time) && hours_lunch?(current_time)
p "[USER1, USER2]"
elsif hours_fri?(current_time) && hours_lunch?(current_time)
p "USER2"
else
p "no users"
end
This code works, but I would like to change how the array is created, so
that each user has workdays and times as attributes and if they are
available then they should be added to the array. Let me provide an
example.
User 1 - Office Hours
Monday 08:30 - 12:00 13:00 - 17:00
Tuesday 08:30 - 12:00 13:00 - 17:00
Wednesday 08:30 - 12:00 13:00 - 17:00
Thursday 08:30 - 12:00 13:00 - 17:00
USER2 - Office Hours
Tuesday 08:30 - 12:00 13:00 - 17:00
Thursday 08:30 - 12:00 13:00 - 17:00
Friday 08:30 - 12:00 13:00 - 15:00
With each user's office hours defined I want to have a similar method as
in my example above. Something like this:
for each user
if current time is within user's office hours
add user to array
else if current time is not within users' office hours
array is empty
I'm not using a database at the moment and I don't know the best way to
structure each user's office hours in such a way as to match if the
current time is within a user's time range and then add them to an array.

No comments:

Post a Comment