time_frame 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/Guardfile +24 -0
- data/lib/time_frame/empty.rb +2 -2
- data/lib/time_frame/time_frame.rb +43 -9
- data/lib/time_frame/version.rb +1 -1
- data/spec/time_frame_spec.rb +169 -15
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36e147ffd05688d225d98d98b03df2055f8705fa
|
4
|
+
data.tar.gz: 52ce6f9719d59c49101b5eae4c25efa6f6bb90af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8324b87fa0ad4685d2d793922f2a45923f16bad5fd2582534ad0df5ad9fa4bb77669cb79521836506c79e17b9dad0fc74a1af8e822c5e28fcb30a13fbc07042a
|
7
|
+
data.tar.gz: 3ffca269f990f2ecfe62d2beed4ac25bf5592b749ff0e71fb4aef019ff24741f6b25f231edde6c89ffc55d70abcc903b69e1a229db0c44084012d56522f584a8
|
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :rspec do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
11
|
+
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
12
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
13
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
14
|
+
watch('config/routes.rb') { "spec/routing" }
|
15
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
16
|
+
|
17
|
+
# Capybara features specs
|
18
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
19
|
+
|
20
|
+
# Turnip features and steps
|
21
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
22
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
23
|
+
end
|
24
|
+
|
data/lib/time_frame/empty.rb
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
# Encoding: utf-8
|
2
|
+
|
3
|
+
# Temporary disable class length cop.
|
4
|
+
# rubocop:disable Style/ClassLength
|
5
|
+
|
2
6
|
# The time frame class provides an specialized and enhanced range for time
|
3
7
|
# values.
|
4
8
|
class TimeFrame
|
@@ -24,23 +28,42 @@ class TimeFrame
|
|
24
28
|
end
|
25
29
|
|
26
30
|
def cover?(element)
|
27
|
-
if
|
31
|
+
if rangy?(element)
|
28
32
|
element.empty? || min <= element.min && element.max <= max
|
29
33
|
else
|
30
34
|
min <= element && element <= max
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
34
|
-
def
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
38
|
+
def before?(item)
|
39
|
+
case
|
40
|
+
when rangy?(item)
|
41
|
+
fail_if_empty item
|
42
|
+
item.min > max
|
43
|
+
else
|
44
|
+
item > max
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def after?(item)
|
49
|
+
case
|
50
|
+
when rangy?(item)
|
51
|
+
fail_if_empty item
|
52
|
+
item.max < min
|
53
|
+
else
|
54
|
+
item < min
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def time_between(item)
|
59
|
+
case
|
60
|
+
when rangy?(item)
|
61
|
+
fail_if_empty item
|
62
|
+
[time_between(item.min), time_between(item.max)].min_by(&:abs)
|
63
|
+
when cover?(item)
|
39
64
|
0
|
40
|
-
elsif item < min
|
41
|
-
item - min
|
42
65
|
else
|
43
|
-
item - max
|
66
|
+
[(item - min).abs, (item - max).abs].min
|
44
67
|
end
|
45
68
|
end
|
46
69
|
|
@@ -124,7 +147,18 @@ class TimeFrame
|
|
124
147
|
|
125
148
|
private
|
126
149
|
|
150
|
+
def fail_if_empty(item)
|
151
|
+
fail ArgumentError, 'time frame is empty' if item.respond_to?(:empty) &&
|
152
|
+
item.empty?
|
153
|
+
end
|
154
|
+
|
155
|
+
def rangy?(item)
|
156
|
+
item.respond_to?(:min) && item.respond_to?(:max)
|
157
|
+
end
|
158
|
+
|
127
159
|
def check_bounds(max, min)
|
128
160
|
fail ArgumentError, 'min is greater than max.' if min > max
|
129
161
|
end
|
130
162
|
end
|
163
|
+
|
164
|
+
# rubocop:enable Style/ClassLength
|
data/lib/time_frame/version.rb
CHANGED
data/spec/time_frame_spec.rb
CHANGED
@@ -276,7 +276,7 @@ describe TimeFrame do
|
|
276
276
|
end
|
277
277
|
end
|
278
278
|
|
279
|
-
describe '#
|
279
|
+
describe '#time_between' do
|
280
280
|
let(:time_frame) do
|
281
281
|
TimeFrame.new(min: Time.zone.local(2012), duration: 2.days)
|
282
282
|
end
|
@@ -284,29 +284,29 @@ describe TimeFrame do
|
|
284
284
|
describe 'when self covers time' do
|
285
285
|
context 'and time equals min' do
|
286
286
|
let(:time) { time_frame.min }
|
287
|
-
subject { time_frame.
|
287
|
+
subject { time_frame.time_between(time) }
|
288
288
|
it { should eq 0.minutes }
|
289
289
|
end
|
290
290
|
context 'and time equals max' do
|
291
291
|
let(:time) { time_frame.max }
|
292
|
-
subject { time_frame.
|
292
|
+
subject { time_frame.time_between(time) }
|
293
293
|
it { should eq 0.minutes }
|
294
294
|
end
|
295
295
|
context 'and time is an interior point of self' do
|
296
296
|
let(:time) { time_frame.min + (time_frame.duration / 2.0) }
|
297
|
-
subject { time_frame.
|
297
|
+
subject { time_frame.time_between(time) }
|
298
298
|
it { should eq 0.minutes }
|
299
299
|
end
|
300
300
|
end
|
301
301
|
context 'when self do not cover time' do
|
302
302
|
context 'and time is smaller than the left bound' do
|
303
303
|
let(:time) { time_frame.min - 42.hours - 42.minutes }
|
304
|
-
subject { time_frame.
|
305
|
-
it { should eq(
|
304
|
+
subject { time_frame.time_between(time) }
|
305
|
+
it { should eq(42.hours + 42.minutes) }
|
306
306
|
end
|
307
307
|
context 'and time is greater than the right bound' do
|
308
308
|
let(:time) { time_frame.max + 42.hours + 42.minutes }
|
309
|
-
subject { time_frame.
|
309
|
+
subject { time_frame.time_between(time) }
|
310
310
|
it { should eq 42.hours + 42.minutes }
|
311
311
|
end
|
312
312
|
end
|
@@ -315,40 +315,40 @@ describe TimeFrame do
|
|
315
315
|
describe 'when self overlaps other' do
|
316
316
|
context 'and its partly' do
|
317
317
|
let(:other) { time_frame.shift_by(time_frame.duration / 2) }
|
318
|
-
subject { time_frame.
|
318
|
+
subject { time_frame.time_between(other) }
|
319
319
|
it { should eq 0.minutes }
|
320
320
|
end
|
321
321
|
context 'and time equals max' do
|
322
322
|
let(:other) { time_frame }
|
323
|
-
subject { time_frame.
|
323
|
+
subject { time_frame.time_between(other) }
|
324
324
|
it { should eq 0.minutes }
|
325
325
|
end
|
326
326
|
context 'and other lies in the interior of self' do
|
327
327
|
let(:other) do
|
328
328
|
TimeFrame.new(min: time_frame.min + 1.hour, duration: 1.hour)
|
329
329
|
end
|
330
|
-
subject { time_frame.
|
330
|
+
subject { time_frame.time_between(other) }
|
331
331
|
it { should eq 0.minutes }
|
332
332
|
end
|
333
333
|
end
|
334
334
|
context 'when self do not cover time' do
|
335
335
|
context 'and time is smaller than the left bound' do
|
336
336
|
let(:other) { time_frame.shift_by(-2.days - 42.seconds) }
|
337
|
-
subject { time_frame.
|
338
|
-
it { should eq(
|
337
|
+
subject { time_frame.time_between(other) }
|
338
|
+
it { should eq(42.seconds) }
|
339
339
|
end
|
340
340
|
context 'and time is greater than the right bound' do
|
341
341
|
let(:other) { time_frame.shift_by(2.days + 42.seconds) }
|
342
|
-
subject { time_frame.
|
342
|
+
subject { time_frame.time_between(other) }
|
343
343
|
it { should eq 42.seconds }
|
344
344
|
end
|
345
345
|
end
|
346
346
|
it 'fails when only argument is empty' do
|
347
|
-
expect(-> { time_frame.
|
347
|
+
expect(-> { time_frame.time_between(TimeFrame::EMPTY) })
|
348
348
|
.to raise_error ArgumentError
|
349
349
|
end
|
350
350
|
it 'fails when only self is empty' do
|
351
|
-
expect(-> { TimeFrame::EMPTY.
|
351
|
+
expect(-> { TimeFrame::EMPTY.time_between(time_frame) })
|
352
352
|
.to raise_error TypeError
|
353
353
|
end
|
354
354
|
end
|
@@ -1095,4 +1095,158 @@ describe TimeFrame do
|
|
1095
1095
|
expect(TimeFrame::EMPTY.inspect).to eq 'EMPTY'
|
1096
1096
|
end
|
1097
1097
|
end
|
1098
|
+
|
1099
|
+
describe '#before?' do
|
1100
|
+
context 'when dealing with Time instances' do
|
1101
|
+
it 'returns false if time is before time frame' do
|
1102
|
+
time = Time.new(2012, 2, 1)
|
1103
|
+
time_frame = TimeFrame.new(min: time, duration: 3.hours)
|
1104
|
+
some_time = time - 1.hour
|
1105
|
+
expect(time_frame.before?(some_time)).to be false
|
1106
|
+
end
|
1107
|
+
|
1108
|
+
it 'returns false if time is on time frame min value' do
|
1109
|
+
time_frame = TimeFrame.new(min: time, duration: 3.hours)
|
1110
|
+
expect(time_frame.before?(time)).to be false
|
1111
|
+
end
|
1112
|
+
|
1113
|
+
it 'returns false if time is on time frame max value' do
|
1114
|
+
time = Time.new(2012, 2, 1)
|
1115
|
+
time_frame = TimeFrame.new(min: time - 1.hour, max: time)
|
1116
|
+
expect(time_frame.before?(time)).to be false
|
1117
|
+
end
|
1118
|
+
|
1119
|
+
it 'returns false if time is covered by time frame' do
|
1120
|
+
time = Time.new(2012, 2, 1)
|
1121
|
+
time_frame = TimeFrame.new(min: time, duration: 3.hours)
|
1122
|
+
some_time = time + 2.hours
|
1123
|
+
expect(time_frame.before?(some_time)).to be false
|
1124
|
+
end
|
1125
|
+
|
1126
|
+
it 'returns true if time is behind time frame max value' do
|
1127
|
+
time = Time.new(2012, 2, 1)
|
1128
|
+
time_frame = TimeFrame.new(min: time, duration: 3.hours)
|
1129
|
+
some_time = time + 10.hours
|
1130
|
+
expect(time_frame.before?(some_time)).to be true
|
1131
|
+
end
|
1132
|
+
end
|
1133
|
+
|
1134
|
+
context 'when dealing with TimeFrame instances' do
|
1135
|
+
it 'returns false if time frame in question is before time frame' do
|
1136
|
+
time_frame = TimeFrame.new(min: Time.new(2012, 2, 1), duration: 2.hours)
|
1137
|
+
other = TimeFrame.new(min: Time.new(2011), duration: 1.hour)
|
1138
|
+
expect(time_frame.before?(other)).to be false
|
1139
|
+
end
|
1140
|
+
|
1141
|
+
it 'returns false if time frame in question ends on min value' do
|
1142
|
+
time_frame = TimeFrame.new(min: Time.new(2012, 2, 1), duration: 2.hours)
|
1143
|
+
other = TimeFrame.new(min: Time.new(2011), max: time_frame.min)
|
1144
|
+
expect(time_frame.before?(other)).to be false
|
1145
|
+
end
|
1146
|
+
|
1147
|
+
it 'returns false if time frame in question is covered by frame' do
|
1148
|
+
time_frame = TimeFrame.new(min: Time.new(2012, 2, 1), duration: 2.hours)
|
1149
|
+
other = TimeFrame.new(
|
1150
|
+
min: time_frame.min + 1.hour,
|
1151
|
+
max: time_frame.min + 2.hours
|
1152
|
+
)
|
1153
|
+
expect(time_frame.before?(other)).to be false
|
1154
|
+
end
|
1155
|
+
|
1156
|
+
it 'returns false if time frame in question starts at max' do
|
1157
|
+
time_frame = TimeFrame.new(min: Time.new(2012, 2, 1), duration: 2.hours)
|
1158
|
+
other = TimeFrame.new(
|
1159
|
+
min: time_frame.max,
|
1160
|
+
max: time_frame.max + 2.hours
|
1161
|
+
)
|
1162
|
+
expect(time_frame.before?(other)).to be false
|
1163
|
+
end
|
1164
|
+
|
1165
|
+
it 'returns true if time frame in question lies after time frame' do
|
1166
|
+
time_frame = TimeFrame.new(min: Time.new(2012, 2, 1), duration: 2.hours)
|
1167
|
+
other = TimeFrame.new(
|
1168
|
+
min: time_frame.max + 1.hour,
|
1169
|
+
max: time_frame.max + 2.hours
|
1170
|
+
)
|
1171
|
+
expect(time_frame.before?(other)).to be true
|
1172
|
+
end
|
1173
|
+
end
|
1174
|
+
end
|
1175
|
+
|
1176
|
+
describe '#after?' do
|
1177
|
+
context 'when dealing with Time instances' do
|
1178
|
+
it 'returns true if time is before time frame' do
|
1179
|
+
time = Time.new(2012, 2, 1)
|
1180
|
+
time_frame = TimeFrame.new(min: time, duration: 3.hours)
|
1181
|
+
some_time = time - 1.hour
|
1182
|
+
expect(time_frame.after?(some_time)).to be true
|
1183
|
+
end
|
1184
|
+
|
1185
|
+
it 'returns false if time is on time frame min value' do
|
1186
|
+
time_frame = TimeFrame.new(min: time, duration: 3.hours)
|
1187
|
+
expect(time_frame.after?(time)).to be false
|
1188
|
+
end
|
1189
|
+
|
1190
|
+
it 'returns false if time is on time frame max value' do
|
1191
|
+
time = Time.new(2012, 2, 1)
|
1192
|
+
time_frame = TimeFrame.new(min: time - 1.hour, max: time)
|
1193
|
+
expect(time_frame.after?(time)).to be false
|
1194
|
+
end
|
1195
|
+
|
1196
|
+
it 'returns false if time is covered by time frame' do
|
1197
|
+
time = Time.new(2012, 2, 1)
|
1198
|
+
time_frame = TimeFrame.new(min: time, duration: 3.hours)
|
1199
|
+
some_time = time + 2.hours
|
1200
|
+
expect(time_frame.after?(some_time)).to be false
|
1201
|
+
end
|
1202
|
+
|
1203
|
+
it 'returns false if time is behind time frame max value' do
|
1204
|
+
time = Time.new(2012, 2, 1)
|
1205
|
+
time_frame = TimeFrame.new(min: time, duration: 3.hours)
|
1206
|
+
some_time = time + 10.hours
|
1207
|
+
expect(time_frame.after?(some_time)).to be false
|
1208
|
+
end
|
1209
|
+
end
|
1210
|
+
|
1211
|
+
context 'when dealing with TimeFrame instances' do
|
1212
|
+
it 'returns false if time frame in question is after time frame' do
|
1213
|
+
time_frame = TimeFrame.new(min: Time.new(2012, 2, 1), duration: 2.hours)
|
1214
|
+
other = TimeFrame.new(min: Time.new(2014), duration: 1.hour)
|
1215
|
+
expect(time_frame.after?(other)).to be false
|
1216
|
+
end
|
1217
|
+
|
1218
|
+
it 'returns false if time frame in question ends on min value' do
|
1219
|
+
time_frame = TimeFrame.new(min: Time.new(2012, 2, 1), duration: 2.hours)
|
1220
|
+
other = TimeFrame.new(min: Time.new(2011), max: time_frame.min)
|
1221
|
+
expect(time_frame.after?(other)).to be false
|
1222
|
+
end
|
1223
|
+
|
1224
|
+
it 'returns false if time frame in question is covered by frame' do
|
1225
|
+
time_frame = TimeFrame.new(min: Time.new(2012, 2, 1), duration: 2.hours)
|
1226
|
+
other = TimeFrame.new(
|
1227
|
+
min: time_frame.min + 1.hour,
|
1228
|
+
max: time_frame.min + 2.hours
|
1229
|
+
)
|
1230
|
+
expect(time_frame.after?(other)).to be false
|
1231
|
+
end
|
1232
|
+
|
1233
|
+
it 'returns false if time frame in question starts at max' do
|
1234
|
+
time_frame = TimeFrame.new(min: Time.new(2012, 2, 1), duration: 2.hours)
|
1235
|
+
other = TimeFrame.new(
|
1236
|
+
min: time_frame.max,
|
1237
|
+
max: time_frame.max + 2.hours
|
1238
|
+
)
|
1239
|
+
expect(time_frame.after?(other)).to be false
|
1240
|
+
end
|
1241
|
+
|
1242
|
+
it 'returns true if time frame in question is before time frame' do
|
1243
|
+
time_frame = TimeFrame.new(min: Time.new(2012, 2, 1), duration: 2.hours)
|
1244
|
+
other = TimeFrame.new(
|
1245
|
+
min: time_frame.min - 10.hours,
|
1246
|
+
max: time_frame.min - 5.hours
|
1247
|
+
)
|
1248
|
+
expect(time_frame.after?(other)).to be true
|
1249
|
+
end
|
1250
|
+
end
|
1251
|
+
end
|
1098
1252
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_frame
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Derichs
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-07-
|
13
|
+
date: 2014-07-23 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
@@ -109,6 +109,7 @@ files:
|
|
109
109
|
- ".rubocop.yml"
|
110
110
|
- ".travis.yml"
|
111
111
|
- Gemfile
|
112
|
+
- Guardfile
|
112
113
|
- README.md
|
113
114
|
- Rakefile
|
114
115
|
- lib/time_frame.rb
|