repeatable 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2f40e8650290808378222b5008cbb78fdbea93aa
4
+ data.tar.gz: 185957b9333e449b91b843ea84517cf09019f33a
5
+ SHA512:
6
+ metadata.gz: e296b0cd1a654e675e09026b7c6672f72a5737819f3e35d8d784b5e5977f7f177747d96aff2f54291e177e3914ef02d7778f2a1d9cb37cc23100bfd414645381
7
+ data.tar.gz: aef5189d0560f22de05c6c7518457f5628f2c2132f4cb96e38467597c1e8929e15aa0a91e8681bdc17d50e4995fd96a8383d19a8e472ca34e6c33345f3ad8d3b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.2.0
5
+ addons:
6
+ code_climate:
7
+ repo_token:
8
+ secure: fyZ7Ycc23fzfh8bBiqPUhlJGcIcnanZWqVc4nsKDJeE1G5ScW01+ot1g4miDWxo7w80gKRKP/PfoYf6lOQ1B5yJJCV0Z5fjoTW3y+EKksMuGNCFaWh8R73MIPlVtfOazGyI2t3l4zDWoik906wHHNQJFveMZawvZGrVMWF6YxKg=
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in repeatable.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
7
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Mo Lawson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # Repeatable
2
+
3
+ [![Build Status](https://img.shields.io/travis/molawson/repeatable.svg)](https://travis-ci.org/molawson/repeatable)
4
+ [![Code Climate](https://img.shields.io/codeclimate/github/molawson/repeatable.svg)](https://codeclimate.com/github/molawson/repeatable)
5
+ [![Code Climate Coverage](https://img.shields.io/codeclimate/coverage/github/molawson/repeatable.svg)](https://codeclimate.com/github/molawson/repeatable)
6
+
7
+ Ruby implementation of Martin Fowler's [Recurring Events for Calendars](http://martinfowler.com/apsupp/recurring.pdf) paper.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'repeatable'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install repeatable
24
+
25
+ ## Usage
26
+
27
+ Describe a schedule with a Hash:
28
+
29
+ ```ruby
30
+ args = {
31
+ intersection: [ # All included conditions must be met
32
+ weekday_in_month: { weekday: 1, count: 2 } # The second Monday of every month
33
+ range_in_year: { start_month: 10, end_month: 12 } # October through December
34
+ ]
35
+ }
36
+
37
+ schedule = Repeatable::Schedule.new(args)
38
+ ```
39
+
40
+ Ask your schedule one of three questions:
41
+
42
+ ```ruby
43
+ schedule.next_occurrence
44
+ # => Date of next occurrence
45
+
46
+ schedule.occurrences(Date.new(2015, 1, 1), Date.new(2016, 6, 30))
47
+ # => Dates of all occurrences between Jan 1, 2015 and June 30, 2016
48
+
49
+ schedule.include?(Date.new(2015, 10, 10))
50
+ # => Whether the schedule has an event on the date given (true/false)
51
+ ```
52
+
53
+ Available time expressions:
54
+
55
+ ```ruby
56
+ # Sets
57
+ union: [] # Any conditions can be met
58
+ intersection: [] # All conditions must be met
59
+
60
+ # Dates
61
+ weekday: { weekday: 0 }
62
+ # Every Sunday
63
+ weekday_in_month: { weekday: 1, count: 3 }
64
+ # The 3rd Monday of every month
65
+ day_in_month: { day: 13 }
66
+ # The 13th of every month
67
+ range_in_year: { start_month: 10 }
68
+ # Any day in October
69
+ range_in_year: { start_month: 10, end_month: 12 }
70
+ # All days from October through December
71
+ range_in_year: { start_month: 10, end_month: 12, start_day: 1, end_day: 20 }
72
+ # All days from October 1 through December 20
73
+ ```
74
+
75
+ ## Development
76
+
77
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
78
+
79
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it ( https://github.com/molawson/repeatable/fork )
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'repeatable'
5
+
6
+ require 'pry'
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ module Repeatable
2
+ module Conversions
3
+ module_function
4
+
5
+ def Date(arg)
6
+ case arg
7
+ when Date, Time
8
+ arg.to_date
9
+ else
10
+ Date.parse(arg)
11
+ end
12
+ rescue ArgumentError
13
+ raise TypeError, "Cannot convert #{arg.inspect} to Date"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module Repeatable
2
+ module Expression
3
+ class Base
4
+ def self.===(other)
5
+ other.ancestors.include?(self)
6
+ end
7
+
8
+ def include?(_date)
9
+ fail(
10
+ NotImplementedError,
11
+ "Don't use Expression::Base directly. Subclasses should implement `#include?`"
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module Repeatable
2
+ module Expression
3
+ class DayInMonth < Base
4
+ def initialize(day:)
5
+ @day = day
6
+ end
7
+
8
+ def include?(date)
9
+ date.day == day
10
+ end
11
+
12
+ private
13
+
14
+ attr_reader :day
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module Repeatable
2
+ module Expression
3
+ class Intersection < Set
4
+ def include?(date)
5
+ elements.all? { |e| e.include?(date) }
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,32 @@
1
+ module Repeatable
2
+ module Expression
3
+ class RangeInYear < Base
4
+ def initialize(start_month:, end_month: start_month, start_day: 0, end_day: 0)
5
+ @start_month = start_month
6
+ @end_month = end_month
7
+ @start_day = start_day
8
+ @end_day = end_day
9
+ end
10
+
11
+ def include?(date)
12
+ months_include?(date) || start_month_include?(date) || end_month_include?(date)
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :start_month, :end_month, :start_day, :end_day
18
+
19
+ def months_include?(date)
20
+ date.month > start_month && date.month < end_month
21
+ end
22
+
23
+ def start_month_include?(date)
24
+ date.month == start_month && (start_day == 0 || date.day >= start_day)
25
+ end
26
+
27
+ def end_month_include?(date)
28
+ date.month == end_month && (end_day == 0 || date.day <= end_day)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ module Repeatable
2
+ module Expression
3
+ class Set < Base
4
+ def initialize(*elements)
5
+ @elements = elements.flatten
6
+ end
7
+
8
+ def <<(element)
9
+ elements << element
10
+ self
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :elements
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module Repeatable
2
+ module Expression
3
+ class Union < Set
4
+ def include?(date)
5
+ elements.any? { |e| e.include?(date) }
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module Repeatable
2
+ module Expression
3
+ class Weekday < Base
4
+ def initialize(weekday:)
5
+ @weekday = weekday
6
+ end
7
+
8
+ def include?(date)
9
+ date.wday == weekday
10
+ end
11
+
12
+ private
13
+
14
+ attr_reader :weekday
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module Repeatable
2
+ module Expression
3
+ class WeekdayInMonth < Base
4
+ def initialize(weekday:, count:)
5
+ @weekday = weekday
6
+ @count = count
7
+ end
8
+
9
+ def include?(date)
10
+ day_matches?(date) && week_matches?(date)
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :weekday, :count
16
+
17
+ def day_matches?(date)
18
+ date.wday == weekday
19
+ end
20
+
21
+ def week_matches?(date)
22
+ week_in_month(date.day) == count
23
+ end
24
+
25
+ def week_in_month(day)
26
+ ((day - 1) / 7) + 1
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ module Repeatable
2
+ module Expression
3
+ end
4
+ end
@@ -0,0 +1,53 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+
3
+ module Repeatable
4
+ class Schedule
5
+ def initialize(args)
6
+ @expression = build_expression(args)
7
+ end
8
+
9
+ def occurrences(start_date, end_date)
10
+ start_date = Date(start_date)
11
+ end_date = Date(end_date)
12
+ (start_date..end_date).select { |date| include?(date) }
13
+ end
14
+
15
+ def next_occurrence(start_date = Date.today)
16
+ date = Date(start_date)
17
+ until include?(date)
18
+ date = date.next_day
19
+ end
20
+ date
21
+ end
22
+
23
+ def include?(date = Date.today)
24
+ date = Date(date)
25
+ expression.include?(date)
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :expression
31
+
32
+ def build_expression(hash)
33
+ if hash.length != 1
34
+ fail "Invalid expression: '#{hash}' should have single key and value"
35
+ else
36
+ expression_for(*hash.first)
37
+ end
38
+ end
39
+
40
+ def expression_for(key, value)
41
+ klass = "repeatable/expression/#{key}".classify.safe_constantize
42
+ case klass
43
+ when nil
44
+ fail "Unknown mapping: Can't map key '#{key.inspect}' to an expression class"
45
+ when Repeatable::Expression::Set
46
+ args = value.map { |hash| build_expression(hash) }
47
+ klass.new(*args)
48
+ else
49
+ klass.new(value)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Repeatable
2
+ VERSION = '0.1.0'
3
+ end
data/lib/repeatable.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'date'
2
+
3
+ require 'repeatable/version'
4
+
5
+ require 'repeatable/conversions'
6
+ include Repeatable::Conversions
7
+
8
+ module Repeatable
9
+ end
10
+
11
+ require 'repeatable/expression'
12
+ require 'repeatable/expression/base'
13
+
14
+ require 'repeatable/expression/weekday'
15
+ require 'repeatable/expression/weekday_in_month'
16
+ require 'repeatable/expression/day_in_month'
17
+ require 'repeatable/expression/range_in_year'
18
+
19
+ require 'repeatable/expression/set'
20
+ require 'repeatable/expression/union'
21
+ require 'repeatable/expression/intersection'
22
+
23
+ require 'repeatable/schedule'
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'repeatable/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'repeatable'
7
+ spec.version = Repeatable::VERSION
8
+ spec.authors = ['Mo Lawson']
9
+ spec.email = ['mo@molawson.com']
10
+
11
+ spec.summary = 'Describe recurring event schedules and calculate their occurrences'
12
+ spec.description = "Ruby implementation of Martin Fowler's 'Recurring Events for Calendars' paper."
13
+ spec.homepage = 'https://github.com/molawson/repeatable'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'activesupport', '>= 3.0'
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: repeatable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mo Lawson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Ruby implementation of Martin Fowler's 'Recurring Events for Calendars'
70
+ paper.
71
+ email:
72
+ - mo@molawson.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/repeatable.rb
87
+ - lib/repeatable/conversions.rb
88
+ - lib/repeatable/expression.rb
89
+ - lib/repeatable/expression/base.rb
90
+ - lib/repeatable/expression/day_in_month.rb
91
+ - lib/repeatable/expression/intersection.rb
92
+ - lib/repeatable/expression/range_in_year.rb
93
+ - lib/repeatable/expression/set.rb
94
+ - lib/repeatable/expression/union.rb
95
+ - lib/repeatable/expression/weekday.rb
96
+ - lib/repeatable/expression/weekday_in_month.rb
97
+ - lib/repeatable/schedule.rb
98
+ - lib/repeatable/version.rb
99
+ - repeatable.gemspec
100
+ homepage: https://github.com/molawson/repeatable
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.5
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Describe recurring event schedules and calculate their occurrences
124
+ test_files: []