weighable 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/weighable/version.rb +1 -1
- data/lib/weighable/weight.rb +13 -1
- 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: c03bc3a4ff0bfd00b9d109976a410d2f388655f5
|
4
|
+
data.tar.gz: 360fc90c09b6d6a7a6201ec7b145cac15f9d8f0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b331cea39251b48e885f9a829f9bb843694c6bc111b11460dc31a40875255e3e4bdbb87fd8c4b953b370e4e4589d25f977bb928be1004f4221c9843fdf4a736
|
7
|
+
data.tar.gz: 851a333bb993776932edf2ee5109e08e87310ba0ae3d6b6e9db04d4babeec293c73b9e399904daf483e32839a86b2032c9242208fb9cc517e43799bcaa98b248
|
data/lib/weighable/version.rb
CHANGED
data/lib/weighable/weight.rb
CHANGED
@@ -22,6 +22,16 @@ module Weighable
|
|
22
22
|
unit: nil
|
23
23
|
}.freeze
|
24
24
|
|
25
|
+
ABBREVATION_ALIASES = {
|
26
|
+
'g' => :gram,
|
27
|
+
'oz' => :ounce,
|
28
|
+
'lb' => :pound,
|
29
|
+
'mg' => :milligram,
|
30
|
+
'kg' => :kilogram,
|
31
|
+
'ea' => :unit,
|
32
|
+
nil => :unit
|
33
|
+
}.freeze
|
34
|
+
|
25
35
|
GRAMS_PER_OUNCE = BigDecimal.new('28.34952')
|
26
36
|
GRAMS_PER_POUND = BigDecimal.new('453.59237')
|
27
37
|
OUNCES_PER_POUND = BigDecimal.new('16')
|
@@ -78,7 +88,9 @@ module Weighable
|
|
78
88
|
|
79
89
|
def self.parse(string)
|
80
90
|
value, unit = string.split(' ')
|
81
|
-
|
91
|
+
unit = ABBREVATION_ALIASES[unit]
|
92
|
+
fail ArgumentError, 'invalid weight' if unit.nil?
|
93
|
+
Weight.new(value, unit)
|
82
94
|
end
|
83
95
|
|
84
96
|
def initialize(value, unit)
|