repeatable 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -9
- data/lib/repeatable/parse_error.rb +4 -0
- data/lib/repeatable/parser.rb +44 -0
- data/lib/repeatable/schedule.rb +6 -27
- data/lib/repeatable/version.rb +1 -1
- data/lib/repeatable.rb +3 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85f2e7cec322abe280bc2d383c2eb3286685e9ce
|
4
|
+
data.tar.gz: b9495fe2fc4044b096d98734ef8521aaff27439e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb4e21f11667e4ef609ee9af000a24a83010119cd53fe00c8068272db8fa250b623eeedc77f137dde683edb71d626401bae6cbf0813fba8f43d44e3a0ba93cce
|
7
|
+
data.tar.gz: f6b85f9183fc1e052d5aaefcc6b57f7e52334cbc6685f91e5c930d3be33631af2229b9baa7e717e645d83f83f9b17eedeb9e75a70b42220b58dec677657590cf
|
data/README.md
CHANGED
@@ -30,7 +30,7 @@ You can create a schedule in one of two ways.
|
|
30
30
|
|
31
31
|
#### Composed objects
|
32
32
|
|
33
|
-
|
33
|
+
Instantiate and compose each of the `Repeatable::Expression` objects manually.
|
34
34
|
|
35
35
|
```ruby
|
36
36
|
second_monday = Repeatabe::Expression::WeekdayInMonth.new(weekday: 1, count: 2)
|
@@ -43,17 +43,17 @@ schedule = Repeatable::Schedule.new(intersection)
|
|
43
43
|
|
44
44
|
#### Hash
|
45
45
|
|
46
|
-
Or
|
46
|
+
Or describe the same structure with a `Hash`, and the gem will handle instantiating and composing the objects.
|
47
47
|
|
48
48
|
```ruby
|
49
|
-
|
50
|
-
intersection: [
|
51
|
-
weekday_in_month: { weekday: 1, count: 2 }
|
52
|
-
range_in_year: { start_month: 10, end_month: 12 }
|
49
|
+
arg = {
|
50
|
+
intersection: [
|
51
|
+
{ weekday_in_month: { weekday: 1, count: 2 } },
|
52
|
+
{ range_in_year: { start_month: 10, end_month: 12 } }
|
53
53
|
]
|
54
54
|
}
|
55
55
|
|
56
|
-
schedule = Repeatable::Schedule.new(
|
56
|
+
schedule = Repeatable::Schedule.new(arg)
|
57
57
|
```
|
58
58
|
|
59
59
|
- - -
|
@@ -88,7 +88,7 @@ Repeatable::Expression::WeekdayInMonth.new(weekday: 1, count: 3)
|
|
88
88
|
{ day_in_month: { day: 13 } }
|
89
89
|
Repeatable::Expression::DayInMonth.new(day: 13)
|
90
90
|
|
91
|
-
#
|
91
|
+
# All days in October
|
92
92
|
{ range_in_year: { start_month: 10 } }
|
93
93
|
Repeatable::Expression::RangeInYear.new(start_month: 10)
|
94
94
|
|
@@ -103,7 +103,7 @@ Repeatable::Expression::RangeInYear.new(start_month: 10, end_month: 12, start_da
|
|
103
103
|
|
104
104
|
### Getting information from a Schedule
|
105
105
|
|
106
|
-
Ask
|
106
|
+
Ask a schedule one of three questions.
|
107
107
|
|
108
108
|
```ruby
|
109
109
|
schedule.next_occurrence
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Repeatable
|
2
|
+
class Parser
|
3
|
+
def initialize(hash)
|
4
|
+
@hash = hash
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.call(hash)
|
8
|
+
new(hash).call
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
build_expression(hash)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
attr_reader :hash
|
18
|
+
|
19
|
+
def build_expression(hash)
|
20
|
+
if hash.length != 1
|
21
|
+
fail(ParseError, "Invalid expression: '#{hash}' must have single key and value")
|
22
|
+
else
|
23
|
+
expression_for(*hash.first)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def expression_for(key, value)
|
28
|
+
klass = "repeatable/expression/#{key}".classify.safe_constantize
|
29
|
+
case klass
|
30
|
+
when nil
|
31
|
+
fail(ParseError, "Unknown mapping: Can't map key '#{key.inspect}' to an expression class")
|
32
|
+
when Repeatable::Expression::Set
|
33
|
+
args = value.map { |hash| build_expression(hash) }
|
34
|
+
klass.new(*args)
|
35
|
+
else
|
36
|
+
klass.new(symbolize_keys(value))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def symbolize_keys(hash)
|
41
|
+
hash.each_with_object({}) { |(k, v), a| a[k.to_sym] = v }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/repeatable/schedule.rb
CHANGED
@@ -2,14 +2,14 @@ require 'active_support/core_ext/string/inflections'
|
|
2
2
|
|
3
3
|
module Repeatable
|
4
4
|
class Schedule
|
5
|
-
def initialize(
|
6
|
-
case
|
7
|
-
when Hash
|
8
|
-
@expression = build_expression(args)
|
5
|
+
def initialize(arg)
|
6
|
+
case arg
|
9
7
|
when Repeatable::Expression::Base
|
10
|
-
@expression =
|
8
|
+
@expression = arg
|
9
|
+
when Hash
|
10
|
+
@expression = Parser.call(arg)
|
11
11
|
else
|
12
|
-
fail
|
12
|
+
fail(ArgumentError, "Can't build a Repeatable::Schedule from #{arg.class}")
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -39,26 +39,5 @@ module Repeatable
|
|
39
39
|
private
|
40
40
|
|
41
41
|
attr_reader :expression
|
42
|
-
|
43
|
-
def build_expression(hash)
|
44
|
-
if hash.length != 1
|
45
|
-
fail "Invalid expression: '#{hash}' should have single key and value"
|
46
|
-
else
|
47
|
-
expression_for(*hash.first)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def expression_for(key, value)
|
52
|
-
klass = "repeatable/expression/#{key}".classify.safe_constantize
|
53
|
-
case klass
|
54
|
-
when nil
|
55
|
-
fail "Unknown mapping: Can't map key '#{key.inspect}' to an expression class"
|
56
|
-
when Repeatable::Expression::Set
|
57
|
-
args = value.map { |hash| build_expression(hash) }
|
58
|
-
klass.new(*args)
|
59
|
-
else
|
60
|
-
klass.new(value)
|
61
|
-
end
|
62
|
-
end
|
63
42
|
end
|
64
43
|
end
|
data/lib/repeatable/version.rb
CHANGED
data/lib/repeatable.rb
CHANGED
@@ -8,6 +8,8 @@ include Repeatable::Conversions
|
|
8
8
|
module Repeatable
|
9
9
|
end
|
10
10
|
|
11
|
+
require 'repeatable/parse_error'
|
12
|
+
|
11
13
|
require 'repeatable/expression'
|
12
14
|
require 'repeatable/expression/base'
|
13
15
|
|
@@ -21,3 +23,4 @@ require 'repeatable/expression/union'
|
|
21
23
|
require 'repeatable/expression/intersection'
|
22
24
|
|
23
25
|
require 'repeatable/schedule'
|
26
|
+
require 'repeatable/parser'
|
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.2.
|
4
|
+
version: 0.2.1
|
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-03-
|
11
|
+
date: 2015-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -94,6 +94,8 @@ files:
|
|
94
94
|
- lib/repeatable/expression/union.rb
|
95
95
|
- lib/repeatable/expression/weekday.rb
|
96
96
|
- lib/repeatable/expression/weekday_in_month.rb
|
97
|
+
- lib/repeatable/parse_error.rb
|
98
|
+
- lib/repeatable/parser.rb
|
97
99
|
- lib/repeatable/schedule.rb
|
98
100
|
- lib/repeatable/version.rb
|
99
101
|
- repeatable.gemspec
|