📜 ⬆️ ⬇️

Gem ice_cube for recurring events

In some projects it is required to allow the user to set up rules for recurring events. Sometimes the rules of events can be quite complicated, for example, “every penultimate day of a month” or “every second Friday of a month before a certain date“. To solve such problems, you can successfully use gem ice_cube .

Gem ice_cube allows you to set rules for recurring events using an iCalendar-like API and serialize / deserialize schedules in YAML and Ruby Hash formats. Ice_cube allows you to store only one schedule, and not immediately generate hundreds of events on before. We chose ice_cube because of the convenient and flexible API, constant updates and bug fixes, popularity among other developers.

Installation gem:

gem install ice_cube 

To create a schedule, you need to use the IceCube :: Schedule class:
')
 require 'rubygems' require 'ice_cube' include IceCube # : # - /   # { # :duration => 3600 -    # :end_time => Time.now + 3600 -   # } schedule = Schedule.new(Date.today) #   schedule.add_recurrence_time(Date.today) #   schedule.add_exception_time(Date.today + 1) 

To create recurrence rules, use the IceCube :: Rule class. Ice_ube supports the rules for daily, weekly, monthly, annual, hourly, every minute and every second repetitions. Some examples of use (a more complete list of examples can be found on the project page):

 #  4-  schedule.add_recurrence_rule Rule.daily(4) #   ,     schedule.add_recurrence_rule Rule.weekly(2).day(:monday, :friday) #  10, 20 ,      schedule.add_recurrence_rule Rule.monthly.day_of_month(10, 20, -1) #  ,       schedule.add_recurrence_rule Rule.monthly.day_of_week( :monday => [1], :tuesday => [-1] ) #  ,  50    100    schedule.add_recurrence_rule Rule.yearly.day_of_year(50, -100) 

Several rules can be combined in one schedule, including exclusive rules:

 #  4-  /     schedule.add_recurrence_rule Rule.daily(4) schedule.add_exception_rule Rule.weekly.day(1, 5) 

For rules, you can set limits on the number of repetitions and up to a certain date:

 #  2- ,   schedule.add_recurrence_rule Rule.daily(2).count(10) #  2- ,    schedule.add_recurrence_rule Rule.daily(2).until(Date.today.next_month - Date.today.day) 

Now, perhaps the most interesting is the schedule requests:

 #    schedule.all_occurrences #       schedule.occurrences((Date.today + 5).to_time) #     schedule.occurs_at?(Time.now) #     schedule.occurs_on?(Date.today) #     schedule.occurs_between?(Time.now, (Date.today + 5).to_time) #    schedule.first schedule.first(3) #    schedule.next_occurrence #  3   schedule.next_occurrences(3) #    schedule.remaining_occurrences 

Serialization of data in YAML / HASH / iCal formats:

 # YAML yaml = schedule.to_yaml Schedule.from_yaml(yaml) # Hash hash = schedule.to_hash Schedule.from_hash(hash) # iCalendar schedule.to_ical 

We use gem ice_cube to implement a calendar of recurring events along with FullCalendar and DelayedJob, an example of use can be found on this page .

Some examples for this article are taken from official documentation. If you have questions or comments, I will be glad to answer them.

References:
github.com/seejohnrun/ice_cube - project page on GitHub.
seejohnrun.github.com/ice_cube/static/ice_cube_ruby_nyc.pdf - presentation ice_cube.
seejohnrun.github.com/ice_cube/static/lsrc_ice_cube.pdf - another presentation.
www.inexfinance.com/en/blog/2012/12/2/gem_ice_cube - English version of this article.
github.com/inex-finance/blog-examples are examples from this article.

Source: https://habr.com/ru/post/161123/


All Articles