Giter VIP home page Giter VIP logo

recurrence's Introduction

Recurrence

A simple library to handle recurring events.

Tests Gem Gem

Installation

gem install recurrence

Usage

require "rubygems"
require "recurrence"

# Daily
r = Recurrence.new(every: :day)
r = Recurrence.new(every: :day, interval: 9)
r = Recurrence.new(every: :day, repeat: 7)
r = Recurrence.daily(options = {})

# Weekly
r = Recurrence.new(every: :week, on: 5)
r = Recurrence.new(every: :week, on: :monday)
r = Recurrence.new(every: :week, on: [:monday, :friday])
r = Recurrence.new(every: :week, on: [:monday, :wednesday, :friday])
r = Recurrence.new(every: :week, on: :friday, interval: 2)
r = Recurrence.new(every: :week, on: :friday, repeat: 4)
r = Recurrence.weekly(on: :thursday)

# Monthly by month day
r = Recurrence.new(every: :month, on: 15)
r = Recurrence.new(every: :month, on: 31)
r = Recurrence.new(every: :month, on: 7, interval: 2)
r = Recurrence.new(every: :month, on: 7, interval: :monthly)
r = Recurrence.new(every: :month, on: 7, interval: :bimonthly)
r = Recurrence.new(every: :month, on: 7, repeat: 6)
r = Recurrence.monthly(on: 31)

# Monthly by week day
r = Recurrence.new(every: :month, on: :first, weekday: :sunday)
r = Recurrence.new(every: :month, on: :third, weekday: :monday)
r = Recurrence.new(every: :month, on: :last,  weekday: :friday)
r = Recurrence.new(every: :month, on: :last,  weekday: :friday, interval: 2)
r = Recurrence.new(every: :month, on: :last,  weekday: :friday, interval: :quarterly)
r = Recurrence.new(every: :month, on: :last,  weekday: :friday, interval: :semesterly)
r = Recurrence.new(every: :month, on: :last,  weekday: :friday, repeat: 3)

# Yearly
r = Recurrence.new(every: :year, on: [7, 4]) # => [month, day]
r = Recurrence.new(every: :year, on: [10, 31], interval: 3)
r = Recurrence.new(every: :year, on: [:jan, 31])
r = Recurrence.new(every: :year, on: [:january, 31])
r = Recurrence.new(every: :year, on: [10, 31], repeat: 3)
r = Recurrence.yearly(on: [:january, 31])

# Limit recurrence
# :starts defaults to Date.current
# :until defaults to 2037-12-31
r = Recurrence.new(every: :day, starts: Date.current)
r = Recurrence.new(every: :day, until: '2010-01-31')
r = Recurrence.new(every: :day, starts: Date.current, until: '2010-01-31')

# Generate a collection of events which always includes a final event with the given through date
# :through defaults to being unset
r = Recurrence.new(every: :day, through: '2010-01-31')
r = Recurrence.new(every: :day, starts: Date.current, through: '2010-01-31')

# Remove a date in the series on the given except date(s)
# :except defaults to being unset
r = Recurrence.new(every: :day, except: '2010-01-31')
r = Recurrence.new(every: :day, except: [Date.current, '2010-01-31'])

# Override the next date handler
r = Recurrence.new(every: :month, on: 1, handler: Proc.new { |day, month, year| raise("Date not allowed!") if year == 2011 && month == 12 && day == 31 })

# Shift the recurrences to maintain dates around boundaries (Jan 31 -> Feb 28 -> Mar 28)
r = Recurrence.new(every: :month, on: 31, shift: true)

# Getting an array with all events
r.events.each {|date| puts date.to_s }  # => Memoized array
r.events!.each {|date| puts date.to_s } # => reset items cache and re-execute it
r.events(starts: '2009-01-01').each {|date| puts date.to_s }
r.events(until: '2009-01-10').each {|date| puts date.to_s }
r.events(through: '2009-01-10').each {|date| puts date.to_s }
r.events(starts: '2009-01-05', until: '2009-01-10').each {|date| puts date.to_s }

# Iterating events
r.each { |date| puts date.to_s } # => Use items method
r.each! { |date| puts date.to_s } # => Use items! method

# Check if a date is included
r.include?(Date.current) # => true or false
r.include?('2008-09-21')

# Get next available date
r.next   # => Keep the original date object
r.next! # => Change the internal date object to the next available date

Troubleshooting

If you're having problems because already have a class/module called Recurrence that is conflicting with this gem, you can require the namespace and create a class that inherits from Recurrence_.

require "recurrence/namespace"

class RecurrentEvent < Recurrence_
end

r = RecurrentEvent.new(every: :day)

If you're using Rails/Bundler or something like that, remember to override the :require option.

# Gemfile
source "https://rubygems.org"

gem "recurrence", require: "recurrence/namespace"

Maintainer

Contributors

License

(The MIT License)

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.

recurrence's People

Contributors

chap avatar fnando avatar josevalim avatar maxim avatar nbibler avatar ricardohsd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

recurrence's Issues

Relative yearly recurrence

I'm not sure if I'm missing the syntax or it isn't support (yet) but I'm trying to create a recurrence for the following case:

Every year on the second Monday of the month September

I've tried the following syntax but it seems it doesn't support :second as day argument, although the every: :month does?

Recurrence.new({:every=>:year, :on=>[:september, :second], :weekday=>:monday})

daylight savings time

Hi, thank you for your work on this project I've been using it for years!

I noticed that daylight savings time is not taken into account if you are making a recurrence spanning multiple years. Is it within the scope of the project to fix this?

Time of day

Hi, and thanks for the awesome work on this gem!

I was wondering if time of day is within the scope of this project. If so, I may have some time to help contribute.

In essence I'd like to be able to do something like

r = Recurrence.new(:every => :day, :at => '07:00:00')

What do you think?

Biweekly events?

Is there anyway to create biweekly events? For example, events on the 15th and 30th of every month.

Support for :except option

(First off, love this library, it has saved us a ton of time.)

We're doing some integration with icalendars, and they allow reoccurring events to specify dates NOT to occur (They call it EXDATE).

I might be missing something, but it seems like this feature is not supported yet.

Since we'll have to write it, how do you guys feel about a syntax like this:

recurrence(:every => :day, :except =>  "2010-11-20")
recurrence(:every => :day, :except =>  ["2010-11-20", "2010-11-21"])

New gem release?

It appears this has not had a release since 2014 despite several commits happening on the default branch since then.

What is preventing cutting a new stable release?

:except not working as expected

Scenario

r = Recurrence.new( {:interval=>3, :starts=>Wed, 31 Aug 2016, :until=>Mon, 31 Oct 2016, :except=>[Sat, 03 Sep 2016, Sun, 04 Sep 2016, Sat, 10 Sep 2016, Sun, 11 Sep 2016, Sat, 17 Sep 2016, Sun, 18 Sep 2016, Sat, 24 Sep 2016, Sun, 25 Sep 2016, Sat, 01 Oct 2016, Sun, 02 Oct 2016, Sat, 08 Oct 2016, Sun, 09 Oct 2016, Sat, 15 Oct 2016, Sun, 16 Oct 2016, Sat, 22 Oct 2016, Sun, 23 Oct 2016, Sat, 29 Oct 2016, Sun, 30 Oct 2016]})

Note: The :except dates are basically excluding all saturday and sunday.

Expected Dates:
Aug 31, Sept 5, Sept 8, Sept 13

Results:
Aug 31, Sept 6, Sept 9, Sept 12

If you can see, it got messed up during Sept 5 instead appearing on Sept 6.

Aug 31 is wednesday so on the 3rd day excluding sat sun should have been Sept 5.

Monthly recurrence skips the first day

When creating a monthly recurrence, the day passed to starts_on is skipped if the on is set to happen on the same day which results in confusing behaviour:

> chosen_start
=> Tue, 19 Apr 2016 10:00:00 BST +01:00
# This works as expected
> Recurrence.new(every: :day, starts: chosen_start, repeat: 3).events
=> [Tue, 19 Apr 2016, Wed, 20 Apr 2016, Thu, 21 Apr 2016]
# This starts on the next month instead of the same one, even though there's no reason why it should
> Recurrence.new(every: :month, on: 19, starts: chosen_start, repeat: 3).events
=> [Thu, 19 May 2016, Sun, 19 Jun 2016, Tue, 19 Jul 2016]

Yearly recurrence

Code

require 'rubygems'
require 'recurrence'

r = Recurrence.new(:every => :year, :on => [:jan, 31])

Error

NoMethodError: undefined method last' for #<Hash:0x1021835c8> from /Library/Ruby/Gems/1.8/gems/recurrence-0.1.3/lib/recurrence/event/yearly.rb:28:invalidate'
from /Library/Ruby/Gems/1.8/gems/recurrence-0.1.3/lib/recurrence/event/base.rb:17:in initialize' from /Library/Ruby/Gems/1.8/gems/recurrence-0.1.3/lib/recurrence/namespace.rb:60:innew'
from /Library/Ruby/Gems/1.8/gems/recurrence-0.1.3/lib/recurrence/namespace.rb:60:in initialize' from (irb):3:innew'
from (irb):3

Querying events using until returns empty list

Problem: When querying for events on a recurrence having recurrence.until = date1, using recurrence.events(until: date2), where date2 >= date1, the second call to events will always fail.

Reproduce:

ruby recurring.rb -v v

# frozen_string_literal: true

require 'recurrence'
require 'test/unit'

class RecurrenceEventsTest < Test::Unit::TestCase
  def recurrence
    Recurrence.new(
      every: :week,
      on: %i[monday tuesday wednesday thursday],
      starts: '2021-10-01',
      until: '2021-10-10'
    )
  end

  # will pass
  test 'events without start_date, end_date' do
    recurrence_model = recurrence

    assert_equal recurrence_model.events, [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6), Date.new(2021, 10, 7)]
    assert_equal recurrence_model.events, [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6), Date.new(2021, 10, 7)]
  end

  # will pass
  test 'events with start_date, end_date within actual end_date' do
    recurrence_model = recurrence

    assert_equal recurrence_model.events(
      starts: '2021-10-04',
      until: '2021-10-06'
    ), [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6)]
    assert_equal recurrence_model.events(
      starts: '2021-10-04',
      until: '2021-10-06'
    ), [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6)]
  end

  # will fail, date2 = date1
  test 'events with start_date, end_date as actual end_date' do
    recurrence_model = recurrence

    assert_equal recurrence_model.events(
      starts: '2021-10-04',
      until: '2021-10-10'
    ), [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6), Date.new(2021, 10, 7)]
    assert_equal recurrence_model.events(
      starts: '2021-10-04',
      until: '2021-10-10'
    ), [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6), Date.new(2021, 10, 7)]
  end

  # will fail, date2 = date1, using events!
  test 'events with start_date, end_date as actual end_date (using events!)' do
    recurrence_model = recurrence

    assert_equal recurrence_model.events!(
      starts: '2021-10-04',
      until: '2021-10-10'
    ), [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6), Date.new(2021, 10, 7)]
    assert_equal recurrence_model.events!(
      starts: '2021-10-04',
      until: '2021-10-10'
    ), [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6), Date.new(2021, 10, 7)]
  end

  # will fail, date2 > date1
  test 'events with start_date, end_date outside end_date' do
    recurrence_model = recurrence

    assert_equal recurrence_model.events(
      starts: '2021-09-30',
      until: '2021-10-15'
    ), [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6), Date.new(2021, 10, 7)]
    assert_equal recurrence_model.events(
      starts: '2021-09-30',
      until: '2021-10-15'
    ), [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6), Date.new(2021, 10, 7)]
  end

  # will fail, date2 > date1, using events!
  test 'events with start_date, end_date outside end_date (using events!)' do
    recurrence_model = recurrence

    assert_equal recurrence_model.events!(
      starts: '2021-09-30',
      until: '2021-10-15'
    ), [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6), Date.new(2021, 10, 7)]
    assert_equal recurrence_model.events!(
      starts: '2021-09-30',
      until: '2021-10-15'
    ), [Date.new(2021, 10, 4), Date.new(2021, 10, 5), Date.new(2021, 10, 6), Date.new(2021, 10, 7)]
  end
end

Version: 1.3.0

Multiple monthly events

Is there anyway to create multiple monthly event? Something like

Recurrence.new every: :month, on: [5, 10, 15]

or maybe the way to create events for every Nth day of the month. For example: for every 3rd day of moth.

Except: Date.today not working

As far as I can tell, except doesn't work with Date.today (Even if it's a string, or inside an array)

r = Recurrence.new(every: :month, on: Date.today.day, interval: 1, except: Date.today)
r.next == Date.today # => true

Let me know if you need any more info

Marshalling and Unmarshalling

Hello @fnando
Thanks for creating this really nice gem to use recurrence. I've spent lots of time trying to create same logic in my project working from database abilities and not recurrence nature of my events. But this gem is what I need.
Though I still need to store initialization information in my database. And for now, I suppose really simple solution is just make store initial string in json.
But what I'm thinking about are some features most people can look for:

  • Marshalling and Unmarshalling it into crontab string and restoring recurrence object from it?
  • making some rails helper, that will work with active_record
    like: mount_recurrence :rec and will return recurrence object on initialization.

I'm really interested in some of these features, though it might be not the best place for that.
So what do you think about that?

Active Support dependency

Nowadays recurrence depends on activesupport ">= 2.1.1". Doing gem install recurrence will install activesupport 3.0.

Maybe it would be a better idea depending on activesupport "~> 2.1"

What do you think?

Exception with default_starts_date

Hi,

Thanks for the work on this gem, it has been quite useful in different projects through the years ๐Ÿ˜…

While doing some tests found a small issue doing Recurrence.default_starts_date, which raises an exception. But if you do Recurrence_.default_starts_date everything works fine. We can also just force the starts option, and everything is fine.

Notice that you've changed default_stars_date class method a couple months ago, which raised this issue when using Recurrence or any class that extends from Recurrence_

Is there any specific reason to have the method like:

def self.default_starts_date
  @default_starts_date.call
end

Instead of directly use the constant:

def self.default_starts_date
  DEFAULT_STARTS_DATE.call
end

If you don't see any issue with the above suggestion, I would be glad to open the PR to fix it.


Edit:

While running the test got the intention for it ๐Ÿ‘

So could it be something like this?

def self.default_starts_date
  @default_starts_date ||= DEFAULT_STARTS_DATE
  @default_starts_date.call
end

Also, this didn't appear on the tests cause in the test_helper.rb#setup is setting it

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.