measurement 0.2.3.1 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.2.4 Suggested units for parsing
2
+
1
3
  v0.2.3.1 Fix for missing file
2
4
 
3
5
  v0.2.3 Group formats
@@ -22,9 +22,9 @@ require File.join(File.dirname(__FILE__), '..', 'measurement')
22
22
  #
23
23
  class Length < Measurement::Base
24
24
  base :metre, :metres, :suffix => 'm', :group => :metric
25
- unit 1000, :kilometre, :kilometres, :km, :suffix => 'km', :group => :metric
26
- unit 0.01, :centimetre, :centimetres, :cm, :suffix => 'cm', :group => :metric
27
- unit 0.001, :millimetre, :mm, :suffix => 'mm', :group => :metric
25
+ unit 1000, :kilometre, :kilometres, :km, :suffix => 'km'
26
+ unit 0.01, :centimetre, :centimetres, :cm, :suffix => 'cm'
27
+ unit 0.001, :millimetre, :mm, :suffix => 'mm'
28
28
 
29
29
  unit 0.0254, :inch, :inches, :suffix => '"', :group => :imperial
30
30
  unit 0.3048, :feet, :foot, :suffix => "'", :group => :imperial
@@ -6,9 +6,14 @@ module Measurement
6
6
  @units = units.sort.reverse
7
7
  end
8
8
 
9
+ def base
10
+ @units.first
11
+ end
12
+
9
13
  def format(amount, precision = 0)
10
14
  units = @units.dup
11
15
  strs = []
16
+ precision = 0 if units.size > 1
12
17
 
13
18
  while unit = units.shift
14
19
  n_in = unit.to(amount)
@@ -20,5 +25,13 @@ module Measurement
20
25
 
21
26
  strs.join(' ')
22
27
  end
28
+
29
+ def from(amount)
30
+ @units.first.from(amount)
31
+ end
32
+
33
+ def to(amount)
34
+ @units.first.to(amount)
35
+ end
23
36
  end
24
37
  end
@@ -16,10 +16,10 @@ require File.join(File.dirname(__FILE__), '..', 'measurement')
16
16
  # puts Length.parse('100kg').in_lbs_and_oz => 220lbs 7oz
17
17
  #
18
18
  class Weight < Measurement::Base
19
- base :gram, :grams, :suffix => 'g', :group => :metric
19
+ base :gram, :grams, :suffix => 'g'
20
20
  unit 1000.0, :kilogram, :kilograms, :kg, :kgs, :suffix => 'kg', :group => :metric
21
21
 
22
- unit 453.59236, :pounds, :pound, :lbs, :suffix => 'lbs', :group => [:imperial, :us]
22
+ unit 453.59236, :pounds, :pound, :lbs, :suffix => 'lb', :group => [:imperial, :us]
23
23
  unit 28.3495231, :ounces, :ounce, :oz, :suffix => 'oz', :group => :us
24
24
  unit 6350.29318, :stone, :st, :suffix => 'st', :group => :imperial
25
25
  end
data/lib/measurement.rb CHANGED
@@ -145,7 +145,7 @@ module Measurement
145
145
  units.detect do |unit|
146
146
  unit.has_name?(scale.to_sym) ||
147
147
  unit.suffix == scale.to_s
148
- end
148
+ end || fetch_group(scale)
149
149
  end
150
150
 
151
151
  def self.from(amount, unit) # :nodoc:
@@ -169,13 +169,17 @@ module Measurement
169
169
  # Length.parse("180cm").in_cm => 180
170
170
  # Length.parse("10m 11cm 12mm").in_metres => 10.122
171
171
  #
172
+ # If the string does not specify the unit then the base
173
+ # unit will be used unless a unit is suggested.
174
+ #
172
175
  # If a valid unit cannot be found an error is raised:
173
176
  #
174
177
  # Weight.parse("180cm") => Measurement::NoUnitFoundException
175
178
  #
176
- def self.parse(string)
177
- string = string.dup
179
+ def self.parse(string, suggested_unit = nil)
180
+ string = string.to_s.dup # Make sure we have a string and duplicate it
178
181
  base_amount = 0.0
182
+ suggested_unit = suggested_unit ? find_scale(suggested_unit) : base
179
183
 
180
184
  while string =~ /(\d+(\.\d+)?)([^\d]*)/
181
185
  amount = $1.to_f
@@ -190,7 +194,7 @@ module Measurement
190
194
  base_amount += unit.from(amount)
191
195
  end
192
196
  else
193
- base_amount += amount
197
+ base_amount += suggested_unit.from(amount)
194
198
  end
195
199
 
196
200
  string.sub!(/(\d+(\.\d+)?)([^\d]*)/, '')
data/measurement.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{measurement}
5
- s.version = "0.2.3.1"
5
+ s.version = "0.2.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jeremy Wells"]
@@ -40,6 +40,14 @@ describe Measurement::Base do
40
40
  Length.parse('113').to_f.should == 113.0
41
41
  end
42
42
 
43
+ it 'should parse in the suggest unit if no unit is in the text' do
44
+ Length.parse('113', :inches).to_f.should == 2.8702
45
+ end
46
+
47
+ it 'should parse the group base unit if the suggest unit is a group and no unit is in the text' do
48
+ Length.parse('113', :metric).to_f.should == 113.0
49
+ end
50
+
43
51
  it 'should parse floating point numbers' do
44
52
  Length.parse('113.43').to_f.should == 113.43
45
53
  end
@@ -67,6 +75,26 @@ describe Measurement::Base do
67
75
  end
68
76
  end
69
77
 
78
+ describe '#initialize' do
79
+ before do
80
+ @klass = Class.new(Measurement::Base)
81
+ @klass.base :grams
82
+ @klass.unit 1000, :kilograms, :group => :metric
83
+ end
84
+
85
+ it 'should initialize to the base value' do
86
+ @klass.new(1000).to_f.should == 1000.0
87
+ end
88
+
89
+ it 'should initialize to the specified unit' do
90
+ @klass.new(1000, :kilograms).to_f.should == 1000000.0
91
+ end
92
+
93
+ it 'should initialize to the base unit of the specified group' do
94
+ @klass.new(1000, :metric).to_f.should == 1000000.0
95
+ end
96
+ end
97
+
70
98
  describe 'operators' do
71
99
  before do
72
100
  @length_1 = Length.new(1)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measurement
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3.1
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Wells