6c13f781b81579be36f2ec1193224db4

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.

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 !

6c13f781b81579be36f2ec1193224db4

ravinggenius

June 20, 2009, June 20, 2009 15:01, permalink

No rating. Login to rate!

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
6c13f781b81579be36f2ec1193224db4

ravinggenius

June 20, 2009, June 20, 2009 15:07, permalink

No rating. Login to rate!

Well it helps to do one's homework. advance doesn't seem to be available outside of Rails either.

E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

June 20, 2009, June 20, 2009 15:20, permalink

No rating. Login to rate!

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
E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

June 20, 2009, June 20, 2009 15:25, permalink

No rating. Login to rate!

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
E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

June 20, 2009, June 20, 2009 15:30, permalink

No rating. Login to rate!

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
6c13f781b81579be36f2ec1193224db4

ravinggenius

June 21, 2009, June 21, 2009 16:35, permalink

No rating. Login to rate!

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?

A8d3f35baafdaea851914b17dae9e1fc

Adam

June 22, 2009, June 22, 2009 02:39, permalink

No rating. Login to rate!

No need to load the entire Rails stack. Just load ActiveSupport.

1
require 'activesupport'

Your refactoring





Format Copy from initial code

or Cancel