duranged 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/duranged/base.rb +21 -0
- data/lib/duranged/version.rb +1 -1
- data/spec/shared/base_examples.rb +59 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 642f12029f91fdc00c2f2e5982ebe82fc79abd9a
|
4
|
+
data.tar.gz: dd5cafa8f72224e081beb598e7e8d35c79e1b762
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ec1213caf3dfd5dcb196fd7e465f3bb85aeadceef35fdef0d8b85bf8a1359324fc81a3a6caaa0710b5e45123941b9012020ef0b0d16850cdabcaded98b9b369
|
7
|
+
data.tar.gz: 67856f597e86030ec143aff31312e81a9a8b1b43e2581c93dbcc3b75223c4b51b17f3f39b89ee68357fdeee5fb2917560d4f97e57275f905a7b3389f1d115eb0
|
data/lib/duranged/base.rb
CHANGED
@@ -48,6 +48,27 @@ module Duranged
|
|
48
48
|
self.class.new(to_i - other.to_i)
|
49
49
|
end
|
50
50
|
|
51
|
+
def round_to!(period=:minutes)
|
52
|
+
send("round_to_#{period}!")
|
53
|
+
end
|
54
|
+
|
55
|
+
def round_to_minutes!
|
56
|
+
if seconds >= 30
|
57
|
+
@value = value + (60 - seconds)
|
58
|
+
else
|
59
|
+
@value = value - seconds
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def round_to_hours!
|
64
|
+
round_to_minutes!
|
65
|
+
if minutes >= 30
|
66
|
+
@value = value + (60 - minutes).minutes
|
67
|
+
else
|
68
|
+
@value = value - minutes.minutes
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
51
72
|
protected
|
52
73
|
|
53
74
|
def parse_hash(hash)
|
data/lib/duranged/version.rb
CHANGED
@@ -180,6 +180,65 @@ RSpec.shared_examples "the base class" do |klass|
|
|
180
180
|
end
|
181
181
|
end
|
182
182
|
|
183
|
+
describe '#round_to!' do
|
184
|
+
context 'when passed minutes' do
|
185
|
+
it 'calls #round_to_minutes!' do
|
186
|
+
expect(subject).to receive(:round_to_minutes!)
|
187
|
+
subject.round_to! :minutes
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
context 'when passed hours' do
|
192
|
+
it 'calls #round_to_hours!' do
|
193
|
+
expect(subject).to receive(:round_to_hours!)
|
194
|
+
subject.round_to! :hours
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
context 'when not passed a period' do
|
199
|
+
it 'calls #round_to_minutes!' do
|
200
|
+
expect(subject).to receive(:round_to_minutes!)
|
201
|
+
subject.round_to!
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe '#round_to_minutes!' do
|
207
|
+
context 'when seconds is >= 30' do
|
208
|
+
subject { klass.new(3630.seconds) }
|
209
|
+
|
210
|
+
it 'rounds up to the next minute' do
|
211
|
+
expect(subject.round_to_minutes!.to_i).to eq 3660
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
context 'when seconds is < 30' do
|
216
|
+
subject { klass.new(3620.seconds) }
|
217
|
+
|
218
|
+
it 'rounds down to the minute' do
|
219
|
+
expect(subject.round_to_minutes!.to_i).to eq 3600
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
describe '#round_to_hours!' do
|
225
|
+
context 'when minutes is >= 30' do
|
226
|
+
subject { klass.new(90.minutes) }
|
227
|
+
|
228
|
+
it 'rounds up to the next hour' do
|
229
|
+
expect(subject.round_to_hours!.to_i).to eq 2.hours
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
context 'when minutes is < 30' do
|
234
|
+
subject { klass.new(80.minutes) }
|
235
|
+
|
236
|
+
it 'rounds down to the hour' do
|
237
|
+
expect(subject.round_to_hours!.to_i).to eq 1.hour
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
183
242
|
describe '#as_json' do
|
184
243
|
it 'returns the value integer' do
|
185
244
|
expect(subject.as_json).to eq subject.value
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: duranged
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rebec
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|