weighable 0.7.1 → 0.8.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 +11 -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: 041e5b084228b37677243615538ee113378e3194
|
4
|
+
data.tar.gz: b9cbe0b310dfdbaf188c84e30cc9746b21cd7ed6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 533e20a981cd6f0b40fd9d45641190e7fe87e118684569f3cf1bf3cca5ab8364cd6446c2934175c79a6183e3b66fe1e6f05618d3fa2e8811cd14d3d0f4ba1a3b
|
7
|
+
data.tar.gz: 7099cd4139a11dc54261a80f51b251160474a3eb2a915f175b60c3ea6f3094245cb59ae13dca6b42b3f87835f2c889acde83e54c00f5e01f810206672ca973f7
|
data/lib/weighable/version.rb
CHANGED
data/lib/weighable/weight.rb
CHANGED
@@ -88,11 +88,21 @@ module Weighable
|
|
88
88
|
|
89
89
|
def self.parse(string)
|
90
90
|
value, unit = string.split(' ')
|
91
|
-
|
91
|
+
from_value_and_unit(value, unit)
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.from_value_and_unit(value, unit)
|
95
|
+
unit = parse_unit(unit)
|
92
96
|
fail ArgumentError, 'invalid weight' if unit.nil? || value.nil?
|
93
97
|
Weight.new(value, unit)
|
94
98
|
end
|
95
99
|
|
100
|
+
def self.parse_unit(unit)
|
101
|
+
unit = ActiveSupport::Inflector.singularize(unit.downcase) unless unit.nil?
|
102
|
+
unit_symbol = unit ? unit.to_sym : unit
|
103
|
+
UNIT[unit_symbol] || ABBREVIATION_ALIASES[unit]
|
104
|
+
end
|
105
|
+
|
96
106
|
def initialize(value, unit)
|
97
107
|
@value = value.to_d
|
98
108
|
@unit = unit.is_a?(Fixnum) ? unit : unit_from_symbol(unit.to_sym)
|