dimensional 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -4,4 +4,5 @@
4
4
  1.0.0 Production Release 1.
5
5
  1.0.1 Use Rationals for default values and avoid the dreaded integer division = 0 problem.
6
6
  1.1.1 Add Metric.load constructor, improve docs and enhance scanner.
7
- 2.0.0 Introduce Dimensional::Locale
7
+ 2.0.0 Introduce Dimensional::Locale
8
+ 2.0.2 Allow loading of non-positive metrics, particularly in Ruby 1.8.6 where Math.log10
data/TODO CHANGED
@@ -1,4 +1,6 @@
1
1
  1. Fix parsing units like "6m3" for six cubic meters.
2
2
  2. Allow locale-specific formatters and detectors.
3
3
  3. Default format per dimension/system/metric.
4
- 4. Auto generation of metric prefix units.
4
+ 4. Auto generation of metric prefix units.
5
+ 5. Change Metric representation into a subclass of BasicObject instead of DelegateClasss
6
+ (http://codeidol.com/other/rubyckbk/Numbers/Simulating-a-Subclass-of-Fixnum/)
@@ -76,6 +76,7 @@ module Dimensional
76
76
 
77
77
  # Create a new instance with the given value (assumed to be in the base unit) and convert it to the preferred unit.
78
78
  def load(v)
79
+ raise "No base unit defined" unless base
79
80
  new(v, base).preferred
80
81
  end
81
82
  end
@@ -104,8 +105,8 @@ module Dimensional
104
105
  # Convert into the best unit for the given Locale. The first system of the given locale with units is elected the preferred system,
105
106
  # and within the preferred system, preference is given to units yielding a metric whose order of magnitude is close to zero.
106
107
  def localize(locale = Locale.default)
107
- target_oom = Math.log10(self) + Math.log10(self.unit.factor)
108
108
  preferred_system = self.class.systems(locale).detect{ |s| self.class.units[s].any? }
109
+ target_oom = Math.log10(self.abs) + Math.log10(self.unit.factor) rescue -(1.0/0.0) # Ruby 1.8.6 raises an exception on log10(0) instead of -Infinity
109
110
  bu = self.class.best_fit(target_oom, preferred_system)
110
111
  convert(bu)
111
112
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module Dimensional
3
- VERSION = "2.0.1"
3
+ VERSION = "2.0.2"
4
4
  end
@@ -127,29 +127,29 @@ Configurator.start do
127
127
 
128
128
  dimension(:A) do
129
129
  system(:SI) do
130
- combine('square meter', 'm2', {Unit[:L, :SI, :meter] => 2}, :detector => /\A(sq\.?\s?met(er|re)s?|m2)\Z/) do
130
+ combine('square meter', 'm2', {Unit[:L, :SI, :meter] => 2}, :detector => /\A(sq(uare|\.)?\s+met(er|re)s?|m2)\Z/) do
131
131
  derive('hectare', 'ha', 10000, :format => "%.4f%U") do
132
132
  end
133
133
  end
134
134
  end
135
135
  system(:US) do # All measures below are approximations due to the difference between a survey foot and an international foot.
136
- combine('square yard', 'yd2', {Unit[:L, :US, :yard] => 2}, :detector => /yd2/) do
136
+ combine('square yard', 'yd2', {Unit[:L, :US, :yard] => 2}, :detector => /\A(sq(uare|\.)?\s+y(ar)?d|yd2)\Z/) do
137
137
  derive('acre', nil, 4840)
138
138
  end
139
- combine('square mile', nil, {Unit[:L, :US, :mile] => 2}, :detector => /\A(sq(uare|\.)?\s?miles?)\Z/) do
139
+ combine('square mile', nil, {Unit[:L, :US, :mile] => 2}, :detector => /\A(sq(uare|\.)?\s+mi(le)?s?|mi(le)?2)\Z/) do
140
140
  self.alias('section', nil, :detector => /\Asections?\Z/) do
141
141
  derive('township', 'twp', 36, :detector => /\Atownships?\Z/)
142
142
  end
143
143
  end
144
- combine('square foot', 'ft2', {Unit[:L, :US, :ft] => 2}, :detector => /ft2/)
145
- combine('square inch', 'in2', {Unit[:L, :US, :in] => 2}, :detector => /in2/)
144
+ combine('square foot', 'ft2', {Unit[:L, :US, :ft] => 2}, :detector => /\A(sq(uare|\.)?\s+f(oo|ee)?t|ft2)\Z/)
145
+ combine('square inch', 'in2', {Unit[:L, :US, :in] => 2}, :detector => /\A(sq(uare|\.)?\s+(ch(es)?)?|in2)\Z/)
146
146
  end
147
147
  end
148
148
 
149
149
  dimension(:V) do
150
150
  system(:SI) do
151
- combine('cubic meter', 'm3', {Unit[:L, :SI, :meter] => 3}, :detector => /\A(cubic met(er|re)s?|m3)\Z/, :preference => -2) do
152
- derive('cubic decimeter', 'dm3', Rational(1, 1000), :detector => /\A(cubic decimet(er|re)s?|dm3)\Z/, :preference => -3) do
151
+ combine('cubic meter', 'm3', {Unit[:L, :SI, :meter] => 3}, :detector => /\A(cubic\s+met(er|re)s?|m3)\Z/, :preference => -3) do
152
+ derive('cubic decimeter', 'dm3', Rational(1, 1000), :detector => /\A(cubic\s+decimet(er|re)s?|dm3)\Z/, :preference => -4) do
153
153
  self.alias('liter', 'l', :detector => /\A(lit(er|re)s?|l|L)\Z/) do
154
154
  derive('milliliter', 'ml', Rational(1, 1000), :detector => /\A(millilit(er|re)s?|ml|mL)\Z/)
155
155
  end
@@ -157,12 +157,12 @@ Configurator.start do
157
157
  end
158
158
  end
159
159
  system(:Imp) do
160
- reference('ounce', 'fl oz', Unit[:V, :SI, 'milliliter'], 28.4130625, :detector => /\A((fluid )?ounces?|oz)\Z/) do
160
+ reference('ounce', 'fl oz', Unit[:V, :SI, 'milliliter'], 28.4130625, :detector => /\A((fluid\s+)?ounces?|oz)\Z/) do
161
161
  derive('gill', 'gi', 5, :detector => /\A(gills?|gis?)\Z/) do
162
162
  derive('cup', 'cp', 2, :detector => /\A(cups?|cps?)\Z/) do
163
163
  derive('pint', 'pt', 2, :detector => /\A(pints?|pts?)\Z/) do
164
164
  derive('quart', 'qt', 2, :detector => /\A(quarts?|qts?)\Z/) do
165
- derive('gallon', 'gal', 4, :detector => /\A(gallons?|gals?)\Z/)
165
+ derive('gallon', 'gal', 4, :detector => /\A(gallons?|gal)\Z/)
166
166
  end
167
167
  end
168
168
  end
@@ -170,14 +170,14 @@ Configurator.start do
170
170
  end
171
171
  end
172
172
  system(:US) do
173
- combine('cubic inch', 'in3', {Unit[:L, :US, :inch] => 3}, :detector => /\A(cubic inch(es)?|in3)\Z/ ) do
174
- derive('gallon', 'gal', 231, :detector => /\A(gallons?|gals?)\Z/) do
173
+ combine('cubic inch', 'in3', {Unit[:L, :US, :inch] => 3}, :detector => /\A(cubic\s+in(ch(es)?)?|in3)\Z/ ) do
174
+ derive('gallon', 'gal', 231, :detector => /\A(gallons?|gal)\Z/) do
175
175
  derive('quart', 'qt', Rational(1,4), :detector => /\A(quarts?|qts?)\Z/) do
176
176
  derive('pint', 'pt', Rational(1,2), :detector => /\A(pints?|pts?)\Z/) do
177
177
  derive('cup', nil, Rational(1,2), :detector => /\Acups?\Z/) do
178
178
  derive('gill', 'gi', Rational(1,2), :detector => /\A(gills?|gis?)\Z/) do
179
- derive('fluid ounce', 'fl oz', Rational(1, 4), :detector => /\A((fluid )?ounces?|oz)\Z/) do
180
- derive('dram', 'dr', Rational(1, 8), :detector => /\A((fluid )?dra(ch)?ms?|(fl )?drs?)\Z/) do
179
+ derive('fluid ounce', 'fl oz', Rational(1, 4), :detector => /\A((fluid\s+)?ounces?|oz)\Z/) do
180
+ derive('dram', 'dr', Rational(1, 8), :detector => /\A((fluid\s+)?dra(ch)?ms?|(fl\s+)?drs?)\Z/) do
181
181
  derive('minim', '♏', Rational(1, 60))
182
182
  end
183
183
  end
@@ -306,4 +306,19 @@ class MetricTest < Test::Unit::TestCase
306
306
  assert_equal "1.8600nm", distance.parse('1.8565454 nm', Locale::BA).strfmeasure("%.4f%U")
307
307
  assert_equal "1.86", distance.parse('1.8565454 nm', Locale::BA).strfmeasure("%s")
308
308
  end
309
+
310
+ # A "true" metric (for some values of true) with negative magnitude has dubious semantics, but we try to support it.
311
+ def test_load_negative
312
+ distance = Class.new(Metric)
313
+ distance.dimension = Dimension::L
314
+ distance.base = @meter
315
+ assert_equal distance.new(-1, @meter), distance.load(-1)
316
+ end
317
+
318
+ def test_load_zero
319
+ distance = Class.new(Metric)
320
+ distance.dimension = Dimension::L
321
+ distance.base = @meter
322
+ assert_equal distance.new(0, @meter), distance.load(0)
323
+ end
309
324
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dimensional
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Hapgood
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-11 00:00:00 -05:00
12
+ date: 2010-02-17 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15