repeatable 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +70 -22
- data/lib/repeatable/expression/base.rb +14 -2
- data/lib/repeatable/expression/day_in_month.rb +4 -0
- data/lib/repeatable/expression/range_in_year.rb +8 -0
- data/lib/repeatable/expression/set.rb +6 -0
- data/lib/repeatable/expression/weekday.rb +4 -0
- data/lib/repeatable/expression/weekday_in_month.rb +4 -0
- data/lib/repeatable/schedule.rb +12 -1
- data/lib/repeatable/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b8c2addf36ece2740c6279e7a62055b85840788
|
4
|
+
data.tar.gz: eb550957ad299c4fc07fbbb2f9cc0dea135bbde1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 020b4553225e0c44e455021dd4b825c52aa5074e3a3204546714f24c643e6eb9bdd69af0103ac82486c840d236595096fca92f2ef5212d85df9492f80e4a0d60
|
7
|
+
data.tar.gz: 9d11e275bb37cea484f635bef9b5b7eb3dc2ca1ea0309132773f98b66d16bfc18572de7c9c5b59db6946391d758eae0e2b0322c6c21714c61edac544f06cbc28
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -24,7 +24,26 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
-
|
27
|
+
### Building a Schedule
|
28
|
+
|
29
|
+
You can create a schedule in one of two ways.
|
30
|
+
|
31
|
+
#### Composed objects
|
32
|
+
|
33
|
+
You can compose each of the expression objects manually.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
second_monday = Repeatabe::Expression::WeekdayInMonth.new(weekday: 1, count: 2)
|
37
|
+
oct_thru_dec = Repeatable::Expression::RangeInYear.new(start_month: 10, end_month: 12)
|
38
|
+
intersection = Repeatable::Expresson::Intersection.new(second_monday, oct_thru_dec)
|
39
|
+
|
40
|
+
schedule = Repeatable::Schedule.new(intersection)
|
41
|
+
```
|
42
|
+
|
43
|
+
|
44
|
+
#### Hash
|
45
|
+
|
46
|
+
Or you can describe the same structure with a `Hash`, and the gem will compose the objects for you.
|
28
47
|
|
29
48
|
```ruby
|
30
49
|
args = {
|
@@ -37,6 +56,53 @@ args = {
|
|
37
56
|
schedule = Repeatable::Schedule.new(args)
|
38
57
|
```
|
39
58
|
|
59
|
+
- - -
|
60
|
+
|
61
|
+
#### Time Expressions
|
62
|
+
|
63
|
+
There are a number of time expressions available which, when combined, can describe most any schedule.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
# SETS
|
67
|
+
|
68
|
+
# Any conditions can be met
|
69
|
+
{ union: [] }
|
70
|
+
Repeatable::Expression::Union.new(expressions)
|
71
|
+
|
72
|
+
# All conditions must be met
|
73
|
+
{ intersection: [] }
|
74
|
+
Repeatable::Expression::Intersection.new(expressions)
|
75
|
+
|
76
|
+
|
77
|
+
# DATES
|
78
|
+
|
79
|
+
# Every Sunday
|
80
|
+
{ weekday: { weekday: 0 } }
|
81
|
+
Repeatable::Expression::Weekday.new(weekday: 0)
|
82
|
+
|
83
|
+
# The 3rd Monday of every month
|
84
|
+
{ weekday_in_month: { weekday: 1, count: 3 } }
|
85
|
+
Repeatable::Expression::WeekdayInMonth.new(weekday: 1, count: 3)
|
86
|
+
|
87
|
+
# The 13th of every month
|
88
|
+
{ day_in_month: { day: 13 } }
|
89
|
+
Repeatable::Expression::DayInMonth.new(day: 13)
|
90
|
+
|
91
|
+
# Any day in October
|
92
|
+
{ range_in_year: { start_month: 10 } }
|
93
|
+
Repeatable::Expression::RangeInYear.new(start_month: 10)
|
94
|
+
|
95
|
+
# All days from October through December
|
96
|
+
{ range_in_year: { start_month: 10, end_month: 12 } }
|
97
|
+
Repeatable::Expression::RangeInYear.new(start_month: 10, end_month: 12)
|
98
|
+
|
99
|
+
# All days from October 1 through December 20
|
100
|
+
{ range_in_year: { start_month: 10, end_month: 12, start_day: 1, end_day: 20 } }
|
101
|
+
Repeatable::Expression::RangeInYear.new(start_month: 10, end_month: 12, start_day: 1, end_day: 20)
|
102
|
+
```
|
103
|
+
|
104
|
+
### Getting information from a Schedule
|
105
|
+
|
40
106
|
Ask your schedule one of three questions:
|
41
107
|
|
42
108
|
```ruby
|
@@ -48,28 +114,10 @@ schedule.occurrences(Date.new(2015, 1, 1), Date.new(2016, 6, 30))
|
|
48
114
|
|
49
115
|
schedule.include?(Date.new(2015, 10, 10))
|
50
116
|
# => Whether the schedule has an event on the date given (true/false)
|
51
|
-
```
|
52
|
-
|
53
|
-
Available time expressions:
|
54
117
|
|
55
|
-
|
56
|
-
#
|
57
|
-
|
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
|
118
|
+
schedule.to_h
|
119
|
+
# => Hash representation of the Schedule, which is useful for storage and
|
120
|
+
# can be used to recreating an identical Schedule object at a later time
|
73
121
|
```
|
74
122
|
|
75
123
|
## Development
|
@@ -2,13 +2,25 @@ module Repeatable
|
|
2
2
|
module Expression
|
3
3
|
class Base
|
4
4
|
def self.===(other)
|
5
|
-
other
|
5
|
+
case other
|
6
|
+
when Class
|
7
|
+
other.ancestors.include?(self)
|
8
|
+
else
|
9
|
+
super
|
10
|
+
end
|
6
11
|
end
|
7
12
|
|
8
13
|
def include?(_date)
|
9
14
|
fail(
|
10
15
|
NotImplementedError,
|
11
|
-
"Don't use Expression::Base directly. Subclasses
|
16
|
+
"Don't use Expression::Base directly. Subclasses must implement `#include?`"
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_h
|
21
|
+
fail(
|
22
|
+
NotImplementedError,
|
23
|
+
"Don't use Expression::Base directly. Subclasses must implement `#to_h`"
|
12
24
|
)
|
13
25
|
end
|
14
26
|
end
|
@@ -12,6 +12,14 @@ module Repeatable
|
|
12
12
|
months_include?(date) || start_month_include?(date) || end_month_include?(date)
|
13
13
|
end
|
14
14
|
|
15
|
+
def to_h
|
16
|
+
args = { start_month: start_month }
|
17
|
+
args[:end_month] = end_month unless end_month == start_month
|
18
|
+
args[:start_day] = start_day unless start_day.zero?
|
19
|
+
args[:end_day] = end_day unless end_day.zero?
|
20
|
+
{ range_in_year: args }
|
21
|
+
end
|
22
|
+
|
15
23
|
private
|
16
24
|
|
17
25
|
attr_reader :start_month, :end_month, :start_day, :end_day
|
data/lib/repeatable/schedule.rb
CHANGED
@@ -3,7 +3,14 @@ require 'active_support/core_ext/string/inflections'
|
|
3
3
|
module Repeatable
|
4
4
|
class Schedule
|
5
5
|
def initialize(args)
|
6
|
-
|
6
|
+
case args
|
7
|
+
when Hash
|
8
|
+
@expression = build_expression(args)
|
9
|
+
when Repeatable::Expression::Base
|
10
|
+
@expression = args
|
11
|
+
else
|
12
|
+
fail ArgumentError, "Can't build a Repeatable::Schedule from #{args.class}"
|
13
|
+
end
|
7
14
|
end
|
8
15
|
|
9
16
|
def occurrences(start_date, end_date)
|
@@ -25,6 +32,10 @@ module Repeatable
|
|
25
32
|
expression.include?(date)
|
26
33
|
end
|
27
34
|
|
35
|
+
def to_h
|
36
|
+
expression.to_h
|
37
|
+
end
|
38
|
+
|
28
39
|
private
|
29
40
|
|
30
41
|
attr_reader :expression
|
data/lib/repeatable/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: repeatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mo Lawson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|