Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently retrieving ice_cube schedules for a given time period

I'm looking into using Ice Cube https://github.com/seejohnrun/ice_cube for recurring events. My question is, if I then need to get any events that fall within a given time period (say, on a day or within a week), is there any better way than to loop through them all like this:

items = Records.find(:all)
items.each do |item|
  schedule = item.schedule
  if schedule.occurs_on?(Date.new)
      #if today is a recurrence, add to array
  end
end

This seems horribly inefficient but I'm not sure how else to go about it.

like image 299
99miles Avatar asked May 30 '12 22:05

99miles


1 Answers

That's one approach - but what people do more often is end up denormalizing their schedules into a format that is conveniently queryable.

You may have a collection called something like ScheduleOccurrences - that you build each week / and then query that instead.

Its unfortunate it has to work this way, but sticking to the iCal way of managing schedules has led IceCube to need to format its data in certain ways (specifically ways that can line up with the requirements of the iCal RFC).

I've been doing some thinking recently about what a library would look like that shook away some of those restrictions, for greater flexibility like this - but its definitely still a bit off.

Hope this helps

like image 53
John Avatar answered Oct 15 '22 05:10

John