hour-ruby 0.0.4 → 0.0.5
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/lib/hour.rb +9 -0
- data/spec/hour_spec.rb +44 -21
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d933c2d8c4bf1a55e212a1f3339e0eadd990dc00d5a9adfba81bd7ffed47ff03
|
4
|
+
data.tar.gz: c9bc666450f39434c9f3f406050b7f496c717c9a9a67255806007ff9b70355d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60c5f824af31d47aaa49ddedc052a60330403046fff831c4b83514f5c033701227735d8c899569d45671dd2512494bae0218ef37ce5a98c9e4f52d51db781ba5
|
7
|
+
data.tar.gz: 1a5caab48563ba99f2e30b9c86f99a47299be769bda0c41b6e812621bbd37d0af3dbcfeac07aa9f4ea5bead14aaf6c982dc767b6f935e5ada2d96d88e9895388
|
data/lib/hour.rb
CHANGED
@@ -145,6 +145,10 @@ class Hour
|
|
145
145
|
end
|
146
146
|
|
147
147
|
def -(other)
|
148
|
+
if other.to_decimal > self.to_decimal
|
149
|
+
raise ArgumentError, "Negative hours not supported"
|
150
|
+
end
|
151
|
+
|
148
152
|
hours = @h - other.h - (@m - other.m - ((@s - other.s) / 60)) / 60
|
149
153
|
minutes = (@m - other.m - ((@s - other.s) / 60)) % 60
|
150
154
|
|
@@ -159,6 +163,11 @@ class Hour
|
|
159
163
|
self.class.new(hours, minutes, seconds)
|
160
164
|
end
|
161
165
|
|
166
|
+
def *(integer)
|
167
|
+
raise ArgumentError, "must be an integer" unless integer.integer?
|
168
|
+
self.class.from(seconds: (@h * integer * 3600) + (@m * integer * 60) + (@s * integer))
|
169
|
+
end
|
170
|
+
|
162
171
|
# Returns a decorator providing convenience methods for working with hours.
|
163
172
|
#
|
164
173
|
# Hour.new(1, 25).hours.round # => 1
|
data/spec/hour_spec.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'hour'
|
2
|
+
require 'coveralls'
|
3
3
|
|
4
4
|
Coveralls.wear!
|
5
5
|
|
6
6
|
describe Hour do
|
7
|
-
describe
|
7
|
+
describe '.parse' do
|
8
8
|
pending "implement me" #do
|
9
9
|
# TODO
|
10
|
-
# p Hour.parse(
|
10
|
+
# p Hour.parse('1:02:30')
|
11
11
|
#end
|
12
12
|
end
|
13
13
|
|
14
|
-
describe
|
14
|
+
describe '.now' do
|
15
15
|
pending "implement me" #do
|
16
16
|
# TODO
|
17
17
|
#end
|
18
18
|
end
|
19
19
|
|
20
|
-
describe
|
20
|
+
describe '#to_s' do
|
21
21
|
it "pads minutes and seconds with zeros" do
|
22
22
|
hour = Hour.new(1, 9, 5)
|
23
23
|
expect(hour.to_s).to eql('1:09:05')
|
@@ -41,7 +41,7 @@ describe Hour do
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
describe
|
44
|
+
describe '#+' do
|
45
45
|
it "returns a new Hour instance returning the total time of the two hour instances" do
|
46
46
|
hour = Hour.new(m: 25, s: 10) + Hour.new(h: 1)
|
47
47
|
expect(hour.hours.value).to eq(1)
|
@@ -50,8 +50,31 @@ describe Hour do
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
describe
|
54
|
-
|
53
|
+
describe '#-' do
|
54
|
+
it "raises an error if the result would end up negative" do
|
55
|
+
expect { Hour.new(m: 55, s: 30) - Hour.new(h: 1) }.to raise_error(ArgumentError)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns a new Hour instance returning the difference between the two times" do
|
59
|
+
pending "TODO: fix me"
|
60
|
+
hour = Hour.new(h: 1, m: 20) - Hour.new(m: 55)
|
61
|
+
expect(hour.hours.value).to eql(1)
|
62
|
+
expect(hour.minutes.value).to eql(15)
|
63
|
+
expect(hour.seconds.value).to eql(0)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#*' do
|
68
|
+
it "returns a new Hour instance returning the total time of the two hour instances" do
|
69
|
+
hour = Hour.new(m: 55, s: 10) * 3
|
70
|
+
expect(hour.hours.value).to eq(2)
|
71
|
+
expect(hour.minutes.value).to eq(45)
|
72
|
+
expect(hour.seconds.value).to eq(30)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '.from' do
|
77
|
+
context 'minutes' do
|
55
78
|
it "returns a new Hour instance" do
|
56
79
|
hour = Hour.from(minutes: 85)
|
57
80
|
expect(hour.hours.value).to eq(1)
|
@@ -60,7 +83,7 @@ describe Hour do
|
|
60
83
|
end
|
61
84
|
end
|
62
85
|
|
63
|
-
context
|
86
|
+
context 'seconds' do
|
64
87
|
it "returns a new Hour instance" do
|
65
88
|
hour = Hour.from(seconds: 2 * 60 * 60 + 25 * 60 + 5)
|
66
89
|
expect(hour.hours.value).to eq(2)
|
@@ -70,15 +93,15 @@ describe Hour do
|
|
70
93
|
end
|
71
94
|
end
|
72
95
|
|
73
|
-
describe
|
74
|
-
describe
|
96
|
+
describe '#hours' do
|
97
|
+
describe '#value' do
|
75
98
|
it "returns the exact hour value" do
|
76
99
|
expect(Hour.new(1, 10).hours.value).to eq(1)
|
77
100
|
expect(Hour.new(1, 59).hours.value).to eq(1)
|
78
101
|
end
|
79
102
|
end
|
80
103
|
|
81
|
-
describe
|
104
|
+
describe '#round' do
|
82
105
|
it "returns the rounded hour" do
|
83
106
|
expect(Hour.new(1, 10).hours.round).to eq(1)
|
84
107
|
expect(Hour.new(1, 29, 59).hours.round).to eq(1)
|
@@ -88,40 +111,40 @@ describe Hour do
|
|
88
111
|
end
|
89
112
|
end
|
90
113
|
|
91
|
-
describe
|
92
|
-
describe
|
114
|
+
describe '#minutes' do
|
115
|
+
describe '#value' do
|
93
116
|
it "returns the exact minute value" do
|
94
117
|
expect(Hour.new(1, 25, 52).minutes.value).to eq(25)
|
95
118
|
end
|
96
119
|
end
|
97
120
|
|
98
|
-
describe
|
121
|
+
describe '#round' do
|
99
122
|
it "returns the rounded minutes" do
|
100
123
|
expect(Hour.new(1, 25, 52).minutes.round).to eq(26)
|
101
124
|
end
|
102
125
|
end
|
103
126
|
|
104
|
-
describe
|
127
|
+
describe '#total' do
|
105
128
|
it "returns the total number of minutes" do
|
106
129
|
expect(Hour.new(1, 25, 52).minutes.total).to eq(85)
|
107
130
|
end
|
108
131
|
end
|
109
132
|
|
110
|
-
describe
|
133
|
+
describe '#round_total' do
|
111
134
|
it "returns the rounded total number of minutes" do
|
112
135
|
expect(Hour.new(1, 25, 52).minutes.round_total).to eq(86)
|
113
136
|
end
|
114
137
|
end
|
115
138
|
end
|
116
139
|
|
117
|
-
describe
|
118
|
-
describe
|
140
|
+
describe '#seconds' do
|
141
|
+
describe '#value' do
|
119
142
|
it "returns the exact second value" do
|
120
143
|
expect(Hour.new(m: 1, s: 25).seconds.value).to eq(25)
|
121
144
|
end
|
122
145
|
end
|
123
146
|
|
124
|
-
describe
|
147
|
+
describe '#total' do
|
125
148
|
it "returns the exact total number of seconds" do
|
126
149
|
expect(Hour.new(m: 1, s: 25).seconds.total).to eq(85)
|
127
150
|
expect(Hour.new(1, 10, 25).seconds.total).to eq(1 * 60 * 60 + 10 * 60 + 25)
|
metadata
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hour-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James C Russell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
14
|
-
|
13
|
+
description: Library for handling hour arithmetics. such as addition, multiplication,
|
14
|
+
conversion to decimal etc.
|
15
|
+
email: james+rubygems@botanicus.me
|
15
16
|
executables: []
|
16
17
|
extensions: []
|
17
18
|
extra_rdoc_files: []
|
@@ -42,5 +43,5 @@ requirements: []
|
|
42
43
|
rubygems_version: 3.0.3
|
43
44
|
signing_key:
|
44
45
|
specification_version: 4
|
45
|
-
summary:
|
46
|
+
summary: Library for handling hour arithmetics.
|
46
47
|
test_files: []
|