date_time_step_with 0.1.3
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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +49 -0
- data/Rakefile +5 -0
- data/date_time_step_with.gemspec +22 -0
- data/lib/date_time_step_with.rb +8 -0
- data/lib/date_time_step_with/cron_matcher.rb +87 -0
- data/lib/date_time_step_with/cron_stepper.rb +29 -0
- data/lib/date_time_step_with/version.rb +3 -0
- data/test/cron_matcher_test.rb +208 -0
- data/test/date_cron_stepper_test.rb +61 -0
- data/test/date_time_cron_stepper_test.rb +108 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ee18ac49e04c2990673cc03da60a42c1fb426f32
|
4
|
+
data.tar.gz: 0bd0d3b38054e352f1f6f02c518111d144e7a78a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 96334b850e2ecebe775da1c45de0c02c41334c78e8a3d81c258250230b340fe22957232cc0b1218acd6e2add218eb1a66df455059de76367de6d7c0644d9d01c
|
7
|
+
data.tar.gz: 06eff7086853ea13f15fafb2f3f294889ff808153c84a00016fd8eef70db35f7b5fd5e32a5a510c3e21aabd0de50b167297008b4553f3c274297a3d7d9939a64
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 jmrepetti
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# DateTimeStepWith
|
2
|
+
|
3
|
+
Filter Ruby Date or DateTime collections using cron pattern.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install date_time_step_with
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
|
12
|
+
####Date class
|
13
|
+
|
14
|
+
require 'date'
|
15
|
+
require 'date_time_step_with'
|
16
|
+
|
17
|
+
Date.include DateTimeStepWith::CronMatcher
|
18
|
+
Date.include DateTimeStepWith::CronStepper
|
19
|
+
|
20
|
+
date_from = Date.new(2015,1,1)
|
21
|
+
date_to = Date.new(2015,12,31)
|
22
|
+
date_from.step_with_cron("* * 12,15 7,8,12 * 2015", date_to).collect {|date| date.strftime("%Y-%m-%d")}
|
23
|
+
=> ["2015-07-12", "2015-07-15", "2015-08-12", "2015-08-15", "2015-12-12", "2015-12-15"]
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
####DateTime class
|
29
|
+
|
30
|
+
require 'time'
|
31
|
+
require 'date_time_step_with'
|
32
|
+
|
33
|
+
DateTime.include DateTimeStepWith::CronMatcher
|
34
|
+
DateTime.include DateTimeStepWith::CronStepper
|
35
|
+
|
36
|
+
one_minute_step = (1.to_f/24/60)
|
37
|
+
|
38
|
+
date_from = DateTime.new(2015,1,1,0,0)
|
39
|
+
date_to = DateTime.new(2015,12,31,0,0)
|
40
|
+
date_from.step_with_cron("10-15 5,6,7 15 11 * 2015", date_to, one_minute_step).collect{|date| date.strftime("%Y-%m-%d %H:%M")}
|
41
|
+
=> ["2015-11-15 05:10", "2015-11-15 05:11", "2015-11-15 05:12", "2015-11-15 05:13", "2015-11-15 05:14", "2015-11-15 05:15",
|
42
|
+
"2015-11-15 06:10", "2015-11-15 06:11", "2015-11-15 06:12", "2015-11-15 06:13", "2015-11-15 06:14", "2015-11-15 06:15",
|
43
|
+
"2015-11-15 07:10", "2015-11-15 07:11", "2015-11-15 07:12", "2015-11-15 07:13", "2015-11-15 07:14", "2015-11-15 07:15"]
|
44
|
+
|
45
|
+
|
46
|
+
## TODO
|
47
|
+
|
48
|
+
* Improve performace. Iteration on wide ranges steping in minutes take long.
|
49
|
+
* Add ICAL RRULE expression stepper
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'date_time_step_with/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "date_time_step_with"
|
8
|
+
spec.version = DateTimeStepWith::VERSION
|
9
|
+
spec.authors = ["jmrepetti"]
|
10
|
+
spec.email = ["jmrepetti@gmail.com"]
|
11
|
+
spec.summary = %q{Filter dates collection using cron pattern.}
|
12
|
+
spec.homepage = "https://github.com/jmrepetti/date_time_step_with"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module DateTimeStepWith
|
2
|
+
|
3
|
+
# Copied from http://en.wikipedia.org/wiki/Cron
|
4
|
+
# * * * * *
|
5
|
+
# │ │ │ │ │
|
6
|
+
# │ │ │ │ │
|
7
|
+
# │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
|
8
|
+
# │ │ │ └────────── month (1 - 12)
|
9
|
+
# │ │ └─────────────── day of month (1 - 31)
|
10
|
+
# │ └──────────────────── hour (0 - 23)
|
11
|
+
# └───────────────────────── min (0 - 59)
|
12
|
+
class CronRangeMatcher
|
13
|
+
def initialize(cron_expression)
|
14
|
+
from, to = cron_expression.split("-")
|
15
|
+
@range = (from..to).to_a
|
16
|
+
end
|
17
|
+
|
18
|
+
def =~(value)
|
19
|
+
@range.include? value
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
class CronListMatcher
|
25
|
+
def initialize(cron_expression)
|
26
|
+
@list = cron_expression.split(",")
|
27
|
+
end
|
28
|
+
|
29
|
+
def =~(value)
|
30
|
+
@list.include? value
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module CronMatcherDateMethods
|
35
|
+
def self_cron_array
|
36
|
+
%W(0 0 #{self.day} #{self.month} #{self.wday} #{self.year})
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
module CronMatcherTimeMethods
|
41
|
+
def self_cron_array
|
42
|
+
%W(#{self.min} #{self.hour} #{self.day} #{self.month} #{self.wday} #{self.year})
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
module CronMatcher
|
48
|
+
@@cron_re_exps = {}
|
49
|
+
|
50
|
+
def self.included(klass)
|
51
|
+
if (klass == Date)
|
52
|
+
klass.include CronMatcherDateMethods
|
53
|
+
else
|
54
|
+
klass.include CronMatcherTimeMethods
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def match_cron?(cron_expression)
|
59
|
+
|
60
|
+
@@cron_re_exps[cron_expression] ||= cron_expression.split(/\s/).collect {|cr_exp|
|
61
|
+
|
62
|
+
if (cr_exp == "*")
|
63
|
+
/.*/
|
64
|
+
elsif cr_exp["-"]
|
65
|
+
CronRangeMatcher.new(cr_exp)
|
66
|
+
elsif cr_exp[","]
|
67
|
+
CronListMatcher.new(cr_exp)
|
68
|
+
else
|
69
|
+
Regexp.new("^#{cr_exp.to_i}$")
|
70
|
+
end
|
71
|
+
}
|
72
|
+
|
73
|
+
self_cron_array.zip(@@cron_re_exps[cron_expression]).all? do |value, cr_exp|
|
74
|
+
cr_exp =~ value
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module DateTimeStepWith
|
2
|
+
module CronStepper
|
3
|
+
def step_with_cron(cron_expression, limit,step=nil, &block)
|
4
|
+
|
5
|
+
#if no step given, then iterate on the minimum cron value
|
6
|
+
if step.nil? && self.respond_to?(:minute)
|
7
|
+
step = (1.to_f/24/60) #one minute
|
8
|
+
else
|
9
|
+
step = step || 1# or one day
|
10
|
+
end
|
11
|
+
|
12
|
+
#Caching
|
13
|
+
if (@step_with_cron_cache == "#{cron_expression} #{limit} #{step}")
|
14
|
+
@step_with_cron_collection ||= self.step(limit,step).select{|d| d.match_cron?(cron_expression) }
|
15
|
+
else
|
16
|
+
@step_with_cron_cache = "#{cron_expression} #{limit} #{step}"
|
17
|
+
@step_with_cron_collection = self.step(limit,step).select{|d| d.match_cron?(cron_expression) }
|
18
|
+
end
|
19
|
+
|
20
|
+
if block_given?
|
21
|
+
@step_with_cron_collection.each do |d|
|
22
|
+
yield d
|
23
|
+
end
|
24
|
+
else
|
25
|
+
@step_with_cron_collection
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,208 @@
|
|
1
|
+
require 'time'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'date_time_step_with'
|
4
|
+
|
5
|
+
Time.include DateTimeStepWith::CronMatcher
|
6
|
+
Date.include DateTimeStepWith::CronMatcher
|
7
|
+
DateTime.include DateTimeStepWith::CronMatcher
|
8
|
+
|
9
|
+
class CronMatcherTest < Minitest::Test
|
10
|
+
|
11
|
+
def test_match_dates
|
12
|
+
date = Date.new(2015,4,1)
|
13
|
+
cr_exp = "* * 1 * * *"
|
14
|
+
assert (date.match_cron? cr_exp), "#{date} should match '#{cr_exp}'"
|
15
|
+
date = Date.new(2015,4,11)
|
16
|
+
assert !(date.match_cron? cr_exp), "#{date} should not match '#{cr_exp}'"
|
17
|
+
date = Date.new(2015,4,1)
|
18
|
+
cr_exp = "* * 01 * * *"
|
19
|
+
assert (date.match_cron? cr_exp), "#{date} should match '#{cr_exp}'"
|
20
|
+
date = Date.new(2015,3,15)
|
21
|
+
cr_exp = "00 00 * * * *"
|
22
|
+
assert (date.match_cron? cr_exp), "#{date} should match '#{cr_exp}'"
|
23
|
+
date = DateTime.new(2015,3,15)
|
24
|
+
cr_exp = "00 00 * * * *"
|
25
|
+
assert (date.match_cron? cr_exp), "#{date} should match '#{cr_exp}'"
|
26
|
+
date = DateTime.new(2015,3,15)
|
27
|
+
cr_exp = "00 00 * 4 * 2015"
|
28
|
+
assert !(date.match_cron? cr_exp), "#{date} should not match '#{cr_exp}'"
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_minutes_range
|
32
|
+
date_time = DateTime.new(2015,1,1,12,20)
|
33
|
+
date_time_limit = DateTime.new(2015,1,1,12,35)
|
34
|
+
cr_exp = "20-35 12 * * * 2015"
|
35
|
+
min_step = 1.0/(24*60)
|
36
|
+
date_time.step(date_time_limit, min_step).each do |d|
|
37
|
+
assert (d.match_cron? cr_exp), "#{d} should match '#{cr_exp}'"
|
38
|
+
end
|
39
|
+
date_time = DateTime.new(2015,1,1,12,19)
|
40
|
+
assert !(date_time.match_cron? cr_exp), "#{date_time} should not match '#{cr_exp}'"
|
41
|
+
date_time = DateTime.new(2015,1,1,12,36)
|
42
|
+
assert !(date_time.match_cron? cr_exp), "#{date_time} should not match '#{cr_exp}'"
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_hours_range
|
46
|
+
date_time = DateTime.new(2015,1,1,12,20)
|
47
|
+
date_time_limit = DateTime.new(2015,1,1,18,35)
|
48
|
+
cr_exp = "* 12-18 * * * 2015"
|
49
|
+
hour_step = 1.0/(24)
|
50
|
+
date_time.step(date_time_limit, hour_step).each do |d|
|
51
|
+
assert (d.match_cron? cr_exp), "#{d} should match '#{cr_exp}'"
|
52
|
+
end
|
53
|
+
date_time = DateTime.new(2015,1,1,11,19)
|
54
|
+
assert !(date_time.match_cron? cr_exp), "#{date_time} should not match '#{cr_exp}'"
|
55
|
+
date_time = DateTime.new(2015,1,1,19,36)
|
56
|
+
assert !(date_time.match_cron? cr_exp), "#{date_time} should not match '#{cr_exp}'"
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_minutes_list
|
60
|
+
date_time = DateTime.new(2015,1,1,12,21)
|
61
|
+
date_time_limit = DateTime.new(2015,1,1,12,24)
|
62
|
+
cr_exp = "21,22,23,24 12 * * * 2015"
|
63
|
+
min_step = 1.0/(24*60)
|
64
|
+
date_time.step(date_time_limit, min_step).each do |d|
|
65
|
+
assert (d.match_cron? cr_exp), "#{d} should match '#{cr_exp}'"
|
66
|
+
end
|
67
|
+
date_time = DateTime.new(2015,1,1,12,20)
|
68
|
+
assert !(date_time.match_cron? cr_exp), "#{date_time} should not match '#{cr_exp}'"
|
69
|
+
date_time = DateTime.new(2015,1,1,12,25)
|
70
|
+
assert !(date_time.match_cron? cr_exp), "#{date_time} should not match '#{cr_exp}'"
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_hours_list
|
74
|
+
date_time = DateTime.new(2015,1,1,12,20)
|
75
|
+
date_time_limit = DateTime.new(2015,1,1,15,35)
|
76
|
+
cr_exp = "* 12,13,14,15 * * * 2015"
|
77
|
+
hour_step = 1.0/(24)
|
78
|
+
date_time.step(date_time_limit, hour_step).each do |d|
|
79
|
+
assert (d.match_cron? cr_exp), "#{d} should match '#{cr_exp}'"
|
80
|
+
end
|
81
|
+
date_time = DateTime.new(2015,1,1,11,19)
|
82
|
+
assert !(date_time.match_cron? cr_exp), "#{date_time} should not match '#{cr_exp}'"
|
83
|
+
date_time = DateTime.new(2015,1,1,16,36)
|
84
|
+
assert !(date_time.match_cron? cr_exp), "#{date_time} should not match '#{cr_exp}'"
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_every_minute
|
88
|
+
(0..59).to_a.each do |minute|
|
89
|
+
date_time = DateTime.new(2015,1,1,1,minute)
|
90
|
+
minutes = "%02d" % minute
|
91
|
+
cr_exp = "#{minutes} 01 1 1 * 2015"
|
92
|
+
assert (date_time.match_cron? cr_exp), "#{date_time} should match '#{cr_exp}'"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_every_hour
|
97
|
+
(0..23).to_a.each do |h|
|
98
|
+
date_time = DateTime.new(2015,1,1,h,0)
|
99
|
+
hs = "%02d" % h
|
100
|
+
cr_exp = "* #{hs} 1 1 * 2015"
|
101
|
+
assert (date_time.match_cron? cr_exp), "#{date_time} should match '#{cr_exp}'"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_every_day_time
|
106
|
+
(1..31).to_a.each do |d|
|
107
|
+
date_time = DateTime.new(2015,1,d,0,0)
|
108
|
+
ds = "%02d" % d
|
109
|
+
cr_exp = "* * #{ds} 1 * 2015"
|
110
|
+
assert (date_time.match_cron? cr_exp), "#{date_time} should match '#{cr_exp}'"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_every_day_date
|
115
|
+
(1..31).to_a.each do |d|
|
116
|
+
date = Date.new(2015,1,d)
|
117
|
+
ds = "%02d" % d
|
118
|
+
cr_exp = "* * #{ds} 1 * 2015"
|
119
|
+
assert (date.match_cron? cr_exp), "#{date} should match '#{cr_exp}'"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
def test_every_month_time
|
125
|
+
(1..12).to_a.each do |m|
|
126
|
+
date_time = DateTime.new(2015,m,1,0,0)
|
127
|
+
ms = "%02d" % m
|
128
|
+
cr_exp = "* * * #{ms} * 2015"
|
129
|
+
assert (date_time.match_cron? cr_exp), "#{date_time} should match '#{cr_exp}'"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_every_month_date
|
134
|
+
(1..12).to_a.each do |m|
|
135
|
+
date = Date.new(2015,m,1)
|
136
|
+
ms = "%02d" % m
|
137
|
+
cr_exp = "* * * #{ms} * 2015"
|
138
|
+
assert (date.match_cron? cr_exp), "#{date} should match '#{cr_exp}'"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_ten_years_time
|
143
|
+
(2015..2025).to_a.each do |y|
|
144
|
+
date_time = DateTime.new(y,1,1,0,0)
|
145
|
+
ys = "%02d" % y
|
146
|
+
cr_exp = "* * * * * #{ys}"
|
147
|
+
assert (date_time.match_cron? cr_exp), "#{date_time} should match '#{cr_exp}'"
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_ten_years_date
|
152
|
+
(2015..2025).to_a.each do |y|
|
153
|
+
date = Date.new(y,1,1)
|
154
|
+
ys = "%02d" % y
|
155
|
+
cr_exp = "* * * * * #{ys}"
|
156
|
+
assert (date.match_cron? cr_exp), "#{date} should match '#{cr_exp}'"
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_match_specific_date_time
|
161
|
+
cr_exp = "* * 1 4 * 2015"
|
162
|
+
date_time = DateTime.new(2015,4,1)
|
163
|
+
assert (date_time.match_cron? cr_exp), "#{date_time} should not match '#{cr_exp}'"
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
def test_not_match_specific_date_time
|
168
|
+
cr_exp = "* * 1 5 * 2015"
|
169
|
+
date_time = DateTime.new(2015,4,1)
|
170
|
+
assert !(date_time.match_cron? cr_exp), "#{date_time} should not match '#{cr_exp}'"
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
def test_match_every_day
|
175
|
+
cr_exp = "* * * * * 2015"
|
176
|
+
|
177
|
+
date = Date.new(2015,4,1)
|
178
|
+
|
179
|
+
date.step(Date.new(2015,4,30)).each do |d|
|
180
|
+
assert (d.match_cron? cr_exp), "#{d} should match '#{cr_exp}'"
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_not_match_not_every_day
|
185
|
+
cr_exp = "* * * * * 2016"
|
186
|
+
date = Date.new(2015,4,1)
|
187
|
+
|
188
|
+
date.step(Date.new(2015,4,30)).each do |d|
|
189
|
+
assert !(d.match_cron? cr_exp), "#{d} should not match '#{cr_exp}'"
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
["sunday","monday","tuesday","wednesday","thursday","friday","saturday"].each_with_index do |day,idx|
|
195
|
+
define_method "test_match_every_#{day}" do
|
196
|
+
cr_exp = "* * * * #{idx} 2015"
|
197
|
+
date = Date.new(2015,1,1)
|
198
|
+
days = date.step(Date.new(2015,12,31)).select { |d| d.send("#{day}?") }
|
199
|
+
days.each do |d|
|
200
|
+
assert (d.match_cron? cr_exp), "#{day}: #{d} should match #{cr_exp}"
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
|
206
|
+
end
|
207
|
+
|
208
|
+
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'time'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'date_time_step_with'
|
4
|
+
|
5
|
+
Time.include DateTimeStepWith::CronMatcher
|
6
|
+
Time.include DateTimeStepWith::CronStepper
|
7
|
+
Date.include DateTimeStepWith::CronMatcher
|
8
|
+
Date.include DateTimeStepWith::CronStepper
|
9
|
+
DateTime.include DateTimeStepWith::CronMatcher
|
10
|
+
DateTime.include DateTimeStepWith::CronStepper
|
11
|
+
|
12
|
+
|
13
|
+
class CronStepperTest < Minitest::Test
|
14
|
+
|
15
|
+
def test_mondays_collection
|
16
|
+
date = Date.new(2015,4,1)
|
17
|
+
date_limit = Date.new(2015,4,30)
|
18
|
+
cron_mondays = date.step_with_cron("* * * * 1 *", date_limit) do |d|
|
19
|
+
assert d.monday?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_days_collection
|
24
|
+
date = Date.new(2015,4,1)
|
25
|
+
date_limit = Date.new(2015,4,30)
|
26
|
+
days = date.step(date_limit).collect{|e| e}
|
27
|
+
cron_days = date.step_with_cron("00 00 * * * *", date_limit).collect{|e| e}
|
28
|
+
assert_equal days, cron_days
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_range_collection
|
32
|
+
date = Date.new(2015,4,1)
|
33
|
+
date_limit = Date.new(2015,6,30)
|
34
|
+
|
35
|
+
days = []
|
36
|
+
days << Date.new(2015,4,12).step(Date.new(2015,4,20)).collect {|e| e}
|
37
|
+
days << Date.new(2015,5,12).step(Date.new(2015,5,20)).collect {|e| e}
|
38
|
+
days << Date.new(2015,6,12).step(Date.new(2015,6,20)).collect {|e| e}
|
39
|
+
|
40
|
+
cron_days = date.step_with_cron("00 00 12-20 * * 2015", date_limit).collect{|e| e}
|
41
|
+
assert_equal days.flatten, cron_days
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_list_collection
|
45
|
+
date = Date.new(2015,7,1)
|
46
|
+
date_limit = Date.new(2015,12,30)
|
47
|
+
|
48
|
+
days = [
|
49
|
+
Date.new(2015,7,12),
|
50
|
+
Date.new(2015,7,15),
|
51
|
+
Date.new(2015,8,12),
|
52
|
+
Date.new(2015,8,15),
|
53
|
+
Date.new(2015,12,12),
|
54
|
+
Date.new(2015,12,15),
|
55
|
+
]
|
56
|
+
|
57
|
+
cron_days = date.step_with_cron("00 00 12,15 7,8,12 * 2015", date_limit).collect{|e| e}
|
58
|
+
assert_equal days.flatten, cron_days
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'time'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'date_time_step_with'
|
4
|
+
|
5
|
+
DateTime.include DateTimeStepWith::CronMatcher
|
6
|
+
DateTime.include DateTimeStepWith::CronStepper
|
7
|
+
|
8
|
+
|
9
|
+
class DateTimeCronStepperTest < Minitest::Test
|
10
|
+
|
11
|
+
|
12
|
+
def test_minutes_collection
|
13
|
+
date_time = DateTime.new(2015,4,1,0,0)
|
14
|
+
date_time_limit = DateTime.new(2015,4,1,0,59)
|
15
|
+
one_minute_step = (1.to_f/24/60)
|
16
|
+
minutes = date_time.step(date_time_limit,one_minute_step).collect{|e| e}
|
17
|
+
cron_minutes = date_time.step_with_cron("* * * * * *", date_time_limit).collect{|e| e}
|
18
|
+
assert_equal minutes, cron_minutes
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_minutes_list_collection
|
22
|
+
date_time = DateTime.new(2015,4,1,0,0)
|
23
|
+
date_time_limit = DateTime.new(2015,4,1,0,59)
|
24
|
+
#sample list
|
25
|
+
minutes = [3,7,9,10,25,30].collect {|e| DateTime.new(2015,4,1,0,e) }
|
26
|
+
cron_minutes = date_time.step_with_cron("3,7,9,10,25,30 * * * * 2015", date_time_limit).collect{|e| e}
|
27
|
+
assert_equal minutes, cron_minutes
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_hours_list_collection
|
31
|
+
date_time = DateTime.new(2015,4,1,0,0)
|
32
|
+
date_time_limit = DateTime.new(2015,4,1,23,59)
|
33
|
+
minutes = [3,7,9,10,16,23].collect {|e| DateTime.new(2015,4,1,e,0) }
|
34
|
+
cron_minutes = date_time.step_with_cron("00 3,7,9,10,16,23 * * * 2015", date_time_limit).collect{|e| e}
|
35
|
+
assert_equal minutes, cron_minutes
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_minutes_range_collection
|
39
|
+
date_time = DateTime.new(2015,4,1,0,0)
|
40
|
+
date_time_limit = DateTime.new(2015,4,1,0,59)
|
41
|
+
|
42
|
+
#sample list
|
43
|
+
minutes = (3..30).collect {|e| DateTime.new(2015,4,1,0,e) }
|
44
|
+
|
45
|
+
cron_minutes = date_time.step_with_cron("3-30 * * * * 2015", date_time_limit).collect{|e| e}
|
46
|
+
assert_equal minutes, cron_minutes
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_hours_range_collection
|
50
|
+
date_time = DateTime.new(2015,4,1,0,0)
|
51
|
+
date_time_limit = DateTime.new(2015,4,1,23,59)
|
52
|
+
|
53
|
+
minutes = (3..16).collect {|e| DateTime.new(2015,4,1,e,0) }
|
54
|
+
cron_minutes = date_time.step_with_cron("00 3-16 * * * 2015", date_time_limit).collect{|e| e}
|
55
|
+
assert_equal minutes, cron_minutes
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
def test_cached_collection
|
60
|
+
date_time = DateTime.new(2015,4,1,0,0)
|
61
|
+
date_time_limit = DateTime.new(2015,4,1,0,59)
|
62
|
+
|
63
|
+
cron_minutes = date_time.step_with_cron("* * * * * *", date_time_limit)
|
64
|
+
cron_minutes_again = date_time.step_with_cron("* * * * * *", date_time_limit)
|
65
|
+
|
66
|
+
assert_equal cron_minutes.object_id, cron_minutes_again.object_id
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_no_cached_collection
|
70
|
+
date_time = DateTime.new(2015,4,1,0,0)
|
71
|
+
date_time_limit = DateTime.new(2015,4,1,0,59)
|
72
|
+
|
73
|
+
cron_minutes = date_time.step_with_cron("* * * * * *", date_time_limit)
|
74
|
+
cron_minutes_again = date_time.step_with_cron("* * * * * 2015", date_time_limit)
|
75
|
+
|
76
|
+
refute_equal cron_minutes.object_id, cron_minutes_again.object_id
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
def test_step_minutes
|
81
|
+
date_time = DateTime.new(2015,4,1,0,3)
|
82
|
+
date_time_limit = DateTime.new(2015,4,1,0,16)
|
83
|
+
|
84
|
+
one_minute_step = (1.to_f/24/60)
|
85
|
+
|
86
|
+
minutes = (3..16).collect {|e| DateTime.new(2015,4,1,0,e) }
|
87
|
+
|
88
|
+
cron_minutes = date_time.step_with_cron("* * * * * *", date_time_limit, one_minute_step)
|
89
|
+
|
90
|
+
assert_equal minutes, cron_minutes
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_step_hours
|
94
|
+
|
95
|
+
date_time = DateTime.new(2015,4,1,3,0)
|
96
|
+
date_time_limit = DateTime.new(2015,4,1,16,0)
|
97
|
+
|
98
|
+
one_minute_step = (1.to_f/24)
|
99
|
+
|
100
|
+
hours = (3..16).collect {|e| DateTime.new(2015,4,1,e,0) }
|
101
|
+
|
102
|
+
cron_hours = date_time.step_with_cron("* * * * * *", date_time_limit, one_minute_step)
|
103
|
+
|
104
|
+
assert_equal hours, cron_hours
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_time_step_with
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jmrepetti
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- jmrepetti@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- date_time_step_with.gemspec
|
54
|
+
- lib/date_time_step_with.rb
|
55
|
+
- lib/date_time_step_with/cron_matcher.rb
|
56
|
+
- lib/date_time_step_with/cron_stepper.rb
|
57
|
+
- lib/date_time_step_with/version.rb
|
58
|
+
- test/cron_matcher_test.rb
|
59
|
+
- test/date_cron_stepper_test.rb
|
60
|
+
- test/date_time_cron_stepper_test.rb
|
61
|
+
homepage: https://github.com/jmrepetti/date_time_step_with
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.2.0
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Filter dates collection using cron pattern.
|
85
|
+
test_files:
|
86
|
+
- test/cron_matcher_test.rb
|
87
|
+
- test/date_cron_stepper_test.rb
|
88
|
+
- test/date_time_cron_stepper_test.rb
|
89
|
+
has_rdoc:
|