Scheduling Recurring Events With Ice Cube Gem

Ice_cube is a ruby library for effectively taking care of repeated events (schedules). The force lies in the ability to indicate multiple rules, and have ice_cube rapidly make sense of whether the schedule falls on a specific date (.occurs_on?), or what times it happens at (.occurrences, .first, .all_occurrences). How to get ice cube For install use the below syntax gem install if you want to get the code
gem clone git://github.com/seejohnrun/ice_cube
For creating icecube schedule
schedule = IceCube::Schedule.new if we want to speciy startdate and enddate we have option to specify in the above mentioned schedule schedule = IceCube::Schedule.new(start = Time.now, :end_time => start + 600)
Daily schedules After creating schedule we have an option to add recurrence rule for the above mentioned schedule consider “schedule every day” on above mentioned time
schedule.add_recurrence_rule IceCube::Rule.daily
consider the same schedule with repeat “n” number of days
schedule.add_recurrence_rule IceCube::Rule.daily(repeat_every_n_days)
in place of repeat_every_n_days you have option to specify the number of days Weekly schedules Recurring rule to generate weekly schedule
schedule.add_recurrence_rule IceCube::Rule.weekly
recurring rule to add repeat n number of weeks with the same schedule
schedule.add_recurrence_rule IceCube::Rule.weekly(repeat_every_n_weeks)
Consider an example repeat the schedule on only week days (monday to friday)
schedule.add_recurrence_rule IceCube::Rule.weekly.day(1, 2, 3, 4, 5)
Every other week on monday and tuesday
schedule.add_recurrence_rule IceCube::Rule.weekly(2).day(:monday, :tuesday) or you can mention as
schedule.add_recurrence_rule IceCube::Rule.weekly(2).day(:monday, :tuesday)
Monthly schedules repeat the same schedule on every month
schedule.add_recurrence_rule IceCube::Rule.montly
Monthly schedules(by day of Month) every month on the first and last days of the month
schedule.add_recurrence_rule IceCube::Rule.monthly.day_of_month(1, -1)
every other month on the 15th of the month
schedule.add_recurrence_rule IceCube::Rule.monthly(2).day_of_month(15)
Monthly (by day of Nth week) # every month on the first and last tuesdays of the month
schedule.add_recurrence_rule IceCube::Rule.monthly.day_of_week(:tuesday => [1, -1])
# every other month on the first monday and last tuesday
schedule.add_recurrence_rule IceCube::Rule.monthly(2).day_of_week(:monday => [1],:tuesday => [-1])
Yearly(by day of year) # every year on the 100th days from the beginning and end of the year
schedule.add_recurrence_rule IceCube::Rule.yearly.day_of_year(100, -100)
# every fourth year on new year’s eve
schedule.add_recurrence_rule IceCube::Rule.yearly(4).day_of_year(-1)
Yearly (by month of year) # every year on the same day as start_time but in january and february
schedule.add_recurrence_rule IceCube::Rule.yearly.month_of_year(:january, :februrary)
# every third year in march
schedule.add_recurrence_rule IceCube::Rule.yearly(3).month_of_year(:march)
# for programatic convenience (same as above)
schedule.add_recurrence_rule IceCube::Rule.yearly(3).month_of_year(3)
Hourly (by hour of day) # every hour on the same minute and second as start date
schedule.add_recurrence_rule IceCube::Rule.hourly
# every other hour, on mondays
schedule.add_recurrence_rule IceCube::Rule.hourly(2).day(:monday) Minutely (every N minutes)
# every 10 minutes
schedule.add_recurrence_rule IceCube::Rule.minutely(10)
# every hour and a half, on the last tuesday of the month
schedule.add_recurrence_rule IceCube::Rule.minutely(90).day_of_week(:tuesday => [-1])
Secondly (every N seconds) # every second
schedule.add_recurrence_rule IceCube::Rule.secondly
# every 15 seconds between 12:00 – 12:59
schedule.add_recurrence_rule IceCube::Rule.secondly(15).hour_of_day(12)
With ice_cube, you can specify exception times also Example Repeat Every day except tomorrow
schedule = IceCube::Schedule.new(now = Time.now) schedule.add_recurrence_rule(IceCube::Rule.daily) schedule.add_exception_time(now + 1.day) list occurrences until end_time occurrences = schedule.occurrences(end_time)
# or the first (n) occurrences
schedule.first(n)
# or the last (n) occurrences (if the schedule terminates)
schedule.last(n)
Time Zones and ActiveSupport vs Standard Ruby Time Classes ice_cube works great without ActiveSupport but only supports the environment’s single “local” time zone (ENV[‘TZ’]) or UTC. To correctly support multiple time zones (especially for DST), you should require ‘active_support/time’. A schedule’s occurrences will be returned in the same class and time zone as the schedule’s start_time. Schedule start times are supported as:
  • Time.local (default when no time is specified)
  • Time.utc
  • ActiveSupport::TimeWithZone (with Time.zone.now, Time.zone.local, time.in_time_zone(tz))
  • DateTime (deprecated) and Date are converted to a Time.local
Persistence ice_cube implements its own hash-based .to_yaml, so you can quickly (and safely) serialize schedule objects in and out of your data store It also supports serialization to/from ICAL.
yaml = schedule.to_yaml IceCube::Schedule.from_yaml(yaml) hash = schedule.to_hash IceCube::Schedule.from_hash(hash) ical = schedule.to_ical IceCube::Schedule.from_ical(ical)
Using your words ice_cube can provide ical or string representations of individual rules, or the whole schedule.
rule = IceCube::Rule.daily(2).day_of_week(:tuesday => [1, -1], :wednesday => [2])
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’
Also Read : A Detailed Look at Rails 5 Features and Changes : Ruby on Rails vs CakePHP- The Battle Continues : Scraping of Websites using Mechanize Gem RailsCarma has been providing ruby on rails development and consulting services from past 8 years Our developers are well versed in doing all kinds of ruby on rails development work and handling any kind of project. Contact us to know more about our development skills and the projects which we can handle.
Katneni Naga Sai Tejaswi
Sr. Software Developer

Subscribe For Latest Updates

Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish