rails-units 1.3.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.txt +212 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +20 -0
- data/Manifest.txt +19 -0
- data/README.md +161 -0
- data/RakeFile +83 -0
- data/TODO +2 -0
- data/VERSION +1 -0
- data/lib/ruby-units.rb +14 -0
- data/lib/ruby_units.rb +14 -0
- data/lib/ruby_units/array.rb +9 -0
- data/lib/ruby_units/cache.rb +20 -0
- data/lib/ruby_units/date.rb +53 -0
- data/lib/ruby_units/fixnum.rb +20 -0
- data/lib/ruby_units/math.rb +101 -0
- data/lib/ruby_units/numeric.rb +8 -0
- data/lib/ruby_units/object.rb +8 -0
- data/lib/ruby_units/string.rb +122 -0
- data/lib/ruby_units/time.rb +71 -0
- data/lib/ruby_units/unit.rb +1272 -0
- data/lib/ruby_units/unit_definitions.rb +248 -0
- data/lib/ruby_units/version.rb +5 -0
- data/rails-units.gemspec +83 -0
- data/spec/ruby-units/array_spec.rb +14 -0
- data/spec/ruby-units/complex_spec.rb +37 -0
- data/spec/ruby-units/date_spec.rb +38 -0
- data/spec/ruby-units/math_spec.rb +63 -0
- data/spec/ruby-units/numeric_spec.rb +12 -0
- data/spec/ruby-units/object_spec.rb +7 -0
- data/spec/ruby-units/string_spec.rb +61 -0
- data/spec/ruby-units/time_spec.rb +28 -0
- data/spec/ruby-units/unit_spec.rb +888 -0
- data/spec/spec_helper.rb +4 -0
- data/test/test_cache.rb +26 -0
- data/test/test_ruby-units.rb +1011 -0
- metadata +129 -0
@@ -0,0 +1,248 @@
|
|
1
|
+
class Unit < Numeric
|
2
|
+
UNIT_DEFINITIONS = {
|
3
|
+
# prefixes
|
4
|
+
'<googol>' => [%w{googol}, 1e100, :prefix],
|
5
|
+
'<kibi>' => [%w{Ki Kibi kibi}, 2**10, :prefix],
|
6
|
+
'<mebi>' => [%w{Mi Mebi mebi}, 2**20, :prefix],
|
7
|
+
'<gibi>' => [%w{Gi Gibi gibi}, 2**30, :prefix],
|
8
|
+
'<tebi>' => [%w{Ti Tebi tebi}, 2**40, :prefix],
|
9
|
+
'<pebi>' => [%w{Pi Pebi pebi}, 2**50, :prefix],
|
10
|
+
'<exi>' => [%w{Ei Exi exi}, 2**60, :prefix],
|
11
|
+
'<zebi>' => [%w{Zi Zebi zebi}, 2**70, :prefix],
|
12
|
+
'<yebi>' => [%w{Yi Yebi yebi}, 2**80, :prefix],
|
13
|
+
'<yotta>' => [%w{Y Yotta yotta}, 1e24, :prefix],
|
14
|
+
'<zetta>' => [%w{Z Zetta zetta}, 1e21, :prefix],
|
15
|
+
'<exa>' => [%w{E Exa exa}, 1e18, :prefix],
|
16
|
+
'<peta>' => [%w{P Peta peta}, 1e15, :prefix],
|
17
|
+
'<tera>' => [%w{T Tera tera}, 1e12, :prefix],
|
18
|
+
'<giga>' => [%w{G Giga giga}, 1e9, :prefix],
|
19
|
+
'<mega>' => [%w{M Mega mega}, 1e6, :prefix],
|
20
|
+
'<kilo>' => [%w{k kilo}, 1e3, :prefix],
|
21
|
+
'<hecto>' => [%w{h Hecto hecto}, 1e2, :prefix],
|
22
|
+
'<deca>' => [%w{da Deca deca deka}, 1e1, :prefix],
|
23
|
+
'<deci>' => [%w{d Deci deci}, Rational(1,1e1), :prefix],
|
24
|
+
'<centi>' => [%w{c Centi centi}, Rational(1,1e2), :prefix],
|
25
|
+
'<milli>' => [%w{m Milli milli}, Rational(1,1e3), :prefix],
|
26
|
+
'<micro>' => [%w{u Micro micro}, Rational(1,1e6), :prefix],
|
27
|
+
'<nano>' => [%w{n Nano nano}, Rational(1,1e9), :prefix],
|
28
|
+
'<pico>' => [%w{p Pico pico}, Rational(1,1e12), :prefix],
|
29
|
+
'<femto>' => [%w{f Femto femto}, Rational(1,1e15), :prefix],
|
30
|
+
'<atto>' => [%w{a Atto atto}, Rational(1,1e18), :prefix],
|
31
|
+
'<zepto>' => [%w{z Zepto zepto}, Rational(1,1e21), :prefix],
|
32
|
+
'<yocto>' => [%w{y Yocto yocto}, Rational(1,1e24), :prefix],
|
33
|
+
'<1>' => [%w{1},1,:prefix],
|
34
|
+
|
35
|
+
# length units
|
36
|
+
'<meter>' => [%w{m meter meters metre metres}, 1, :length, %w{<meter>} ],
|
37
|
+
'<inch>' => [%w{in inch inches "}, Rational(254,10_000), :length, %w{<meter>}],
|
38
|
+
'<foot>' => [%w{ft foot feet '}, Rational(3048,10_000), :length, %w{<meter>}],
|
39
|
+
'<yard>' => [%w{yd yard yards}, 0.9144, :length, %w{<meter>}],
|
40
|
+
'<mile>' => [%w{mi mile miles}, 1609.344, :length, %w{<meter>}],
|
41
|
+
'<naut-mile>' => [%w{nmi}, 1852, :length, %w{<meter>}],
|
42
|
+
'<league>'=> [%w{league leagues}, 4828, :length, %w{<meter>}],
|
43
|
+
'<furlong>'=> [%w{furlong furlongs}, 201.2, :length, %w{<meter>}],
|
44
|
+
'<rod>' => [%w{rd rod rods}, 5.029, :length, %w{<meter>}],
|
45
|
+
'<mil>' => [%w{mil mils}, 0.0000254, :length, %w{<meter>}],
|
46
|
+
'<angstrom>' =>[%w{ang angstrom angstroms}, Rational(1,1e10), :length, %w{<meter>}],
|
47
|
+
'<fathom>' => [%w{fathom fathoms}, 1.829, :length, %w{<meter>}],
|
48
|
+
'<pica>' => [%w{pica picas}, 0.004217, :length, %w{<meter>}],
|
49
|
+
'<point>' => [%w{pt point points}, 0.0003514, :length, %w{<meter>}],
|
50
|
+
'<redshift>' => [%w{z red-shift}, 1.302773e26, :length, %w{<meter>}],
|
51
|
+
'<AU>' => [%w{AU astronomical-unit}, 149597900000, :length, %w{<meter>}],
|
52
|
+
'<light-second>'=>[%w{ls light-second}, 299792500, :length, %w{<meter>}],
|
53
|
+
'<light-minute>'=>[%w{lmin light-minute}, 17987550000, :length, %w{<meter>}],
|
54
|
+
'<light-year>' => [%w{ly light-year}, 9460528000000000, :length, %w{<meter>}],
|
55
|
+
'<parsec>' => [%w{pc parsec parsecs}, 30856780000000000, :length, %w{<meter>}],
|
56
|
+
|
57
|
+
#mass
|
58
|
+
'<kilogram>' => [%w{kg kilogram kilograms}, 1, :mass, %w{<kilogram>}],
|
59
|
+
'<AMU>' => [%w{u AMU amu}, 6.0221415e26, :mass, %w{<kilogram>}],
|
60
|
+
'<dalton>' => [%w{Da Dalton Daltons dalton daltons}, 6.0221415e26, :mass, %w{<kilogram>}],
|
61
|
+
'<slug>' => [%w{slug slugs}, 14.5939029, :mass, %w{<kilogram>}],
|
62
|
+
'<short-ton>' => [%w{tn ton}, 907.18474, :mass, %w{<kilogram>}],
|
63
|
+
'<metric-ton>'=>[%w{tonne}, 1000, :mass, %w{<kilogram>}],
|
64
|
+
'<carat>' => [%w{ct carat carats}, 0.0002, :mass, %w{<kilogram>}],
|
65
|
+
'<pound>' => [%w{lbs lb pound pounds #}, Rational(8171193714040401,18014398509481984), :mass, %w{<kilogram>}],
|
66
|
+
'<ounce>' => [%w{oz ounce ounces}, Rational(8171193714040401,288230376151711744), :mass, %w{<kilogram>}],
|
67
|
+
'<gram>' => [%w{g gram grams gramme grammes},Rational(1,1e3),:mass, %w{<kilogram>}],
|
68
|
+
|
69
|
+
#area
|
70
|
+
'<hectare>'=>[%w{hectare}, 10000, :area, %w{<meter> <meter>}],
|
71
|
+
'<acre>'=>[%w(acre acres), 4046.85642, :area, %w{<meter> <meter>}],
|
72
|
+
'<sqft>'=>[%w(sqft), 1, :area, %w{<feet> <feet>}],
|
73
|
+
|
74
|
+
#volume
|
75
|
+
'<liter>' => [%w{l L liter liters litre litres}, Rational(1,1e3), :volume, %w{<meter> <meter> <meter>}],
|
76
|
+
'<gallon>'=> [%w{gal gallon gallons}, 0.0037854118, :volume, %w{<meter> <meter> <meter>}],
|
77
|
+
'<quart>'=> [%w{qt quart quarts}, 0.00094635295, :volume, %w{<meter> <meter> <meter>}],
|
78
|
+
'<pint>'=> [%w{pt pint pints}, 0.000473176475, :volume, %w{<meter> <meter> <meter>}],
|
79
|
+
'<cup>'=> [%w{cu cup cups}, 0.000236588238, :volume, %w{<meter> <meter> <meter>}],
|
80
|
+
'<fluid-ounce>'=> [%w{floz fluid-ounce}, 2.95735297e-5, :volume, %w{<meter> <meter> <meter>}],
|
81
|
+
'<tablespoon>'=> [%w{tbs tablespoon tablespoons}, 1.47867648e-5, :volume, %w{<meter> <meter> <meter>}],
|
82
|
+
'<teaspoon>'=> [%w{tsp teaspoon teaspoons}, 4.92892161e-6, :volume, %w{<meter> <meter> <meter>}],
|
83
|
+
|
84
|
+
#speed
|
85
|
+
'<kph>' => [%w{kph}, 0.277777778, :speed, %w{<meter>}, %w{<second>}],
|
86
|
+
'<mph>' => [%w{mph}, 0.44704, :speed, %w{<meter>}, %w{<second>}],
|
87
|
+
'<knot>' => [%w{kt kn kts knot knots}, 0.514444444, :speed, %w{<meter>}, %w{<second>}],
|
88
|
+
'<fps>' => [%w{fps}, 0.3048, :speed, %w{<meter>}, %w{<second>}],
|
89
|
+
|
90
|
+
#acceleration
|
91
|
+
'<gee>' => [%w{gee}, 9.80655, :acceleration, %w{<meter>}, %w{<second> <second>}],
|
92
|
+
|
93
|
+
#temperature_difference
|
94
|
+
'<kelvin>' => [%w{degK kelvin}, 1, :temperature, %w{<kelvin>}],
|
95
|
+
'<celsius>' => [%w{degC celsius celsius centigrade}, 1, :temperature, %w{<kelvin>}],
|
96
|
+
'<fahrenheit>' => [%w{degF fahrenheit}, Rational(1,1.8), :temperature, %w{<kelvin>}],
|
97
|
+
'<rankine>' => [%w{degR rankine}, Rational(1,1.8), :temperature, %w{<kelvin>}],
|
98
|
+
'<temp-K>' => [%w{tempK}, 1, :temperature, %w{<temp-K>}],
|
99
|
+
'<temp-C>' => [%w{tempC}, 1, :temperature, %w{<temp-K>}],
|
100
|
+
'<temp-F>' => [%w{tempF}, Rational(1,1.8), :temperature, %w{<temp-K>}],
|
101
|
+
'<temp-R>' => [%w{tempR}, Rational(1,1.8), :temperature, %w{<temp-K>}],
|
102
|
+
|
103
|
+
#time
|
104
|
+
'<second>'=> [%w{s sec second seconds}, 1, :time, %w{<second>}],
|
105
|
+
'<minute>'=> [%w{min minute minutes}, 60, :time, %w{<second>}],
|
106
|
+
'<hour>'=> [%w{h hr hrs hour hours}, 3600, :time, %w{<second>}],
|
107
|
+
'<day>'=> [%w{d day days}, 3600*24, :time, %w{<second>}],
|
108
|
+
'<week>'=> [%w{wk week weeks}, 7*3600*24, :time, %w{<second>}],
|
109
|
+
'<fortnight>'=> [%w{fortnight fortnights}, 1209600, :time, %W{<second>}],
|
110
|
+
'<year>'=> [%w{y yr year years annum}, 31556926, :time, %w{<second>}],
|
111
|
+
'<decade>'=>[%w{decade decades}, 315569260, :time, %w{<second>}],
|
112
|
+
'<century>'=>[%w{century centuries}, 3155692600, :time, %w{<second>}],
|
113
|
+
|
114
|
+
#pressure
|
115
|
+
'<pascal>' => [%w{Pa pascal Pascal}, 1, :pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
116
|
+
'<bar>' => [%w{bar bars}, 100000, :pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
117
|
+
'<mmHg>' => [%w{mmHg}, 133.322368,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
118
|
+
'<inHg>' => [%w{inHg}, 3386.3881472,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
119
|
+
'<torr>' => [%w{torr}, 133.322368,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
120
|
+
'<bar>' => [%w{bar}, 100000,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
121
|
+
'<atm>' => [%w{atm ATM atmosphere atmospheres}, 101325,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
122
|
+
'<psi>' => [%w{psi}, 6894.76,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
123
|
+
'<cmh2o>' => [%w{cmH2O}, 98.0638,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
124
|
+
'<inh2o>' => [%w{inH2O}, 249.082052,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
125
|
+
|
126
|
+
#viscosity
|
127
|
+
'<poise>' => [%w{P poise}, Rational(1,10), :viscosity, %w{<kilogram>},%w{<meter> <second>} ],
|
128
|
+
'<stokes>' => [%w{St stokes}, Rational(1,1e4), :viscosity, %w{<meter> <meter>}, %w{<second>}],
|
129
|
+
|
130
|
+
#substance
|
131
|
+
'<mole>' => [%w{mol mole}, 1, :substance, %w{<mole>}],
|
132
|
+
|
133
|
+
#concentration
|
134
|
+
'<molar>' => [%w{M molar}, 1000, :concentration, %w{<mole>}, %w{<meter> <meter> <meter>}],
|
135
|
+
'<wtpercent>' => [%w{wt% wtpercent}, 10, :concentration, %w{<kilogram>}, %w{<meter> <meter> <meter>}],
|
136
|
+
|
137
|
+
#activity
|
138
|
+
'<katal>' => [%w{kat katal Katal}, 1, :activity, %w{<mole>}, %w{<second>}],
|
139
|
+
'<unit>' => [%w{U enzUnit}, 16.667e-16, :activity, %w{<mole>}, %w{<second>}],
|
140
|
+
|
141
|
+
#capacitance
|
142
|
+
'<farad>' => [%w{F farad Farad}, 1, :capacitance, %w{<farad>}],
|
143
|
+
|
144
|
+
#charge
|
145
|
+
'<coulomb>' => [%w{C coulomb Coulomb}, 1, :charge, %w{<ampere> <second>}],
|
146
|
+
|
147
|
+
#current
|
148
|
+
'<ampere>' => [%w{A Ampere ampere amp amps}, 1, :current, %w{<ampere>}],
|
149
|
+
|
150
|
+
#conductance
|
151
|
+
'<siemens>' => [%w{S Siemens siemens}, 1, :resistance, %w{<second> <second> <second> <ampere> <ampere>}, %w{<kilogram> <meter> <meter>}],
|
152
|
+
|
153
|
+
#inductance
|
154
|
+
'<henry>' => [%w{H Henry henry}, 1, :inductance, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere> <ampere>}],
|
155
|
+
|
156
|
+
#potential
|
157
|
+
'<volt>' => [%w{V Volt volt volts}, 1, :potential, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <second> <ampere>}],
|
158
|
+
|
159
|
+
#resistance
|
160
|
+
'<ohm>' => [%w{Ohm ohm}, 1, :resistance, %w{<meter> <meter> <kilogram>},%w{<second> <second> <second> <ampere> <ampere>}],
|
161
|
+
|
162
|
+
#magnetism
|
163
|
+
'<weber>' => [%w{Wb weber webers}, 1, :magnetism, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere>}],
|
164
|
+
'<tesla>' => [%w{T tesla teslas}, 1, :magnetism, %w{<kilogram>}, %w{<second> <second> <ampere>}],
|
165
|
+
'<gauss>' => [%w{G gauss}, Rational(1,1e4), :magnetism, %w{<kilogram>}, %w{<second> <second> <ampere>}],
|
166
|
+
'<maxwell>' => [%w{Mx maxwell maxwells}, Rational(1,1e8), :magnetism, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere>}],
|
167
|
+
'<oersted>' => [%w{Oe oersted oersteds}, 250.0/Math::PI, :magnetism, %w{<ampere>}, %w{<meter>}],
|
168
|
+
|
169
|
+
#energy
|
170
|
+
'<joule>' => [%w{J joule Joule joules}, 1, :energy, %w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
171
|
+
'<erg>' => [%w{erg ergs}, Rational(1,1e7), :energy, %w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
172
|
+
'<btu>' => [%w{BTU btu BTUs}, 1055.056, :energy, %w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
173
|
+
'<calorie>' => [%w{cal calorie calories}, 4.18400, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
174
|
+
'<Calorie>' => [%w{Cal Calorie Calories}, 4184.00, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
175
|
+
'<therm-US>' => [%w{th therm therms Therm}, 105480400, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
176
|
+
|
177
|
+
#force
|
178
|
+
'<newton>' => [%w{N Newton newton}, 1, :force, %w{<kilogram> <meter>}, %w{<second> <second>}],
|
179
|
+
'<dyne>' => [%w{dyn dyne}, Rational(1,1e5), :force, %w{<kilogram> <meter>}, %w{<second> <second>}],
|
180
|
+
'<pound-force>' => [%w{lbf pound-force}, 4.448222, :force, %w{<kilogram> <meter>}, %w{<second> <second>}],
|
181
|
+
|
182
|
+
#frequency
|
183
|
+
'<hertz>' => [%w{Hz hertz Hertz}, 1, :frequency, %w{<1>}, %{<second>}],
|
184
|
+
|
185
|
+
#angle
|
186
|
+
'<radian>' =>[%w{rad radian radian radians}, 1, :angle, %w{<radian>}],
|
187
|
+
'<degree>' =>[%w{deg degree degrees}, Math::PI / 180.0, :angle, %w{<radian>}],
|
188
|
+
'<grad>' =>[%w{grad gradian grads}, Math::PI / 200.0, :angle, %w{<radian>}],
|
189
|
+
'<steradian>' => [%w{sr steradian steradians}, 1, :solid_angle, %w{<steradian>}],
|
190
|
+
|
191
|
+
#rotation
|
192
|
+
'<rotation>' => [%w{rotation}, 2.0*Math::PI, :angle, %w{<radian>}],
|
193
|
+
'<rpm>' =>[%w{rpm}, 2.0*Math::PI / 60.0, :angular_velocity, %w{<radian>}, %w{<second>}],
|
194
|
+
|
195
|
+
#memory
|
196
|
+
'<byte>' =>[%w{B byte}, 1, :memory, %w{<byte>}],
|
197
|
+
'<bit>' =>[%w{b bit}, 0.125, :memory, %w{<byte>}],
|
198
|
+
|
199
|
+
#currency
|
200
|
+
'<dollar>'=>[%w{USD dollar}, 1, :currency, %w{<dollar>}],
|
201
|
+
'<cents>' =>[%w{cents}, Rational(1,100), :currency, %w{<dollar>}],
|
202
|
+
|
203
|
+
#luminosity
|
204
|
+
'<candela>' => [%w{cd candela}, 1, :luminosity, %w{<candela>}],
|
205
|
+
'<lumen>' => [%w{lm lumen}, 1, :luminous_power, %w{<candela> <steradian>}],
|
206
|
+
'<lux>' =>[%w{lux}, 1, :illuminance, %w{<candela> <steradian>}, %w{<meter> <meter>}],
|
207
|
+
|
208
|
+
#power
|
209
|
+
'<watt>' => [%w{W watt watts}, 1, :power, %w{<kilogram> <meter> <meter>}, %w{<second> <second> <second>}],
|
210
|
+
'<horsepower>' => [%w{hp horsepower}, 745.699872, :power, %w{<kilogram> <meter> <meter>}, %w{<second> <second> <second>}],
|
211
|
+
|
212
|
+
#radiation
|
213
|
+
'<gray>' => [%w{Gy gray grays}, 1, :radiation, %w{<meter> <meter>}, %w{<second> <second>}],
|
214
|
+
'<roentgen>' => [%w{R roentgen}, 0.009330, :radiation, %w{<meter> <meter>}, %w{<second> <second>}],
|
215
|
+
'<sievert>' => [%w{Sv sievert sieverts}, 1, :radiation, %w{<meter> <meter>}, %w{<second> <second>}],
|
216
|
+
'<becquerel>' => [%w{Bq bequerel bequerels}, 1, :radiation, %w{<1>},%w{<second>}],
|
217
|
+
'<curie>' => [%w{Ci curie curies}, 3.7e10, :radiation, %w{<1>},%w{<second>}],
|
218
|
+
|
219
|
+
# rate
|
220
|
+
'<cpm>' => [%w{cpm}, Rational(1,60), :rate, %w{<count>},%w{<second>}],
|
221
|
+
'<dpm>' => [%w{dpm}, Rational(1,60), :rate, %w{<count>},%w{<second>}],
|
222
|
+
'<bpm>' => [%w{bpm}, Rational(1,60), :rate, %w{<count>},%w{<second>}],
|
223
|
+
|
224
|
+
#resolution / typography
|
225
|
+
'<dot>' => [%w{dot dots}, 1, :resolution, %w{<each>}],
|
226
|
+
'<pixel>' => [%w{pixel px}, 1, :resolution, %w{<each>}],
|
227
|
+
'<ppi>' => [%w{ppi}, 1, :resolution, %w{<pixel>}, %w{<inch>}],
|
228
|
+
'<dpi>' => [%w{dpi}, 1, :typography, %w{<dot>}, %w{<inch>}],
|
229
|
+
'<pica>' => [%w{pica}, 0.00423333333 , :typography, %w{<meter>}],
|
230
|
+
'<point>' => [%w{point pt}, 0.000352777778, :typography, %w{<meter>}],
|
231
|
+
|
232
|
+
#other
|
233
|
+
'<cell>' => [%w{cells cell}, 1, :counting, %w{<each>}],
|
234
|
+
'<each>' => [%w{each}, 1, :counting, %w{<each>}],
|
235
|
+
'<count>' => [%w{count}, 1, :counting, %w{<each>}],
|
236
|
+
'<base-pair>' => [%w{bp}, 1, :counting, %w{<each>}],
|
237
|
+
'<nucleotide>' => [%w{nt}, 1, :counting, %w{<each>}],
|
238
|
+
'<molecule>' => [%w{molecule molecules}, 1, :counting, %w{<1>}],
|
239
|
+
'<dozen>' => [%w{doz dz dozen},12,:prefix_only, %w{<each>}],
|
240
|
+
'<percent>'=> [%w{% percent}, Rational(1,100), :prefix_only, %w{<1>}],
|
241
|
+
'<ppm>' => [%w{ppm},Rational(1,1e6),:prefix_only, %w{<1>}],
|
242
|
+
'<ppt>' => [%w{ppt},Rational(1,1e9),:prefix_only, %w{<1>}],
|
243
|
+
'<gross>' => [%w{gr gross},144, :prefix_only, %w{<dozen> <dozen>}],
|
244
|
+
'<decibel>' => [%w{dB decibel decibels}, 1, :logarithmic, %w{<decibel>}]
|
245
|
+
|
246
|
+
|
247
|
+
} # doc
|
248
|
+
end
|
data/rails-units.gemspec
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in RakeFile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rails-units}
|
8
|
+
s.version = "1.3.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Kevin Olbrich, Ph.D.", "Stephen Pike"]
|
12
|
+
s.date = %q{2011-06-23}
|
13
|
+
s.description = %q{Provides classes and methods to perform unit math and conversions. Based on ruby-units but rails-compatible}
|
14
|
+
s.email = ["spike@scpike.net"]
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md",
|
18
|
+
"TODO"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
"CHANGELOG.txt",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"Manifest.txt",
|
25
|
+
"README.md",
|
26
|
+
"RakeFile",
|
27
|
+
"TODO",
|
28
|
+
"VERSION",
|
29
|
+
"lib/ruby-units.rb",
|
30
|
+
"lib/ruby_units.rb",
|
31
|
+
"lib/ruby_units/array.rb",
|
32
|
+
"lib/ruby_units/cache.rb",
|
33
|
+
"lib/ruby_units/date.rb",
|
34
|
+
"lib/ruby_units/fixnum.rb",
|
35
|
+
"lib/ruby_units/math.rb",
|
36
|
+
"lib/ruby_units/numeric.rb",
|
37
|
+
"lib/ruby_units/object.rb",
|
38
|
+
"lib/ruby_units/string.rb",
|
39
|
+
"lib/ruby_units/time.rb",
|
40
|
+
"lib/ruby_units/unit.rb",
|
41
|
+
"lib/ruby_units/unit_definitions.rb",
|
42
|
+
"lib/ruby_units/version.rb",
|
43
|
+
"rails-units.gemspec",
|
44
|
+
"spec/ruby-units/array_spec.rb",
|
45
|
+
"spec/ruby-units/complex_spec.rb",
|
46
|
+
"spec/ruby-units/date_spec.rb",
|
47
|
+
"spec/ruby-units/math_spec.rb",
|
48
|
+
"spec/ruby-units/numeric_spec.rb",
|
49
|
+
"spec/ruby-units/object_spec.rb",
|
50
|
+
"spec/ruby-units/string_spec.rb",
|
51
|
+
"spec/ruby-units/time_spec.rb",
|
52
|
+
"spec/ruby-units/unit_spec.rb",
|
53
|
+
"spec/spec_helper.rb",
|
54
|
+
"test/test_cache.rb",
|
55
|
+
"test/test_ruby-units.rb"
|
56
|
+
]
|
57
|
+
s.homepage = %q{https://github.com/scpike/rails-units}
|
58
|
+
s.require_paths = ["lib"]
|
59
|
+
s.rubygems_version = %q{1.7.2}
|
60
|
+
s.summary = %q{A class that performs unit conversions and unit math}
|
61
|
+
|
62
|
+
if s.respond_to? :specification_version then
|
63
|
+
s.specification_version = 3
|
64
|
+
|
65
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
66
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
67
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
68
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
69
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.5"])
|
70
|
+
else
|
71
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
72
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
73
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
74
|
+
s.add_dependency(%q<rspec>, ["~> 2.5"])
|
75
|
+
end
|
76
|
+
else
|
77
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
78
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
79
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
80
|
+
s.add_dependency(%q<rspec>, ["~> 2.5"])
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
|
5
|
+
subject { [1, 'cm'] }
|
6
|
+
|
7
|
+
it {should be_kind_of Array}
|
8
|
+
it {should respond_to :to_unit}
|
9
|
+
|
10
|
+
specify { subject.to_unit.should be_instance_of Unit}
|
11
|
+
specify { subject.to_unit.should == "1 cm".to_unit }
|
12
|
+
specify { subject.to_unit('mm').should == "10 mm".to_unit}
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
# Complex numbers are a bit strange
|
4
|
+
# Technically you can't really compare them using <=>, and ruby 1.9 does not implement this method for them
|
5
|
+
# so it stands to reason that complex units should also not support :> or :<
|
6
|
+
|
7
|
+
describe Complex do
|
8
|
+
subject { Complex(1,1) }
|
9
|
+
it { should respond_to :to_unit }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "Complex Unit" do
|
13
|
+
subject { Complex(1.0, -1.0).to_unit }
|
14
|
+
|
15
|
+
it { should be_instance_of Unit}
|
16
|
+
it(:scalar) { should be_kind_of Complex }
|
17
|
+
|
18
|
+
it { should == "1-1i".to_unit }
|
19
|
+
it { should === "1-1i".to_unit }
|
20
|
+
|
21
|
+
if RUBY_VERSION < "1.9"
|
22
|
+
context "in Ruby < 1.9" do
|
23
|
+
it "is comparable" do
|
24
|
+
subject.should > "1+0.5i".to_unit
|
25
|
+
subject.should < "2+1i".to_unit
|
26
|
+
end
|
27
|
+
end
|
28
|
+
else
|
29
|
+
context "in Ruby >= 1.9" do
|
30
|
+
it "is not comparable" do
|
31
|
+
expect { subject > "1+1i".to_unit }.to raise_error(NoMethodError)
|
32
|
+
expect { subject < "1+1i".to_unit }.to raise_error(NoMethodError)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Date do
|
4
|
+
subject { Date.new(2011,4,1) }
|
5
|
+
|
6
|
+
it {should be_instance_of Date}
|
7
|
+
it {should respond_to :to_unit}
|
8
|
+
it {should respond_to :to_time}
|
9
|
+
it {should respond_to :to_date}
|
10
|
+
|
11
|
+
specify { (subject + "5 days".unit).should == Date.new(2011,4,6) }
|
12
|
+
specify { (subject - "5 days".unit).should == Date.new(2011,3,27) }
|
13
|
+
# 2012 is a leap year...
|
14
|
+
specify { (subject + "1 year".unit).should == Date.new(2012,3,31) }
|
15
|
+
specify { (subject - "1 year".unit).should == Date.new(2010,4,1) }
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "Date Unit" do
|
19
|
+
|
20
|
+
subject { Date.new(2011,4,1).to_unit }
|
21
|
+
|
22
|
+
it { should be_instance_of Unit }
|
23
|
+
its(:scalar) { should be_kind_of Rational }
|
24
|
+
its(:units) { should == "d" }
|
25
|
+
its(:kind) { should == :time }
|
26
|
+
|
27
|
+
specify { (subject + "5 days".unit).should == Date.new(2011,4,6) }
|
28
|
+
specify { (subject - "5 days".unit).should == Date.new(2011,3,27) }
|
29
|
+
|
30
|
+
specify { expect { subject + Date.new(2011,4,1) }.to raise_error(ArgumentError) }
|
31
|
+
specify { expect { subject + DateTime.new(2011,4,1,12,00,00) }.to raise_error(ArgumentError) }
|
32
|
+
specify { expect { subject + Time.parse("2011-04-01 12:00:00") }.to raise_error(ArgumentError) }
|
33
|
+
|
34
|
+
specify { (subject - Date.new(2011,4,1)).should be_zero }
|
35
|
+
specify { (subject - DateTime.new(2011,4,1,00,00,00)).should be_zero }
|
36
|
+
specify { expect {(subject - Time.parse("2011-04-01 00:00"))}.to raise_error(ArgumentError) }
|
37
|
+
specify { (Date.new(2011,4,1) + 1).should == Date.new(2011,4,2)}
|
38
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Math do
|
4
|
+
context "with '1 mm^6'" do
|
5
|
+
subject { '1 mm^6'.to_unit }
|
6
|
+
|
7
|
+
specify { Math.sqrt(subject).should == '1 mm^3'.to_unit }
|
8
|
+
specify { Math.sqrt(4).should == 2 }
|
9
|
+
|
10
|
+
if RUBY_VERSION > "1.9"
|
11
|
+
# cbrt is only defined in Ruby > 1.9
|
12
|
+
specify { Math.cbrt(subject).should == '1 mm^2'.to_unit }
|
13
|
+
specify { Math.cbrt(8).should == 2 }
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
context "Trigonometry functions" do
|
19
|
+
|
20
|
+
context "with '45 deg' unit" do
|
21
|
+
subject { "45 deg".unit }
|
22
|
+
specify { Math.sin(subject).should be_within(0.01).of(0.70710678) }
|
23
|
+
specify { Math.cos(subject).should be_within(0.01).of(0.70710678) }
|
24
|
+
specify { Math.tan(subject).should be_within(0.01).of(1) }
|
25
|
+
specify { Math.sinh(subject).should be_within(0.01).of(0.8686709614860095) }
|
26
|
+
specify { Math.cosh(subject).should be_within(0.01).of(1.3246090892520057) }
|
27
|
+
specify { Math.tanh(subject).should be_within(0.01).of(0.6557942026326724) }
|
28
|
+
end
|
29
|
+
|
30
|
+
context "with 'PI/4 radians' unit" do
|
31
|
+
subject { (Math::PI/4).unit('radians') }
|
32
|
+
specify { Math.sin(subject).should be_within(0.01).of(0.70710678) }
|
33
|
+
specify { Math.cos(subject).should be_within(0.01).of(0.70710678) }
|
34
|
+
specify { Math.tan(subject).should be_within(0.01).of(1) }
|
35
|
+
specify { Math.sinh(subject).should be_within(0.01).of(0.8686709614860095) }
|
36
|
+
specify { Math.cosh(subject).should be_within(0.01).of(1.3246090892520057) }
|
37
|
+
specify { Math.tanh(subject).should be_within(0.01).of(0.6557942026326724) }
|
38
|
+
end
|
39
|
+
|
40
|
+
context "with 'PI/4' continues to work" do
|
41
|
+
subject { (Math::PI/4) }
|
42
|
+
specify { Math.sin(subject).should be_within(0.01).of(0.70710678) }
|
43
|
+
specify { Math.cos(subject).should be_within(0.01).of(0.70710678) }
|
44
|
+
specify { Math.tan(subject).should be_within(0.01).of(1) }
|
45
|
+
specify { Math.sinh(subject).should be_within(0.01).of(0.8686709614860095) }
|
46
|
+
specify { Math.cosh(subject).should be_within(0.01).of(1.3246090892520057) }
|
47
|
+
specify { Math.tanh(subject).should be_within(0.01).of(0.6557942026326724) }
|
48
|
+
end
|
49
|
+
|
50
|
+
specify { Math.hypot("1 m".unit, "2 m".unit).should be_within("0.01 m".unit).of("2.23607 m".unit) }
|
51
|
+
specify { Math.hypot("1 m".unit, "2 ft".unit).should be_within("0.01 m".unit).of("1.17116 m".unit) }
|
52
|
+
specify { expect {Math.hypot("1 m".unit, "2 lbs".unit) }.to raise_error(ArgumentError) }
|
53
|
+
|
54
|
+
specify { Math.atan2("1 m".unit, "2 m".unit).should be_within(0.01).of(0.4636476090008061) }
|
55
|
+
specify { Math.atan2("1 m".unit, "2 ft".unit).should be_within(0.01).of(1.0233478888629426) }
|
56
|
+
specify { Math.atan2(1,1).should be_within(0.01).of(0.785398163397448)}
|
57
|
+
specify { expect {Math.atan2("1 m".unit, "2 lbs".unit)}.to raise_error(ArgumentError) }
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
end
|