dimensional 1.0.1 → 1.1.1
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 +4 -3
- data/README +17 -4
- data/Rakefile +3 -2
- data/TODO +1 -0
- data/lib/dimensional/metric.rb +6 -1
- data/lib/dimensional/version.rb +1 -1
- data/test/demo.rb +11 -10
- data/test/metric_test.rb +33 -0
- metadata +3 -1
data/CHANGELOG
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
0.1.0
|
2
2
|
0.1.1 Simpler conversion system-to-system and redefinition of preference semantics.
|
3
|
-
0.1.2 Cleanup tests to work with gem check -t
|
4
|
-
1.0.0 Production Release 1
|
5
|
-
1.0.1 Use Rationals for default values and avoid the dreaded integer division = 0 problem
|
3
|
+
0.1.2 Cleanup tests to work with gem check -t.
|
4
|
+
1.0.0 Production Release 1.
|
5
|
+
1.0.1 Use Rationals for default values and avoid the dreaded integer division = 0 problem.
|
6
|
+
1.1.1 Add Metric.load constructor, improve docs and enhance scanner.
|
data/README
CHANGED
@@ -3,9 +3,22 @@
|
|
3
3
|
# unit-oriented methods into Ruby standard classes -to perform operations, particularly across dimensions, you
|
4
4
|
# will need to use basic conversion methods provided by this library combined with standard Ruby numerical oper-
|
5
5
|
# ations. There is no method_missing magic or large mixin of spiffy unit-like methods here.
|
6
|
-
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
6
|
+
#
|
7
|
+
# Here are some examples of what you can do with the Dimensional library:
|
8
|
+
#
|
9
|
+
# require 'dimensional'
|
10
|
+
# require 'test/demo'
|
11
|
+
# class Autonomy < Dimensional::Metric
|
12
|
+
# self.dimension = Dimensional::Dimension::L
|
13
|
+
# end
|
14
|
+
# range = Autonomy.parse("15 nautical miles")
|
15
|
+
# range.to_s # => "15M"
|
16
|
+
# range.base # => <Rational(27780, 1) <meter>>
|
17
|
+
# range.base.to_s # => "27780m"
|
18
|
+
# usrange = range.convert(Dimensional::Unit[:L, :US, :yd]) # => <30380.5774278215 <yard>>
|
19
|
+
#
|
20
|
+
# The demo library in the test directory contains a good starter set of units.
|
21
|
+
#
|
22
|
+
# The long-term objective for this library is to achieve compliance with the UCUM standard. References:
|
10
23
|
# UCUM Website: http://unitsofmeasure.org/
|
11
24
|
# UCUM Standard: http://aurora.regenstrief.org/~ucum/ucum.html
|
data/Rakefile
CHANGED
@@ -39,8 +39,9 @@ Rake::RDocTask.new do |rdoc|
|
|
39
39
|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
40
|
|
41
41
|
rdoc.rdoc_dir = 'rdoc'
|
42
|
-
rdoc.title = "
|
43
|
-
rdoc.rdoc_files.include(
|
42
|
+
rdoc.title = "Dimensional #{version}"
|
43
|
+
rdoc.rdoc_files.include(%w(README LICENSE CHANGELOG TODO))
|
44
|
+
rdoc.rdoc_files.include('test/demo.rb')
|
44
45
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
46
|
end
|
46
47
|
|
data/lib/dimensional/metric.rb
CHANGED
@@ -8,7 +8,7 @@ module Dimensional
|
|
8
8
|
# A Measure string is composed of a number followed by a unit separated by optional whitespace.
|
9
9
|
# A unit (optional) is composed of a non-digit character followed by zero or more word characters and terminated by some stuff.
|
10
10
|
# Scientific notation is not currently supported.
|
11
|
-
NUMERIC_REGEXP = /((?=\d|\.\d)\d*(?:\.\d*)?)\s*(\D
|
11
|
+
NUMERIC_REGEXP = /((?=\d|\.\d)\d*(?:\.\d*)?)\s*(\D.*?)?\s*(?=\d|$)/
|
12
12
|
|
13
13
|
class << self
|
14
14
|
attr_accessor :dimension, :base, :default
|
@@ -67,6 +67,11 @@ module Dimensional
|
|
67
67
|
configuration[u][:preference] - oom_delta
|
68
68
|
end
|
69
69
|
end
|
70
|
+
|
71
|
+
# Create a new instance with the given value (assumed to be in the base unit) and convert it to the preferred unit.
|
72
|
+
def load(v)
|
73
|
+
new(v, base).preferred
|
74
|
+
end
|
70
75
|
end
|
71
76
|
|
72
77
|
attr_reader :unit
|
data/lib/dimensional/version.rb
CHANGED
data/test/demo.rb
CHANGED
@@ -36,7 +36,7 @@ Configurator.start do
|
|
36
36
|
base('meter', 'm', :detector => /\A(met(er|re)s?|m)\Z/) do
|
37
37
|
derive('decimeter', 'dm', Rational(1, 10), :detector => /\A(decimet(er|re)s?|dm)\Z/, :preference => 0.5)
|
38
38
|
derive('centimeter', 'cm', Rational(1, 100), :detector => /\A(centimet(er|re)s?|cm)\Z/)
|
39
|
-
derive('decameter', '
|
39
|
+
derive('decameter', 'dam', 10, :detector => /\A(de(c|k)amet(er|re)s?|dam)\Z/)
|
40
40
|
derive('kilometer', 'km', 1000, :detector => /\A(kilomet(er|re)s?|km)\Z/)
|
41
41
|
derive('nautical mile', 'M', 1852, :detector => /\A(nautical miles?|NMs?|nmis?)\Z/)
|
42
42
|
end
|
@@ -70,8 +70,8 @@ Configurator.start do
|
|
70
70
|
reference('mile', 'nm', Dimensional::Unit[:L, :SI, 'meter'], 1852.216, :detector => /\A((nautical )?mile|nm|nmi)\Z/) do
|
71
71
|
derive('league', nil, 3, :detector => /\A(leagues?)\Z/)
|
72
72
|
derive('cable', nil, Rational(1,10), :detector => /\A(cables?|cbls?)\Z/) do
|
73
|
-
derive('fathom', 'fm', Rational(1,
|
74
|
-
derive('yard', 'yd', Rational(1,
|
73
|
+
derive('fathom', 'fm', Rational(1,100), :detector => /\A(fathoms?|fms?)\Z/) do
|
74
|
+
derive('yard', 'yd', Rational(1,2), :detector => /\A(yards?|yds?)\Z/) do
|
75
75
|
derive('foot', 'ft', Rational(1,3), :detector => /\A(foot|feet|ft|')\Z/) do
|
76
76
|
derive('inch', 'in', Rational(1,12), :detector => /\A(inch|inches|in|")\Z/)
|
77
77
|
end
|
@@ -137,6 +137,7 @@ Configurator.start do
|
|
137
137
|
derive('township', 'twp', 36, :detector => /\Atownships?\Z/)
|
138
138
|
end
|
139
139
|
end
|
140
|
+
combine('square foot', 'ft2', {Unit[:L, :US, :ft] => 2}, :detector => /ft2/)
|
140
141
|
end
|
141
142
|
end
|
142
143
|
|
@@ -250,7 +251,6 @@ Configurator.start do
|
|
250
251
|
end
|
251
252
|
system(:US) do
|
252
253
|
# Using the definition for mechanical horsepower, http://en.wikipedia.org/wiki/Horsepower
|
253
|
-
# reference('horsepower', 'hp', Dimensional::Unit[:P, :SI, 'watt'], 745.69987158227022, :detector => /\A(horsepower|hp)\Z/)
|
254
254
|
combine('horsepower', 'hp', {Unit[:E, :US, :'ft-lbf'] => 1, Unit[:T, :SI, :m] => -1}, {:reference_factor => 33000})
|
255
255
|
end
|
256
256
|
end
|
@@ -303,13 +303,14 @@ end
|
|
303
303
|
|
304
304
|
class Length < Dimensional::Metric
|
305
305
|
self.dimension = Dimensional::Dimension::L
|
306
|
-
self.
|
306
|
+
self.base = Unit[:L, :SI, :m]
|
307
|
+
self.default = base
|
307
308
|
end
|
308
309
|
|
309
310
|
class Autonomy < Dimensional::Metric
|
310
311
|
self.dimension = Dimension::L
|
311
|
-
self.default = Unit[:L, :BA, :nm]
|
312
312
|
self.base = Unit[:L, :SI, :m]
|
313
|
+
self.default = Unit[:L, :BA, :nm]
|
313
314
|
configure(Unit[:L, :SI, :km], {:preference => 2})
|
314
315
|
configure(Unit[:L, :US, :mi], {:preference => 3.5})
|
315
316
|
configure(Unit[:L, :Imp, :mi], {:preference => 3.5})
|
@@ -317,13 +318,13 @@ end
|
|
317
318
|
|
318
319
|
class EngineDisplacement < Dimensional::Metric
|
319
320
|
self.dimension = Dimensional::Dimension::V
|
320
|
-
self.
|
321
|
-
self.
|
321
|
+
self.base = Unit[:V, :SI, :l]
|
322
|
+
self.default = base
|
322
323
|
configure(Unit[:V, :US, :in3], {:preference => 2})
|
323
324
|
end
|
324
325
|
|
325
326
|
class MechanicalPower < Dimensional::Metric
|
326
327
|
self.dimension = Dimensional::Dimension::P
|
327
|
-
self.
|
328
|
-
self.
|
328
|
+
self.base = Unit[:P, :SI, :W]
|
329
|
+
self.default = base
|
329
330
|
end
|
data/test/metric_test.rb
CHANGED
@@ -15,6 +15,7 @@ class MetricTest < Test::Unit::TestCase
|
|
15
15
|
@meter = Unit.register('meter', System::SI, Dimension::L, {:abbreviation => 'm'})
|
16
16
|
@kilometer = Unit.register('kilometer', System::SI, Dimension::L, {:reference_units => {@meter => 1}, :reference_factor => 1000, :abbreviation => 'km'})
|
17
17
|
@centimeter = Unit.register('centimeter', System::SI, Dimension::L, {:reference_units => {@meter => 1}, :reference_factor => Rational(1,100), :abbreviation => 'cm'})
|
18
|
+
@international_nautical_mile = Unit.register('nautical mile', System::SI, Dimension::L, {:reference_units => {@meter => 1}, :reference_factor => Rational(1852,1), :abbreviation => 'M', :preference => -3})
|
18
19
|
# Length Units - US
|
19
20
|
@yard_us = Unit.register('yard', System::US, Dimension::L, {:reference_units => {@meter => 1}, :reference_factor => 0.9144, :abbreviation => 'yd'})
|
20
21
|
@foot_us = Unit.register('foot', System::US, Dimension::L, {:reference_units => {@yard_us => 1}, :reference_factor => Rational(1,3), :abbreviation => 'ft'})
|
@@ -74,6 +75,18 @@ class MetricTest < Test::Unit::TestCase
|
|
74
75
|
assert_equal @yard_us, m.unit
|
75
76
|
end
|
76
77
|
|
78
|
+
def test_load_metric
|
79
|
+
range = Class.new(Metric)
|
80
|
+
range.instance_eval do
|
81
|
+
self.dimension = Dimension::L
|
82
|
+
self.base = Unit[:L, :SI, :m]
|
83
|
+
configure Unit[:L, :SI, :M], :preference => 0
|
84
|
+
end
|
85
|
+
assert r = range.load(200000)
|
86
|
+
assert_in_delta(108, r, 0.1)
|
87
|
+
assert_same @international_nautical_mile, r.unit
|
88
|
+
end
|
89
|
+
|
77
90
|
def test_find_unit
|
78
91
|
depth = Class.new(Metric)
|
79
92
|
depth.dimension = Dimension::L
|
@@ -81,6 +94,18 @@ class MetricTest < Test::Unit::TestCase
|
|
81
94
|
assert_same @foot_us, depth.find_unit('foot', :US)
|
82
95
|
end
|
83
96
|
|
97
|
+
def test_scanner
|
98
|
+
assert_equal [%w(3 m)], "3m".scan(Metric::NUMERIC_REGEXP)
|
99
|
+
assert_equal [%w(3 m)], "3 m".scan(Metric::NUMERIC_REGEXP)
|
100
|
+
assert_equal [%w(.3 m)], ".3m".scan(Metric::NUMERIC_REGEXP)
|
101
|
+
assert_equal [%w(0.3 m)], "0.3m".scan(Metric::NUMERIC_REGEXP)
|
102
|
+
assert_equal [%w(01.32 m)], "01.32m".scan(Metric::NUMERIC_REGEXP)
|
103
|
+
assert_equal [%w(6 '), %w(4 ")], "6'4\"".scan(Metric::NUMERIC_REGEXP)
|
104
|
+
assert_equal [%w(6 feet), %w(4 inches)], "6 feet 4 inches".scan(Metric::NUMERIC_REGEXP)
|
105
|
+
assert_equal [["1.32", "nautical miles"]], "1.32 nautical miles".scan(Metric::NUMERIC_REGEXP)
|
106
|
+
# assert_equal [%w(.3 m3)], ".3m3".scan(Metric::NUMERIC_REGEXP)
|
107
|
+
end
|
108
|
+
|
84
109
|
def test_parse
|
85
110
|
depth = Class.new(Metric)
|
86
111
|
depth.dimension = Dimension::L
|
@@ -97,6 +122,14 @@ class MetricTest < Test::Unit::TestCase
|
|
97
122
|
assert_equal 15, m
|
98
123
|
end
|
99
124
|
|
125
|
+
def test_parse_with_multiword_unit
|
126
|
+
range = Class.new(Metric)
|
127
|
+
range.dimension = Dimension::L
|
128
|
+
m = range.parse("15 nautical miles", :SI)
|
129
|
+
assert_same @international_nautical_mile, m.unit
|
130
|
+
assert_equal 15, m
|
131
|
+
end
|
132
|
+
|
100
133
|
def test_parse_compound
|
101
134
|
depth = Class.new(Metric)
|
102
135
|
depth.dimension = Dimension::L
|
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: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Hapgood
|
@@ -22,6 +22,7 @@ extensions: []
|
|
22
22
|
extra_rdoc_files:
|
23
23
|
- LICENSE
|
24
24
|
- README
|
25
|
+
- TODO
|
25
26
|
files:
|
26
27
|
- CHANGELOG
|
27
28
|
- LICENSE
|
@@ -42,6 +43,7 @@ files:
|
|
42
43
|
- test/metric_test.rb
|
43
44
|
- test/system_test.rb
|
44
45
|
- test/unit_test.rb
|
46
|
+
- TODO
|
45
47
|
has_rdoc: true
|
46
48
|
homepage: http://cho.hapgoods.com/dimensional
|
47
49
|
licenses: []
|