bmicalc 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/bmicalc.rb +20 -3
- data/lib/bmicalc/version.rb +1 -1
- data/spec/bmicalc_spec.rb +48 -31
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: caddc3d9ae32bd5dbc245aaf0ce951f436f5b6e5
|
4
|
+
data.tar.gz: 2ca53c881e67210ee7167012fa02aa0fcab38877
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e10b639bd9f98a46ddd4240c7647c97fb74cb9f26ed9e424efa3b1d28daa6bc3034a28187b64b031b495e7b793909dbb5e15a6195bb98767490116bae51d8015
|
7
|
+
data.tar.gz: a3f7cbb9e7d0b3454a55d612ae0678e2d351a0d3c6ba23f6ac25ada822dd5d4fa536e7ef317339601dd5d2f91262029e2970989c8e6fbd79424b3c9b26114c37
|
data/lib/bmicalc.rb
CHANGED
@@ -6,9 +6,10 @@ class Bmicalc
|
|
6
6
|
|
7
7
|
def initialize(options = {})
|
8
8
|
@round = options.fetch(:round, true)
|
9
|
+
@metric = options.fetch(:metric, true)
|
9
10
|
end
|
10
11
|
|
11
|
-
def result
|
12
|
+
def result
|
12
13
|
error unless weight && height
|
13
14
|
calculate_result
|
14
15
|
end
|
@@ -16,10 +17,26 @@ class Bmicalc
|
|
16
17
|
private
|
17
18
|
|
18
19
|
def calculate_result
|
20
|
+
if @metric
|
21
|
+
round(metric)
|
22
|
+
else
|
23
|
+
round(imperial)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def metric
|
28
|
+
weight / (height * height)
|
29
|
+
end
|
30
|
+
|
31
|
+
def imperial
|
32
|
+
(weight * 703) / (height * height)
|
33
|
+
end
|
34
|
+
|
35
|
+
def round(result)
|
19
36
|
if @round
|
20
|
-
|
37
|
+
result.round
|
21
38
|
else
|
22
|
-
|
39
|
+
result
|
23
40
|
end
|
24
41
|
end
|
25
42
|
|
data/lib/bmicalc/version.rb
CHANGED
data/spec/bmicalc_spec.rb
CHANGED
@@ -2,48 +2,65 @@ require 'bmicalc'
|
|
2
2
|
|
3
3
|
describe Bmicalc do
|
4
4
|
|
5
|
-
context '
|
5
|
+
context 'metric' do
|
6
|
+
context 'returns bmi' do
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
it 'underweight' do
|
9
|
+
subject.weight = 54
|
10
|
+
subject.height = 1.78
|
11
|
+
expect(subject.result).to eq(17)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'ideal' do
|
15
|
+
subject.weight = 64
|
16
|
+
subject.height = 1.78
|
17
|
+
expect(subject.result).to eq(20)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'overweight' do
|
21
|
+
subject.weight = 68
|
22
|
+
subject.height = 1.48
|
23
|
+
expect(subject.result).to eq(31)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'obese' do
|
27
|
+
subject.weight = 118
|
28
|
+
subject.height = 1.68
|
29
|
+
expect(subject.result).to eq(42)
|
30
|
+
end
|
11
31
|
end
|
12
32
|
|
13
|
-
it '
|
14
|
-
subject.weight = 64
|
33
|
+
it 'returns correct error message for weight' do
|
15
34
|
subject.height = 1.78
|
16
|
-
|
35
|
+
proc { subject.result }.should raise_error(StandardError, 'weight not set')
|
17
36
|
end
|
18
37
|
|
19
|
-
it '
|
20
|
-
subject.weight =
|
21
|
-
subject.height
|
22
|
-
expect(subject.result).to eq(31)
|
38
|
+
it 'returns correct error message for height' do
|
39
|
+
subject.weight = 64
|
40
|
+
proc { subject.result }.should raise_error(StandardError, 'height not set')
|
23
41
|
end
|
24
42
|
|
25
|
-
it '
|
26
|
-
|
27
|
-
|
28
|
-
|
43
|
+
it 'returns bmi without rounding' do
|
44
|
+
bmi = Bmicalc.new(round: false)
|
45
|
+
bmi.weight = 64
|
46
|
+
bmi.height = 1.78
|
47
|
+
expect(bmi.result).to eq(20.199469763918696)
|
29
48
|
end
|
30
49
|
end
|
31
50
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
proc { subject.result }.should raise_error(StandardError, 'height not set')
|
40
|
-
end
|
51
|
+
context 'imperial' do
|
52
|
+
it 'calculates with imperial values' do
|
53
|
+
bmi = Bmicalc.new(metric: false)
|
54
|
+
bmi.weight = 141
|
55
|
+
bmi.height = 70
|
56
|
+
expect(bmi.result).to eq(20)
|
57
|
+
end
|
41
58
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
59
|
+
it 'returns bmi without rounding' do
|
60
|
+
bmi = Bmicalc.new(metric: false, round: false)
|
61
|
+
bmi.weight = 141
|
62
|
+
bmi.height = 70.1
|
63
|
+
expect(bmi.result).to eq(20.171509622487545)
|
64
|
+
end
|
47
65
|
end
|
48
|
-
|
49
66
|
end
|