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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f40e8650290808378222b5008cbb78fdbea93aa
4
- data.tar.gz: 185957b9333e449b91b843ea84517cf09019f33a
3
+ metadata.gz: 2b8c2addf36ece2740c6279e7a62055b85840788
4
+ data.tar.gz: eb550957ad299c4fc07fbbb2f9cc0dea135bbde1
5
5
  SHA512:
6
- metadata.gz: e296b0cd1a654e675e09026b7c6672f72a5737819f3e35d8d784b5e5977f7f177747d96aff2f54291e177e3914ef02d7778f2a1d9cb37cc23100bfd414645381
7
- data.tar.gz: aef5189d0560f22de05c6c7518457f5628f2c2132f4cb96e38467597c1e8929e15aa0a91e8681bdc17d50e4995fd96a8383d19a8e472ca34e6c33345f3ad8d3b
6
+ metadata.gz: 020b4553225e0c44e455021dd4b825c52aa5074e3a3204546714f24c643e6eb9bdd69af0103ac82486c840d236595096fca92f2ef5212d85df9492f80e4a0d60
7
+ data.tar.gz: 9d11e275bb37cea484f635bef9b5b7eb3dc2ca1ea0309132773f98b66d16bfc18572de7c9c5b59db6946391d758eae0e2b0322c6c21714c61edac544f06cbc28
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ /*.gem
1
2
  /.bundle/
2
3
  /.yardoc
3
4
  /Gemfile.lock
data/README.md CHANGED
@@ -24,7 +24,26 @@ Or install it yourself as:
24
24
 
25
25
  ## Usage
26
26
 
27
- Describe a schedule with a Hash:
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
- ```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
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.ancestors.include?(self)
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 should implement `#include?`"
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
@@ -9,6 +9,10 @@ module Repeatable
9
9
  date.day == day
10
10
  end
11
11
 
12
+ def to_h
13
+ { day_in_month: { day: day } }
14
+ end
15
+
12
16
  private
13
17
 
14
18
  attr_reader :day
@@ -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
@@ -10,6 +10,12 @@ module Repeatable
10
10
  self
11
11
  end
12
12
 
13
+ def to_h
14
+ hash = {}
15
+ hash[self.class.name.demodulize.underscore.to_sym] = elements.map(&:to_h)
16
+ hash
17
+ end
18
+
13
19
  private
14
20
 
15
21
  attr_reader :elements
@@ -9,6 +9,10 @@ module Repeatable
9
9
  date.wday == weekday
10
10
  end
11
11
 
12
+ def to_h
13
+ { weekday: { weekday: weekday } }
14
+ end
15
+
12
16
  private
13
17
 
14
18
  attr_reader :weekday
@@ -10,6 +10,10 @@ module Repeatable
10
10
  day_matches?(date) && week_matches?(date)
11
11
  end
12
12
 
13
+ def to_h
14
+ { weekday_in_month: { weekday: weekday, count: count } }
15
+ end
16
+
13
17
  private
14
18
 
15
19
  attr_reader :weekday, :count
@@ -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
- @expression = build_expression(args)
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
@@ -1,3 +1,3 @@
1
1
  module Repeatable
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
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.1.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-02-23 00:00:00.000000000 Z
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport