time_chunk 0.0.1 → 0.1.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.
- data/VERSION +1 -1
- data/lib/time_chunk.rb +21 -11
- data/spec/time_chunk_spec.rb +15 -0
- data/time_chunk.gemspec +2 -2
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/lib/time_chunk.rb
CHANGED
@@ -17,19 +17,29 @@ module TimeChunk
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.iterate(range, step_size)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
if range.first.is_a?(Time)
|
21
|
+
begin_at = range.first
|
22
|
+
end_at = range.last.is_a?(Time) ? range.last : Time.parse(range.last.to_s)
|
23
|
+
elsif range.first.is_a?(Date)
|
24
|
+
begin_at = range.first
|
25
|
+
end_at = range.last.is_a?(Date) ? range.last : Date.parse(range.last.to_s)
|
26
|
+
# step size is always seconds, regardless of whether range is Time or Date instances
|
27
|
+
step_size = step_size/86400
|
28
|
+
else
|
29
|
+
begin_at = Time.parse(range.first.to_s)
|
30
|
+
end_at = Time.parse(range.last.to_s)
|
31
|
+
end
|
32
|
+
|
33
|
+
raise ArgumentError, "Given range's begin (#{begin_at.to_time.utc.iso8601}) is after its end (#{end_at.to_time.utc.iso8601})." if begin_at > end_at
|
34
|
+
|
35
|
+
current_end = begin_at - 1
|
25
36
|
|
26
37
|
begin
|
27
|
-
|
28
|
-
|
29
|
-
|
38
|
+
current_begin = current_end + 1
|
39
|
+
current_end = current_begin + step_size - 1
|
40
|
+
current_end = [current_end, end_at].min
|
30
41
|
|
31
|
-
yield
|
32
|
-
end while
|
42
|
+
yield current_begin..current_end
|
43
|
+
end while current_end < end_at
|
33
44
|
end
|
34
|
-
|
35
45
|
end
|
data/spec/time_chunk_spec.rb
CHANGED
@@ -88,6 +88,21 @@ describe TimeChunk do
|
|
88
88
|
(t('2013-01-01T02:00:00Z')..t('2013-01-01T02:59:59Z')),
|
89
89
|
]
|
90
90
|
end
|
91
|
+
|
92
|
+
it "should handle date arguments" do
|
93
|
+
TimeChunk.iterate(
|
94
|
+
Date.parse('2013-02-27')..Date.parse('2013-03-05'), 86400*2
|
95
|
+
) do |range|
|
96
|
+
@calls << range
|
97
|
+
end
|
98
|
+
|
99
|
+
@calls.map(&:to_s).should eq [
|
100
|
+
'2013-02-27..2013-02-28',
|
101
|
+
'2013-03-01..2013-03-02',
|
102
|
+
'2013-03-03..2013-03-04',
|
103
|
+
'2013-03-05..2013-03-05'
|
104
|
+
]
|
105
|
+
end
|
91
106
|
end
|
92
107
|
|
93
108
|
it "should iterate over each_day" do
|
data/time_chunk.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "time_chunk"
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alex Dean"]
|
12
|
-
s.date = "2013-
|
12
|
+
s.date = "2013-03-08"
|
13
13
|
s.description = "Iterate over time ranges in discrete chunks."
|
14
14
|
s.email = "alex@crackpot.org"
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_chunk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
110
|
version: '0'
|
111
111
|
segments:
|
112
112
|
- 0
|
113
|
-
hash:
|
113
|
+
hash: -3932107950510678518
|
114
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
115
|
none: false
|
116
116
|
requirements:
|