views:

34

answers:

2

I'm creating a Rails app that will store the opening and closing hours for a business. Originally, I thought of simply using a text data type and letting it be free-form:

"Monday to Friday 9am to 5pm
Saturday 11am to 4pm
Closed Sundays"

But, requirements have changed and I need to check the hours against the current date & time and display an "Open" or "Closed" in the view. Something like:

class Business < ActiveRecord::Base

  def open?
    # Something like ... 
    Time.now > open_time && Time.now < close_time
  end

end

So what would be the best way to tackle this in terms of storing the hours for each day of the week? Should the Business simply has_many :open_blocks (or whatever) that have open and close times? Should I just store the day as a string?

+1  A: 

Checkout these libraries for handling recurring dates.

Once the recurrence is setup in an object say office_hours, in ice_cube you can query it as:

office_hours.occurring_at?(Time.now)
Anurag
Ok, ice_cube looked good but then I realized it only stores recurring "start times" of events. It's not really designed for recurring blocks of start and end blocks of time. Still searching ...
Callmeed
A: 

This was the subject of a recent RubyLearning challenge - might be helpful :)

Sam Phillips
Thanks, I'll check it out ...
Callmeed