upcoming 0.1.0 → 0.2.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 +4 -4
- data/LICENSE.md +1 -1
- data/README.md +30 -9
- data/lib/upcoming/factory.rb +5 -6
- data/lib/upcoming/generators/generator.rb +2 -6
- data/lib/upcoming/version.rb +1 -1
- data/spec/factory_spec.rb +5 -7
- data/spec/generators/generator_spec.rb +20 -16
- data/spec/generators/last_day_of_month_generator_spec.rb +8 -5
- data/spec/generators/working_day_generator_spec.rb +2 -4
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f262a85c40d0c408b913135974601da00d4d89d1
|
4
|
+
data.tar.gz: 6dbac7937346f821d7c0aa7611d7f1d5954b7a00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1be5ca77077ce49852a700796164db3cd94611e535fa81139fa69db513257baeab5db0dd80b61273a34e59658a3d00b5f3d74065e7120996b51a027a68e4c932
|
7
|
+
data.tar.gz: 746ef6accdd1c981004b49819d61cd0796297abbc925d9e92a6716efc4cae621c6b353abeb11a3dcce50325adefe60199a73f429b2f262215c0f00e6a736ecb5
|
data/LICENSE.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (C) 2014 David Lantos
|
1
|
+
Copyright (C) 2014-2016 David Lantos
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
4
|
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# upcoming
|
2
2
|
|
3
|
-
[](http://badge.fury.io/rb/upcoming)
|
4
4
|
[](https://travis-ci.org/sldblog/upcoming)
|
5
5
|
[](https://codeclimate.com/github/sldblog/upcoming)
|
6
6
|
[](https://gemnasium.com/sldblog/upcoming)
|
@@ -14,27 +14,48 @@ Recurring date sequence generator.
|
|
14
14
|
```ruby
|
15
15
|
# running on 20th of June, 2014
|
16
16
|
> factory = Upcoming::Factory.every(:last_day_of_month)
|
17
|
-
=> #<Upcoming::Factory:
|
17
|
+
=> #<Upcoming::Factory:0xb8ba6838 @options={:from=>#<Date: 2014-06-20 ((2456829j,0s,0n),+0s,2299161j)>},
|
18
|
+
@chain=[#<Upcoming::LastDayOfMonthGenerator:0xb8ba6310 @choose=:first>]>
|
18
19
|
|
19
20
|
> factory.first
|
20
21
|
=> #<Date: 2014-06-30 ((2456839j,0s,0n),+0s,2299161j)>
|
21
22
|
|
22
23
|
> factory.take(12).map(&:iso8601)
|
23
|
-
=> ["2014-06-30", "2014-07-31", "2014-08-31", "2014-09-30", "2014-10-31", "2014-11-30",
|
24
|
+
=> ["2014-06-30", "2014-07-31", "2014-08-31", "2014-09-30", "2014-10-31", "2014-11-30",
|
25
|
+
"2014-12-31", "2015-01-31", "2015-02-28", "2015-03-31", "2015-04-30", "2015-05-31"]
|
24
26
|
```
|
25
27
|
|
26
|
-
It is possible to chain methods together.
|
28
|
+
It is possible to chain methods together. Any number of chains can be added.
|
29
|
+
|
30
|
+
Chaining forward in time is done via `then_find_upcoming`:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
> Upcoming::Factory.every(:last_day_of_month).then_find_upcoming(:working_day).take(12).map(&:iso8601)
|
34
|
+
=> ["2014-06-30", "2014-07-31", "2014-09-01", "2014-09-30", "2014-10-31", "2014-12-01",
|
35
|
+
"2014-12-31", "2015-02-02", "2015-03-02", "2015-03-31", "2015-04-30", "2015-06-01"]
|
36
|
+
```
|
37
|
+
|
38
|
+
Stepping backwards in time is done via `then_find_preceding`:
|
27
39
|
|
28
40
|
```ruby
|
29
|
-
>
|
30
|
-
=>
|
41
|
+
> Upcoming::Factory.every(:last_day_of_month, from: '2014-08-20').then_find_preceding(:working_day).first
|
42
|
+
=> #<Date: 2014-08-29 ((2456899j,0s,0n),+0s,2299161j)>
|
31
43
|
```
|
32
44
|
|
33
|
-
Chaining
|
45
|
+
Chaining the same method has no effect:
|
34
46
|
|
35
47
|
```ruby
|
36
|
-
|
37
|
-
|
48
|
+
Upcoming::Factory.every(:last_day_of_month, from: '2014-06-20')
|
49
|
+
.then_find_upcoming(:working_day)
|
50
|
+
.take(3).map(&:iso8601)
|
51
|
+
=> ["2014-06-30", "2014-07-31", "2014-09-01"]
|
52
|
+
|
53
|
+
Upcoming::Factory.every(:last_day_of_month, from: '2014-06-20')
|
54
|
+
.then_find_upcoming(:working_day)
|
55
|
+
.then_find_upcoming(:working_day)
|
56
|
+
.then_find_upcoming(:working_day)
|
57
|
+
.take(3).map(&:iso8601)
|
58
|
+
=> ["2014-06-30", "2014-07-31", "2014-09-01"]
|
38
59
|
```
|
39
60
|
|
40
61
|
## Generators
|
data/lib/upcoming/factory.rb
CHANGED
@@ -11,16 +11,16 @@ module Upcoming
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.every(method, options = {})
|
14
|
-
new(options).
|
14
|
+
new(options).then_find_upcoming(method)
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
18
|
-
@chain << create_generator(method, :
|
17
|
+
def then_find_upcoming(method)
|
18
|
+
@chain << create_generator(method, :upcoming)
|
19
19
|
self
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
@chain << create_generator(method, :
|
22
|
+
def then_find_preceding(method)
|
23
|
+
@chain << create_generator(method, :preceding)
|
24
24
|
self
|
25
25
|
end
|
26
26
|
|
@@ -51,6 +51,5 @@ module Upcoming
|
|
51
51
|
generator_class = Upcoming.const_get class_name
|
52
52
|
generator_class.new(choose: direction)
|
53
53
|
end
|
54
|
-
|
55
54
|
end
|
56
55
|
end
|
@@ -1,12 +1,9 @@
|
|
1
|
-
require 'active_support/core_ext/string'
|
2
|
-
|
3
1
|
module Upcoming
|
4
2
|
class Generator
|
5
|
-
|
6
3
|
attr_reader :choose
|
7
4
|
|
8
5
|
def initialize(options = {})
|
9
|
-
@choose = options.fetch(:choose, :
|
6
|
+
@choose = options.fetch(:choose, :upcoming)
|
10
7
|
end
|
11
8
|
|
12
9
|
def step(from)
|
@@ -16,9 +13,8 @@ module Upcoming
|
|
16
13
|
private
|
17
14
|
|
18
15
|
def date_range(date)
|
19
|
-
return date.downto(date.prev_year) if choose == :
|
16
|
+
return date.downto(date.prev_year) if choose == :preceding
|
20
17
|
date.upto(date.next_year)
|
21
18
|
end
|
22
|
-
|
23
19
|
end
|
24
20
|
end
|
data/lib/upcoming/version.rb
CHANGED
data/spec/factory_spec.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Upcoming::Factory do
|
4
|
-
|
5
4
|
class Upcoming::FizzGenerator < Upcoming::Generator
|
6
5
|
def valid?(date)
|
7
6
|
date.day % 3 == 0
|
@@ -67,23 +66,22 @@ describe Upcoming::Factory do
|
|
67
66
|
end
|
68
67
|
|
69
68
|
context 'chains do not alter main sequence' do
|
70
|
-
Given(:subject) { Upcoming::Factory.every(:buzz).
|
69
|
+
Given(:subject) { Upcoming::Factory.every(:buzz).then_find_preceding(:month_ago_if_twenty_fifth) }
|
71
70
|
Then { result == %w(2014-06-20 2014-05-25 2014-06-30) }
|
72
71
|
end
|
73
72
|
|
74
|
-
context '#
|
73
|
+
context '#then_find_upcoming' do
|
75
74
|
context 'modifies date found by moving to the next date that is a match' do
|
76
|
-
Given(:subject) { Upcoming::Factory.every(:buzz).
|
75
|
+
Given(:subject) { Upcoming::Factory.every(:buzz).then_find_upcoming(:fizz) }
|
77
76
|
Then { result == %w(2014-06-21 2014-06-27 2014-06-30) }
|
78
77
|
end
|
79
78
|
end
|
80
79
|
|
81
|
-
context '#
|
80
|
+
context '#then_find_preceding' do
|
82
81
|
context 'modifies date found by moving to the previous date that is a match' do
|
83
|
-
Given(:subject) { Upcoming::Factory.every(:buzz).
|
82
|
+
Given(:subject) { Upcoming::Factory.every(:buzz).then_find_preceding(:fizz) }
|
84
83
|
Then { result == %w(2014-06-18 2014-06-24 2014-06-30) }
|
85
84
|
end
|
86
85
|
end
|
87
86
|
end
|
88
|
-
|
89
87
|
end
|
@@ -1,14 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Upcoming::Generator do
|
4
|
-
|
5
4
|
class Upcoming::FakeGenerator < Upcoming::Generator
|
6
5
|
def initialize(options = {})
|
7
6
|
super
|
8
7
|
@valid_dates = {}
|
9
8
|
end
|
10
9
|
|
11
|
-
def
|
10
|
+
def mark_as_valid(date)
|
12
11
|
@valid_dates[Date.parse(date)] = true
|
13
12
|
end
|
14
13
|
|
@@ -22,18 +21,22 @@ describe Upcoming::Generator do
|
|
22
21
|
result and result.iso8601
|
23
22
|
end
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
24
|
+
def self.returns_the_starting_date_if_it_is_valid
|
25
|
+
context 'returns the starting date if it is valid' do
|
26
|
+
Given(:start_date) { '2014-05-19' }
|
27
|
+
When { subject.mark_as_valid(start_date) }
|
28
|
+
Then { step(start_date) == start_date }
|
29
|
+
end
|
31
30
|
end
|
32
31
|
|
33
32
|
context 'forward in time' do
|
33
|
+
Given(:subject) { Upcoming::FakeGenerator.new(choose: :upcoming) }
|
34
|
+
|
35
|
+
returns_the_starting_date_if_it_is_valid
|
36
|
+
|
34
37
|
context 'returns the first valid date, checking all dates in sequence' do
|
35
|
-
When { subject.
|
36
|
-
When { subject.
|
38
|
+
When { subject.mark_as_valid('2014-05-20') }
|
39
|
+
When { subject.mark_as_valid('2014-06-20') }
|
37
40
|
Then { step('2014-05-19') == '2014-05-20' }
|
38
41
|
Then { step('2014-05-21') == '2014-06-20' }
|
39
42
|
Then { step('2014-06-10') == '2014-06-20' }
|
@@ -41,18 +44,20 @@ describe Upcoming::Generator do
|
|
41
44
|
|
42
45
|
context 'returns nil if there is no valid date within 1 year' do
|
43
46
|
# implement a custom +step+ method in your generator if you need this
|
44
|
-
When { subject.
|
47
|
+
When { subject.mark_as_valid('2020-01-01') }
|
45
48
|
Then { step('2018-12-31') == nil }
|
46
49
|
Then { step('2019-01-01') == '2020-01-01' }
|
47
50
|
end
|
48
51
|
end
|
49
52
|
|
50
53
|
context 'backward in time' do
|
51
|
-
Given(:subject) { Upcoming::FakeGenerator.new(choose: :
|
54
|
+
Given(:subject) { Upcoming::FakeGenerator.new(choose: :preceding) }
|
55
|
+
|
56
|
+
returns_the_starting_date_if_it_is_valid
|
52
57
|
|
53
58
|
context 'returns the closest past date, checking all dates in sequence' do
|
54
|
-
When { subject.
|
55
|
-
When { subject.
|
59
|
+
When { subject.mark_as_valid('2010-03-04') }
|
60
|
+
When { subject.mark_as_valid('2010-09-21') }
|
56
61
|
Then { step('2010-09-22') == '2010-09-21' }
|
57
62
|
Then { step('2010-09-20') == '2010-03-04' }
|
58
63
|
Then { step('2010-06-01') == '2010-03-04' }
|
@@ -60,10 +65,9 @@ describe Upcoming::Generator do
|
|
60
65
|
|
61
66
|
context 'returns nil if there is no valid date within -1 year' do
|
62
67
|
# implement a custom +step+ method in your generator if you need this
|
63
|
-
When { subject.
|
68
|
+
When { subject.mark_as_valid('2010-01-01') }
|
64
69
|
Then { step('2011-01-02') == nil }
|
65
70
|
Then { step('2011-01-01') == '2010-01-01' }
|
66
71
|
end
|
67
72
|
end
|
68
|
-
|
69
73
|
end
|
@@ -1,23 +1,26 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Upcoming::LastDayOfMonthGenerator do
|
4
|
-
|
5
4
|
Given(:subject) { Upcoming::LastDayOfMonthGenerator.new }
|
6
5
|
When(:valid) { subject.valid?(date) }
|
7
6
|
|
8
|
-
context '
|
7
|
+
context 'start of the month is invalid' do
|
8
|
+
Given(:date) { Date.parse('2014-06-01') }
|
9
|
+
Then { !valid }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'middle of the month is invalid' do
|
9
13
|
Given(:date) { Date.parse('2014-06-15') }
|
10
14
|
Then { !valid }
|
11
15
|
end
|
12
16
|
|
13
|
-
context '
|
17
|
+
context 'last day of the month is valid' do
|
14
18
|
Given(:date) { Date.parse('2014-05-31') }
|
15
19
|
Then { valid }
|
16
20
|
end
|
17
21
|
|
18
|
-
context '
|
22
|
+
context 'last day of February in a leap year is valid' do
|
19
23
|
Given(:date) { Date.parse('2012-02-29') }
|
20
24
|
Then { valid }
|
21
25
|
end
|
22
|
-
|
23
26
|
end
|
@@ -1,10 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Upcoming::WorkingDayGenerator do
|
4
|
-
|
5
4
|
Given(:subject) { Upcoming::WorkingDayGenerator.new }
|
6
5
|
|
7
|
-
context '
|
6
|
+
context 'weekends are invalid' do
|
8
7
|
Given(:saturday) { Date.parse('2014-06-14') }
|
9
8
|
Given(:sunday) { Date.parse('2014-06-15') }
|
10
9
|
|
@@ -12,7 +11,7 @@ describe Upcoming::WorkingDayGenerator do
|
|
12
11
|
Then { !subject.valid?(sunday) }
|
13
12
|
end
|
14
13
|
|
15
|
-
context '
|
14
|
+
context 'weekdays are valid' do
|
16
15
|
Given(:monday) { Date.parse('2014-06-16') }
|
17
16
|
Given(:tuesday) { monday + 1 }
|
18
17
|
Given(:wednesday) { tuesday + 1 }
|
@@ -25,5 +24,4 @@ describe Upcoming::WorkingDayGenerator do
|
|
25
24
|
Then { subject.valid?(thursday) }
|
26
25
|
Then { subject.valid?(friday) }
|
27
26
|
end
|
28
|
-
|
29
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: upcoming
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Lantos
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '5.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -87,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
87
|
requirements:
|
88
88
|
- - ">="
|
89
89
|
- !ruby/object:Gem::Version
|
90
|
-
version:
|
90
|
+
version: 2.2.2
|
91
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
92
|
requirements:
|
93
93
|
- - ">="
|
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
95
|
version: '0'
|
96
96
|
requirements: []
|
97
97
|
rubyforge_project:
|
98
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.5.1
|
99
99
|
signing_key:
|
100
100
|
specification_version: 4
|
101
101
|
summary: Recurring date generator
|