views:

28

answers:

1

Hello. I'm creating a very simple timeshare application using Sinatra and Datamapper. Every user in the application will have n reservations and for the time being each reservation runs from Monday to Sunday and there can only be one reservation per week.

Now I will need a view with a textbox (and label) for each week of the year where the users will put their name (through autocompletion or something) and thereby creating a reservation for that week. And if the week is reserved the name will of course be filled in the textbox (and disabled)

It would be something like

weeks.each do
  find user that has reserved this week - and create a textbox
end

So my question I guess is as simple - how do I loop through all weeks of a year in Ruby?

Or would it be a better solution to just loop 52 times and make an array for each user with the numbers of reserved weeks in it?

+1  A: 
(1..52).each do |week|
   # find user that has reserved this week - and create a textbox
end
John Drummond
I edited my question before I saw your answer :) So that is how I do it. But...how do I find the week number for the reserved dates - if I later want to be able to reserved from say only monday to thursday?
theory
Store the week number as a field in the reservation instead of date ranges.
John Drummond
So is there a way in Ruby to calculate the week number according to the ISO-8601 standard?
theory
I think you can use Date#cweek : http://www.ruby-doc.org/core/classes/Date.html#M000659
John Drummond
Aah...thank you. That's what I was looking for. So I'll use that function to figure out what week January 31st is for any given year and loop either 52 or 53 times. Thanks a lot.
theory