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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b8c2addf36ece2740c6279e7a62055b85840788
4
- data.tar.gz: eb550957ad299c4fc07fbbb2f9cc0dea135bbde1
3
+ metadata.gz: 85f2e7cec322abe280bc2d383c2eb3286685e9ce
4
+ data.tar.gz: b9495fe2fc4044b096d98734ef8521aaff27439e
5
5
  SHA512:
6
- metadata.gz: 020b4553225e0c44e455021dd4b825c52aa5074e3a3204546714f24c643e6eb9bdd69af0103ac82486c840d236595096fca92f2ef5212d85df9492f80e4a0d60
7
- data.tar.gz: 9d11e275bb37cea484f635bef9b5b7eb3dc2ca1ea0309132773f98b66d16bfc18572de7c9c5b59db6946391d758eae0e2b0322c6c21714c61edac544f06cbc28
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
- You can compose each of the expression objects manually.
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 you can describe the same structure with a `Hash`, and the gem will compose the objects for you.
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
- args = {
50
- intersection: [ # All included conditions must be met
51
- weekday_in_month: { weekday: 1, count: 2 } # The second Monday of every month
52
- range_in_year: { start_month: 10, end_month: 12 } # October through December
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(args)
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
- # Any day in October
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 your schedule one of three questions:
106
+ Ask a schedule one of three questions.
107
107
 
108
108
  ```ruby
109
109
  schedule.next_occurrence
@@ -0,0 +1,4 @@
1
+ module Repeatable
2
+ class ParseError < StandardError
3
+ end
4
+ end
@@ -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
@@ -2,14 +2,14 @@ require 'active_support/core_ext/string/inflections'
2
2
 
3
3
  module Repeatable
4
4
  class Schedule
5
- def initialize(args)
6
- case args
7
- when Hash
8
- @expression = build_expression(args)
5
+ def initialize(arg)
6
+ case arg
9
7
  when Repeatable::Expression::Base
10
- @expression = args
8
+ @expression = arg
9
+ when Hash
10
+ @expression = Parser.call(arg)
11
11
  else
12
- fail ArgumentError, "Can't build a Repeatable::Schedule from #{args.class}"
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
@@ -1,3 +1,3 @@
1
1
  module Repeatable
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
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.0
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-03 00:00:00.000000000 Z
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