1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Message def initialize right_now = Time.now case Date.today.wday when 0, 6 # sunday, saturday @is_weekend = true when 1 # monday @is_weekend = (right_now.hour < 8) when 5 # friday @is_weekend = (right_now.hour >= 17) else @is_weekend = false end @next_event = right_now.monday + (@is_weekend ? 1.week + 8.hours : 4.days + 17.hours) end end
Refactorings
No refactoring yet !
ravinggenius
June 20, 2009, June 20, 2009 15:01, permalink
Trying to removed code that isn't native to Ruby. Just have the call to monday on the last line left.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Message def initialize right_now = Time.now case Date.today.wday when 0, 6 # sunday, saturday @is_weekend = true when 1 # monday @is_weekend = (right_now.hour < 8) when 5 # friday @is_weekend = (right_now.hour >= 17) else @is_weekend = false end a = @is_weekend ? { :weeks => 1, :hours => 8 } : { :days => 4, :hours => 17 } @next_event = right_now.monday.advance(a) end end
ravinggenius
June 20, 2009, June 20, 2009 15:07, permalink
Well it helps to do one's homework. advance doesn't seem to be available outside of Rails either.
Martin Plöger
June 20, 2009, June 20, 2009 15:20, permalink
refactor out the @is_weekend-assignment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Message def initialize right_now = Time.now @is_weekend = case Date.today.wday when 0, 6 # sunday, saturday true when 1 # monday right_now.hour < 8 when 5 # friday right_now.hour >= 17 else false end a = @is_weekend ? { :weeks => 1, :hours => 8 } : { :days => 4, :hours => 17 } @next_event = right_now.monday.advance a end end
Martin Plöger
June 20, 2009, June 20, 2009 15:25, permalink
replaced Date.today.wday with right_now.wday.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Message def initialize right_now = Time.now @is_weekend = case right_now.wday when 0, 6 # sunday, saturday true when 1 # monday right_now.hour < 8 when 5 # friday right_now.hour >= 17 else false end a = @is_weekend ? { :weeks => 1, :hours => 8 } : { :days => 4, :hours => 17 } # @next_event = right_now.monday.advance a end end
Martin Plöger
June 20, 2009, June 20, 2009 15:30, permalink
accidently commented out a line and arranged it more compact.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Message def initialize right_now = Time.now @is_weekend = case right_now.wday when 0, 6 then true # sunday, saturday when 1 then right_now.hour < 8 # monday when 5 then right_now.hour >= 17 # friday else false end a = @is_weekend ? { :weeks => 1, :hours => 8 } : { :days => 4, :hours => 17 } @next_event = right_now.monday.advance a end end
ravinggenius
June 21, 2009, June 21, 2009 16:35, permalink
Thank you for your help, but I'd really like to get rid of _monday_ and _advance_. Those methods come with Rails, but I can't use them because Rails is overkill for the site I'm building. Is there any way to replace those without -stealing- borrowing their implementations from Rails?
I have a small Ruby on Rails application that I'm porting to Sinatra. I didn't know it when I wrote the following excerpt, but monday is a Rails method. Anyway the results I am looking for are as follows:
If the current time is between Monday at 08:00 and Friday at 17:00 (the work week), @is_weekend should be false and @next_event should be the nearest Friday at 5:00 in the future.
If the current time is between Friday at 17:00 and Monday at 8:00 (the weekend), @is_weekend should be true and @next_event should be the nearest Monday at 08:00 in the future.
I am not terribly concerned with time zone adjustments at the moment, but I will be later on.