Coder Social home page Coder Social logo

bansalnagesh / recurrence Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fnando/recurrence

0.0 1.0 0.0 185 KB

A simple library that handles recurring events.

Home Page: http://github.com/fnando/recurrence

License: MIT License

Ruby 100.00%

recurrence's Introduction

Recurrence

A simple library to handle recurring events.

Travis-CI CodeClimate 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.today
# :until defaults to 2037-12-31
r = Recurrence.new(:every => :day, :starts => Date.today)
r = Recurrence.new(:every => :day, :until => '2010-01-31')
r = Recurrence.new(:every => :day, :starts => Date.today, :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.today, :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.today, '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.today) # => 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

Watchers

 avatar

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.