measurement 0.2.1 → 0.2.2
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.
- data/CHANGELOG +2 -0
- data/lib/measurement/unit.rb +2 -1
- data/lib/measurement/weight.rb +1 -1
- data/lib/measurement.rb +23 -6
- data/measurement.gemspec +1 -1
- data/spec/lib/measurement_spec.rb +18 -0
- data/spec/lib/unit_spec.rb +4 -0
- data/spec/lib/weight_spec.rb +1 -1
- metadata +1 -1
data/CHANGELOG
CHANGED
data/lib/measurement/unit.rb
CHANGED
data/lib/measurement/weight.rb
CHANGED
@@ -17,7 +17,7 @@ require File.join(File.dirname(__FILE__), '..', 'measurement')
|
|
17
17
|
#
|
18
18
|
class Weight < Measurement::Base
|
19
19
|
base :gram, :grams, :suffix => 'g'
|
20
|
-
unit 1000.0, :kilograms, :kg, :kgs, :suffix => 'kg'
|
20
|
+
unit 1000.0, :kilogram, :kilograms, :kg, :kgs, :suffix => 'kg'
|
21
21
|
unit 453.59236, :pounds, :pound, :lbs, :suffix => 'lbs'
|
22
22
|
unit 28.3495231, :ounces, :ounce, :oz, :suffix => 'oz'
|
23
23
|
unit 6350.29318, :stone, :st, :suffix => 'st'
|
data/lib/measurement.rb
CHANGED
@@ -1,7 +1,18 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'measurement', 'unit')
|
2
2
|
|
3
3
|
module Measurement
|
4
|
-
class NoUnitFoundException < Exception
|
4
|
+
class NoUnitFoundException < Exception
|
5
|
+
attr_reader :unit
|
6
|
+
|
7
|
+
def initialize(unit)
|
8
|
+
@unit = unit
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
"No unit found: #{@unit}"
|
14
|
+
end
|
15
|
+
end
|
5
16
|
|
6
17
|
# The Measurement::Base class provides a basis for types of
|
7
18
|
# measurement. For example, length or weight. It should
|
@@ -100,15 +111,21 @@ module Measurement
|
|
100
111
|
end
|
101
112
|
|
102
113
|
def self.fetch_scale(scale = nil) # :nodoc:
|
103
|
-
scale.nil? ? base : units.detect do |unit|
|
104
|
-
unit.has_name?(scale)
|
114
|
+
unit = (scale.nil? ? base : units.detect do |unit|
|
115
|
+
unit.has_name?(scale.to_sym)
|
116
|
+
end)
|
117
|
+
|
118
|
+
unless unit
|
119
|
+
raise NoUnitFoundException.new(scale)
|
120
|
+
else
|
121
|
+
unit
|
105
122
|
end
|
106
123
|
end
|
107
124
|
|
108
125
|
def self.find_scale(scale) # :nodoc:
|
109
126
|
units.detect do |unit|
|
110
|
-
unit.has_name?(scale) ||
|
111
|
-
unit.suffix == scale
|
127
|
+
unit.has_name?(scale.to_sym) ||
|
128
|
+
unit.suffix == scale.to_s
|
112
129
|
end
|
113
130
|
end
|
114
131
|
|
@@ -135,7 +152,7 @@ module Measurement
|
|
135
152
|
#
|
136
153
|
# If a valid unit cannot be found an error is raised:
|
137
154
|
#
|
138
|
-
# Weight.parse("180cm") => Measurement::
|
155
|
+
# Weight.parse("180cm") => Measurement::NoUnitFoundException
|
139
156
|
#
|
140
157
|
def self.parse(string)
|
141
158
|
string = string.dup
|
data/measurement.gemspec
CHANGED
@@ -17,6 +17,24 @@ describe Measurement::Base do
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
+
describe '::find_scale' do
|
21
|
+
it 'should be able to find the unit by name' do
|
22
|
+
Weight.find_scale(:kilogram).should_not be_nil
|
23
|
+
Weight.find_scale(:kilogram).has_name?(:kilogram).should be_true
|
24
|
+
end
|
25
|
+
it 'should be able to find the unit if the name passed is a string' do
|
26
|
+
Weight.find_scale("kilogram").should_not be_nil
|
27
|
+
Weight.find_scale(:kilogram).has_name?(:kilogram).should be_true
|
28
|
+
end
|
29
|
+
it 'should be able to find the unit by suffix' do
|
30
|
+
Weight.unit 2000, :quit, :qt, :suffix => 'q'
|
31
|
+
Weight.find_scale('q').should_not be_nil
|
32
|
+
end
|
33
|
+
it 'should return nil if no unit is found' do
|
34
|
+
Weight.find_scale('gigboot').should be_nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
20
38
|
describe '::parse' do
|
21
39
|
it 'should parse in the base unit if no unit is specified' do
|
22
40
|
Length.parse('113').to_f.should == 113.0
|
data/spec/lib/unit_spec.rb
CHANGED
@@ -28,6 +28,10 @@ describe Measurement::Unit do
|
|
28
28
|
@unit.format(200,2).should == "1.33"
|
29
29
|
end
|
30
30
|
|
31
|
+
it 'should remove the end 0' do
|
32
|
+
@unit.format(255, 2).should == '1.7'
|
33
|
+
end
|
34
|
+
|
31
35
|
it 'should apply the suffix' do
|
32
36
|
@unit = Measurement::Unit.new(150.0, :test, :suffix => 'test')
|
33
37
|
@unit.format(150, 0).should == "1test"
|
data/spec/lib/weight_spec.rb
CHANGED