gem install ice_cube
ice_cube is a ruby library for easily handling repeated events (schedules). The API is modeled after iCalendar repeated dates. The power lies in the ability to specify multiple rules and dates, and have ice_cube quickly figure out whether the schedule falls on a certain date (.occurs_on?), or what dates it occurs on (.occurrences, .first, .all_occurrences)
Imagine you want:
Every friday the 13th that falls in October
You would write:
With ice_cube, you can specify (in order of precendence)
Exception Dates - To specifically exclude from a schedule
Recurrence Dates - To specifically include in a schedule
Exception Rules - Rules on how to exclude recurring dates in a schedule
Recurrence Rules - Rules on how to include recurring dates in a schedule
EX: Specifying a exception/recurrence date:
schedule = Schedule.new(start_date) schedule.add_recurrence_date(Time.now) schedule.add_exception_date(Time.now + 1) # list all occurrences from start_date to end_date occurrences = schedule.occurrences(end_date) # [Time.now] # or all of the occurrences occurrences = schedule.all_occurrences # [Time.now] # or check just a single day schedule.occurs_at?(Time.now) # [Time.now] # or the first (n) occurrences schedule.first(n) # [Time.now]
ice_cube can provide ical or string representations of individual rules.
rule = Rule.daily(2).day_of_week(:tuesday => [1, -1], :wednesday => ) rule.to_ical # 'FREQ=DAILY;INTERVAL=2;BYDAY=1TU,-1TU,2WE' rule.to_s # 'Every 2 days on the last and 1st Tuesdays and the 2nd Wednesday'
All rules are based off of the schedule's start date. Individual rules may optionally specify an until date, which is a date that that individual rule is no longer effective, or a count (which is how many times maximum you want the rule to be effective)
There are many types of recurrence rules that can be added to a schedule:
# every day schedule.add_recurrence_rule Rule.daily # every third day schedule.add_recurrence_rule Rule.daily(3)
# every week schedule.add_recurrence_rule Rule.weekly # every other week on monday and tuesday schedule.add_recurrence_rule Rule.weekly(2).day(:monday, :tuesday) # for programatic convenience (same as above) schedule.add_recurrence_rule Rule.weekly(2).day(1, 2)
# every month on the first and last days of the month schedule.add_recurrence_rule Rule.monthly.day_of_month(1, -1) # every other month on the 15th of the month schedule.add_recurrence_rule Rule.monthly(2).day_of_month(15)
# every month on the first and last tuesdays of the month schedule.add_recurrence_rule Rule.monthly.day_of_week(:tuesday => [1, -1]) # every other month on the first monday and last tuesday schedule.add_recurrence_rule Rule.monthly(2).day_of_week(:monday => , :tuesday => [-1]) # for programatic convenience (same as above) schedule.add_recurrence_rule Rule.monthly(2).day_of_week(1 => , 2 => [-1])
# every year on the 100th day from the beginning and end of the year schedule.add_recurrence_rule Rule.yearly.day_of_year(100, -100) # every fourth year on new year's eve schedule.add_recurrence_rule Rule.yearly(4).day_of_year(-1)
# every year on the same day as start_date but in january and february schedule.add_recurrence_rule Rule.yearly.month_of_year(:january, :februrary) # every third year in march schedule.add_recurrence_rule Rule.yearly(3).month_of_year(:march) # for programatic convenience (same as above) schedule.add_recurrence_rule Rule.yearly(3).month_of_year(3)
# every hour on the same minute and second as start date schedule.add_recurrence_rule Rule.hourly # every other hour, on mondays schedule.add_recurrence_rule Rule.hourly(2).day(:monday)
# every 10 minutes schedule.add_recurrence_rule Rule.minutely(10) # every hour and a half, on the last tuesday of the month schedule.add_recurrence_rule Rule.minutely(90).day_of_week(:tuesday => [-1])
# every second schedule.add_recurrence_rule Rule.secondly # every 15 seconds between 12 - 12:59 schedule.add_recurrence_rule Rule.secondly(15).hour_of_day(12)
Mat Brown - firstname.lastname@example.org
(The MIT License)
Copyright © 2010 John Crepezzi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.