Recurring events library for Ruby. Enumerable recurrence objects and convenient chainable interface.
Recurring events library for Ruby. Enumerable recurrence objects and convenient chainable interface.
Montrose is an easy-to-use library for defining recurring events in Ruby. It uses a simple chaining system for building enumerable recurrences, inspired heavily by the design principles of HTTP.rb and rule definitions available in Recurrence.
Add this line to your application's Gemfile:
gem "montrose"And then execute:
$ bundleOr install it yourself as:
$ gem install montroseDealing with recurring events is hard. Montrose provides a simple interface for specifying and enumerating recurring events as Time objects for Ruby applications.
More specifically, this project intends to:
What Montrose doesn't do:
Montrose allows you to easily create "recurrence objects" through chaining:
# Every Monday at 10:30am
Montrose.weekly.on(:monday).at("10:30 am")
=> #Each chained recurrence returns a new object so they can be composed and merged. In both examples below, recurrence r4 represents 'every week on Tuesday and Thursday at noon for four occurrences'.
# Example 1 - building recurrence in succession
r1 = Montrose.every(:week)
r2 = r1.on([:tuesday, :thursday])
r3 = r2.at("12 pm")
r4 = r3.total(4)
# Example 2 - merging distinct recurrences
r1 = Montrose.every(:week)
r2 = Montrose.on([:tuesday, :thursday])
r3 = Montrose.at("12 pm")
r4 = r1.merge(r2).merge(r3).total(4)Most recurrence methods accept additional options if you favor the hash-syntax:
Montrose.r(every: :week, on: :monday, at: "10:30 am")
=> #See the docs for Montrose::Chainable for more info on recurrence creation methods.
A Montrose recurrence responds to #events, which returns an Enumerator that can generate timestamps:
r = Montrose.hourly
=> #
r.events
=> #
r.events.take(10)
=> [2016-02-03 18:26:08 -0500,
2016-02-03 19:26:08 -0500,
2016-02-03 20:26:08 -0500,
2016-02-03 21:26:08 -0500,
2016-02-03 22:26:08 -0500,
2016-02-03 23:26:08 -0500,
2016-02-04 00:26:08 -0500,
2016-02-04 01:26:08 -0500,
2016-02-04 02:26:08 -0500,
2016-02-04 03:26:08 -0500]Montrose recurrences are themselves enumerable:
# Every month starting a year from now on Friday the 13th for 5 occurrences
r = Montrose.monthly.starting(1.year.from_now).on(friday: 13).repeat(5)
r.map(&:to_date)
=> [Fri, 13 Oct 2017,
Fri, 13 Apr 2018,
Fri, 13 Jul 2018,
Fri, 13 Sep 2019,
Fri, 13 Dec 2019]Conceptually, recurrences can represent an infinite sequence. When we say
simply "every day", there is no implied ending. It's therefore possible to
create a recurrence that can enumerate forever, so use your Enumerable methods wisely.
# Every day starting now
r = Montrose.daily
# this expression will never complete, Ctrl-c!
r.map(&:to_date)
# use `lazy` enumerator to avoid eager enumeration
r.lazy.map(&:to_date).select { |d| d.mday > 25 }.take(5).to_a
=> [Fri, 26 Feb 2016,
Sat, 27 Feb 2016,
Sun, 28 Feb 2016,
Mon, 29 Feb 2016,
Sat, 26 Mar 2016]It's straightforward to convert a recurrence to a hash and back.
opts = Montrose::Recurrence.new(every: 10.minutes).to_h
=> {:every=>:minute, :interval=>10}
Montrose::Recurrence.new(opts).take(3)
=> [2016-02-03 19:06:07 -0500,
2016-02-03 19:16:07 -0500,
2016-02-03 19:26:07 -0500]A recurrence object must minimally specify a frequency, e.g. :minute, :hour, :day, :week, :month, or, :year, to be viable. Otherwise, you'll see an informative error message when attempting to enumerate the recurrence.
r = Montrose.at("12pm")
=> #
r.each
Montrose::ConfigurationError: Please specify the :every option…Montrose relies on ActiveSupport for DateTime, Date, and Time calculations. As such, configuring ActiveSupport settings should work for Montrose recurrences.
For example, your application can configure the Date "beginning of the week" (docs):
Date.beginning_of_the_week = :sunday
# OR
Date.beginning_of_the_week = :mondaySimilarly in Rails (docs):
config.beginning_of_week = :sunday
# OR
config.beginning_of_week = :mondayChanging these settings may affect the behavior of Montrose weekly recurrences.
It may be necessary to combine several recurrence rules into a single
enumeration of events. For this purpose, there is Montrose::Schedule. To create a schedule of multiple recurrences:
recurrence_1 = Montrose.monthly(day: { friday: [1] })
recurrence_2 = Montrose.weekly(on: :tuesday)
schedule = Montrose::Schedule.build do |s|
s #
schedule.each do |event|
puts event
endInstances of Montrose::Recurrence support the ActiveRecord serialization API so recurrence objects can be marshalled to and from a single database column:
class RecurringEvent < ApplicationRecord
serialize :recurrence, Montrose::Recurrence
endMontrose::Schedule can also be serialized:
class RecurringEvent < ApplicationRecord
serialize :recurrence, Montrose::Schedule
endMontrose is named after the beautifully diverse and artistic neighborhood in Houston, Texas.
Check out following related projects, all of which have provided inspiration for Montrose.
MontroseAfter checking out the repo, run bin/setup to install dependencies. bin/setup will install gems for each gemfile in gemfiles/ against the current Ruby version.
To run tests against all gemfiles for current Ruby:
bin/specTo update installed gems for gemfiles:
bin/updateTo fix lint errors:
bin/standardrb --fixWhen adding a new gemfile to gemfiles/, run bin/setup and commit the generated lock file.
You can also run bin/console for an interactive prompt that will allow you to experiment.
Bug reports and pull requests are welcome on GitHub at https://github.com/rossta/montrose. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
No open issues yet, or sync has not completed.