stick 1.3.1 → 1.3.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/CHANGES +7 -1
- data/lib/stick/constants/cgs.rb +3 -3
- data/lib/stick/constants/mks.rb +3 -4
- data/lib/stick/constants/number.rb +6 -4
- data/lib/stick/constants/typeless_cgs.rb +5 -3
- data/lib/stick/constants/typeless_mks.rb +5 -3
- data/lib/stick/currency.rb +2 -2
- data/lib/stick/units.rb +0 -2
- data/lib/stick/units/base.rb +83 -83
- data/lib/stick/units/currency.rb +12 -12
- data/lib/stick/units/loaders.rb +2 -3
- data/lib/stick/units/units.rb +0 -2
- data/meta/stick.roll +1 -1
- data/test/test_currency.rb +1 -1
- data/test/test_units.rb +1 -1
- metadata +1 -1
data/CHANGES
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
= Stick Change History
|
2
2
|
|
3
|
-
== 1.3.
|
3
|
+
== 1.3.2 / 2007-12-21
|
4
|
+
|
5
|
+
* Removed top-level Stick namespace. Stick isn't big enough to warrent it at this point.
|
6
|
+
* This means Units and Constants are at the toplevel instead.
|
7
|
+
* This can be reconsidered later if Stick grows large enough to necessary.
|
8
|
+
|
9
|
+
== 1.3.0 / 2007-12-20
|
4
10
|
|
5
11
|
* Removed times.rb. WE can reconsider this type of functionaility later.
|
6
12
|
* Decided to distribute under the MIT License.
|
data/lib/stick/constants/cgs.rb
CHANGED
@@ -16,8 +16,7 @@
|
|
16
16
|
|
17
17
|
require 'stick/units'
|
18
18
|
|
19
|
-
module
|
20
|
-
module Constants
|
19
|
+
module Constants
|
21
20
|
|
22
21
|
# = Constants in the CGS system (cm, g, s)
|
23
22
|
#
|
@@ -131,5 +130,6 @@ module Stick
|
|
131
130
|
JOULE = 1e7.g*cm**2/s**2 # g cm^2 / s^2
|
132
131
|
ERG = 1e0.g*cm**2/s**2 # g cm^2 / s^2
|
133
132
|
end
|
134
|
-
|
133
|
+
|
135
134
|
end
|
135
|
+
|
data/lib/stick/constants/mks.rb
CHANGED
@@ -15,8 +15,7 @@
|
|
15
15
|
# - Brian Gough
|
16
16
|
|
17
17
|
#
|
18
|
-
module
|
19
|
-
module Constants
|
18
|
+
module Constants
|
20
19
|
|
21
20
|
# = Constants in the MKS system (meters, kg, sec)
|
22
21
|
#
|
@@ -133,10 +132,10 @@ module Stick
|
|
133
132
|
JOULE = 1e0.kg*m**2/s**2 # kg m^2 / s^2
|
134
133
|
ERG = 1e-7.kg*m**2/s**2 # kg m^2 / s^2
|
135
134
|
end
|
136
|
-
|
135
|
+
|
137
136
|
end
|
138
137
|
|
139
138
|
|
140
139
|
module Math
|
141
|
-
include
|
140
|
+
include ::Constants::MKS
|
142
141
|
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
module
|
2
|
-
|
1
|
+
module Constants
|
2
|
+
|
3
3
|
# = Unit-less constants
|
4
4
|
#
|
5
5
|
# Stick::Contants::Number::FINE_STRUCTURE #=> 7.297352533e-3
|
@@ -24,10 +24,12 @@ module Stick
|
|
24
24
|
ZEPTO = 1e-21 # 1
|
25
25
|
YOCTO = 1e-24 # 1
|
26
26
|
end
|
27
|
+
|
27
28
|
include Number
|
28
|
-
|
29
|
+
|
29
30
|
end
|
30
31
|
|
32
|
+
|
31
33
|
module Math
|
32
|
-
include
|
34
|
+
include ::Contants::Number
|
33
35
|
end
|
@@ -15,9 +15,10 @@
|
|
15
15
|
# - Thomas Sawyer
|
16
16
|
|
17
17
|
#
|
18
|
-
module
|
19
|
-
|
18
|
+
module Constants
|
19
|
+
|
20
20
|
module Typeless
|
21
|
+
|
21
22
|
# Unitless Constants in the CGS system (cm, kg, s)
|
22
23
|
|
23
24
|
module CGS
|
@@ -120,6 +121,7 @@ module Stick
|
|
120
121
|
JOULE = 1e7 # g cm^2 / s^2
|
121
122
|
ERG = 1e0 # g cm^2 / s^2
|
122
123
|
end
|
124
|
+
|
123
125
|
end
|
124
|
-
|
126
|
+
|
125
127
|
end
|
@@ -15,9 +15,10 @@
|
|
15
15
|
# - Brian Gough
|
16
16
|
|
17
17
|
#
|
18
|
-
module
|
19
|
-
|
18
|
+
module Constants
|
19
|
+
|
20
20
|
module Typeless
|
21
|
+
|
21
22
|
# Unitless constants in the MKS system (meters, kg, sec)
|
22
23
|
|
23
24
|
module MKS
|
@@ -121,6 +122,7 @@ module Stick
|
|
121
122
|
JOULE = 1e0 # kg m^2 / s^2
|
122
123
|
ERG = 1e-7 # kg m^2 / s^2
|
123
124
|
end
|
125
|
+
|
124
126
|
end
|
125
|
-
|
127
|
+
|
126
128
|
end
|
data/lib/stick/currency.rb
CHANGED
data/lib/stick/units.rb
CHANGED
data/lib/stick/units/base.rb
CHANGED
@@ -69,16 +69,15 @@ end
|
|
69
69
|
# http://en.wikipedia.org/wiki/Conversion_of_units
|
70
70
|
#++
|
71
71
|
|
72
|
-
module Stick
|
73
72
|
module Units
|
74
73
|
|
75
74
|
def method_missing(m, *args, &blk)
|
76
75
|
if args.length == 1
|
77
|
-
args[0] = (::
|
78
|
-
return ::
|
79
|
-
elsif (::
|
76
|
+
args[0] = (::Units::Converter.converter(args[0]) rescue nil) if not args[0].is_a? ::Units::Converter
|
77
|
+
return ::Units::Unit.new({m => 1}, args[0]) if args[0] && args[0].registered?(m)
|
78
|
+
elsif (::Units::Converter.current.registered?(m) rescue false)
|
80
79
|
raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
|
81
|
-
return ::
|
80
|
+
return ::Units::Unit.new({m => 1}, ::Units::Converter.current)
|
82
81
|
end
|
83
82
|
::Exception.with_clean_backtrace("method_missing") {
|
84
83
|
super
|
@@ -86,8 +85,8 @@ module Units
|
|
86
85
|
end
|
87
86
|
|
88
87
|
def const_missing(c)
|
89
|
-
if (::
|
90
|
-
return ::
|
88
|
+
if (::Units::Converter.current.registered?(c) rescue false)
|
89
|
+
return ::Units::Unit.new({c => 1}, ::Units::Converter.current)
|
91
90
|
else
|
92
91
|
::Exception.with_clean_backtrace("const_missing") {
|
93
92
|
super
|
@@ -96,7 +95,7 @@ module Units
|
|
96
95
|
end
|
97
96
|
|
98
97
|
def self.append_features(m)
|
99
|
-
m.send(:extend, ::
|
98
|
+
m.send(:extend, ::Units)
|
100
99
|
super
|
101
100
|
end
|
102
101
|
|
@@ -117,7 +116,7 @@ module Units
|
|
117
116
|
#
|
118
117
|
# See also Converter.current.
|
119
118
|
def with_unit_converter(name, &blk) # :yields:
|
120
|
-
::
|
119
|
+
::Units::Converter.with_converter(name, &blk)
|
121
120
|
end
|
122
121
|
|
123
122
|
module_function :with_unit_converter
|
@@ -138,7 +137,7 @@ module Units
|
|
138
137
|
|
139
138
|
attr_reader :name, :converter
|
140
139
|
|
141
|
-
def initialize(name, converter = ::
|
140
|
+
def initialize(name, converter = ::Units::Converter.current)
|
142
141
|
name = name.to_sym
|
143
142
|
raise ::ArgumentError, "unit #{name.to_s.dump} not registered with #{converter}" if not converter.registered? name
|
144
143
|
@name = name
|
@@ -150,7 +149,7 @@ module Units
|
|
150
149
|
end
|
151
150
|
|
152
151
|
def ==(other)
|
153
|
-
other.is_a?(::
|
152
|
+
other.is_a?(::Units::BaseUnit) && other.name == @name && other.converter == @converter
|
154
153
|
end
|
155
154
|
|
156
155
|
alias eql? ==
|
@@ -160,7 +159,7 @@ module Units
|
|
160
159
|
end
|
161
160
|
|
162
161
|
def to_s
|
163
|
-
if ::
|
162
|
+
if ::Units::Converter.current.includes?(converter)
|
164
163
|
@name.to_s
|
165
164
|
else
|
166
165
|
"#{@converter}:#{@name}"
|
@@ -172,13 +171,13 @@ module Units
|
|
172
171
|
end
|
173
172
|
|
174
173
|
# This class represents a Unit. A Unit uses a given Converter with
|
175
|
-
# a number of registered
|
176
|
-
# A Unit is the product of the powers of other
|
174
|
+
# a number of registered units in which it can be expressed.
|
175
|
+
# A Unit is the product of the powers of other units. In principle, these
|
177
176
|
# need not be integer powers, but this may cause problems with rounding.
|
178
177
|
# The following code for example returns +false+:
|
179
178
|
# Unit.new(:m => 0.1) * Unit.new(:m => 0.2) == Unit.new(:m => 0.3)
|
180
179
|
#
|
181
|
-
#
|
180
|
+
# Units can be multiplied, divided, and raised to a given power.
|
182
181
|
# As an extra, 1 can be divided by a Unit.
|
183
182
|
#
|
184
183
|
# Examples:
|
@@ -203,13 +202,13 @@ module Units
|
|
203
202
|
#
|
204
203
|
# See also Converter, Converter.converter
|
205
204
|
def initialize(units = {}, converter = nil)
|
206
|
-
conv = proc { converter ||= ::
|
205
|
+
conv = proc { converter ||= ::Units::Converter.current }
|
207
206
|
if units.is_a? ::String
|
208
207
|
@units = decode_string(units, conv)
|
209
208
|
else
|
210
209
|
@units = {}
|
211
210
|
units.each_pair do |k, v|
|
212
|
-
k = conv[].base_unit(k.to_sym) if not k.is_a? ::
|
211
|
+
k = conv[].base_unit(k.to_sym) if not k.is_a? ::Units::BaseUnit
|
213
212
|
@units[k] = v
|
214
213
|
end
|
215
214
|
end
|
@@ -222,7 +221,7 @@ module Units
|
|
222
221
|
@units.each_pair do |u, e|
|
223
222
|
result[u] = e * p
|
224
223
|
end
|
225
|
-
::
|
224
|
+
::Units::Unit.new(result)
|
226
225
|
end
|
227
226
|
|
228
227
|
# Multiplies with the given Unit.
|
@@ -241,18 +240,18 @@ module Units
|
|
241
240
|
end
|
242
241
|
|
243
242
|
def coerce(other) # :nodoc:
|
244
|
-
return [::
|
245
|
-
::
|
243
|
+
return [::Units::Unit.new({}), self] if other == 1
|
244
|
+
::Units::Converter.coerce_units(self, other)
|
246
245
|
end
|
247
246
|
|
248
247
|
def simplify
|
249
|
-
::
|
248
|
+
::Units::Converter.simplify_unit(self)
|
250
249
|
end
|
251
250
|
|
252
251
|
# Returns +true+ iff the two units are equals, <i>i.e.,</i> iff they have
|
253
252
|
# the same exponent for all units, and they both use the same Converter.
|
254
253
|
def ==(other)
|
255
|
-
other.is_a?(::
|
254
|
+
other.is_a?(::Units::Unit) && other.units == units
|
256
255
|
end
|
257
256
|
|
258
257
|
alias eql? ==
|
@@ -265,7 +264,7 @@ module Units
|
|
265
264
|
# Unit. This is less strict than equality because for example hours are
|
266
265
|
# compatible with seconds ("we can add them"), but hours are not seconds.
|
267
266
|
def compatible_with?(other)
|
268
|
-
conv1, conv2 = ::
|
267
|
+
conv1, conv2 = ::Units::Converter.coerce_units(self, other)
|
269
268
|
conv1.units == conv2.units
|
270
269
|
end
|
271
270
|
|
@@ -289,11 +288,11 @@ module Units
|
|
289
288
|
|
290
289
|
def method_missing(m, *args, &blk)
|
291
290
|
if args.length == 1
|
292
|
-
args[0] = (::
|
293
|
-
return self * ::
|
294
|
-
elsif (::
|
291
|
+
args[0] = (::Units::Converter.converter(args[0]) rescue nil) if not args[0].is_a? ::Units::Converter
|
292
|
+
return self * ::Units::Unit.new({m => 1}, args[0]) if args[0] && args[0].registered?(m)
|
293
|
+
elsif (::Units::Converter.current.registered?(m) rescue false)
|
295
294
|
raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
|
296
|
-
return self * ::
|
295
|
+
return self * ::Units::Unit.new({m => 1}, ::Units::Converter.current)
|
297
296
|
end
|
298
297
|
::Exception.with_clean_backtrace("method_missing") {
|
299
298
|
super
|
@@ -303,7 +302,7 @@ module Units
|
|
303
302
|
private
|
304
303
|
|
305
304
|
def decode_string(s, converter)
|
306
|
-
if ::
|
305
|
+
if ::Units::Regexps::TOTAL_UNIT_REGEXP =~ s
|
307
306
|
numerator, denominator = $1, $2
|
308
307
|
units = {}
|
309
308
|
decode_multiplicative_string(numerator, 1, converter, units) if numerator
|
@@ -317,9 +316,9 @@ module Units
|
|
317
316
|
end
|
318
317
|
|
319
318
|
def decode_multiplicative_string(s, multiplier, converter, result)
|
320
|
-
s.scan(::
|
319
|
+
s.scan(::Units::Regexps::SINGLE_UNIT_REGEXP) do |conv, unit, exp|
|
321
320
|
if unit
|
322
|
-
conv = ::
|
321
|
+
conv = ::Units::Converter.converter(conv)
|
323
322
|
else
|
324
323
|
conv, unit = converter[], conv
|
325
324
|
end
|
@@ -337,13 +336,13 @@ module Units
|
|
337
336
|
|
338
337
|
def do_op(op, dual_op, other)
|
339
338
|
other = other.to_unit
|
340
|
-
raise TypeError, "cannot convert to Unit" unless ::
|
339
|
+
raise TypeError, "cannot convert to Unit" unless ::Units::Unit === other
|
341
340
|
result = @units.dup
|
342
341
|
other.units.each_pair do |u, e|
|
343
342
|
result[u] = 0 if not result[u]
|
344
343
|
result[u] = result[u].send(dual_op, e)
|
345
344
|
end
|
346
|
-
::
|
345
|
+
::Units::Unit.new(result)
|
347
346
|
end
|
348
347
|
|
349
348
|
end
|
@@ -414,7 +413,7 @@ module Units
|
|
414
413
|
%w{ * / }.each do |op|
|
415
414
|
eval %{
|
416
415
|
def #{op}(other)
|
417
|
-
::
|
416
|
+
::Units::Value.new(*do_multiplicative_op(:#{op}, other))
|
418
417
|
end
|
419
418
|
}
|
420
419
|
end
|
@@ -422,14 +421,14 @@ module Units
|
|
422
421
|
%w{ + - modulo remainder % }.each do |op|
|
423
422
|
eval %{
|
424
423
|
def #{op}(other)
|
425
|
-
::
|
424
|
+
::Units::Value.new(*do_additive_op(:#{op}, other))
|
426
425
|
end
|
427
426
|
}
|
428
427
|
end
|
429
428
|
|
430
429
|
def divmod(other)
|
431
430
|
(q, r), unit = *do_additive_op(:divmod, other)
|
432
|
-
[q, ::
|
431
|
+
[q, ::Units::Value.new(r, unit)]
|
433
432
|
end
|
434
433
|
|
435
434
|
def div(other)
|
@@ -445,7 +444,7 @@ module Units
|
|
445
444
|
end
|
446
445
|
|
447
446
|
def **(other) # :nodoc:
|
448
|
-
::
|
447
|
+
::Units::Value.new(@value ** other, @unit ** other)
|
449
448
|
end
|
450
449
|
|
451
450
|
def <=>(other) # :nodoc:
|
@@ -467,7 +466,7 @@ module Units
|
|
467
466
|
%w{ abs ceil floor next round succ truncate }.each do |op|
|
468
467
|
eval %{
|
469
468
|
def #{op}
|
470
|
-
::
|
469
|
+
::Units::Value.new(@value.#{op}, @unit)
|
471
470
|
end
|
472
471
|
}
|
473
472
|
end
|
@@ -487,16 +486,16 @@ module Units
|
|
487
486
|
def to(to_unit, converter = nil)
|
488
487
|
raise ArgumentError, "Wrong number of arguments" if converter && !(::String === to_unit)
|
489
488
|
to_unit = to_unit.to_unit(converter)
|
490
|
-
raise TypeError, "cannot convert to Unit" unless ::
|
489
|
+
raise TypeError, "cannot convert to Unit" unless ::Units::Unit === to_unit
|
491
490
|
conv1, conv2 = unit.coerce(to_unit)
|
492
491
|
raise TypeError, "incompatible units for operation" if conv1.units != conv2.units
|
493
492
|
mult = conv1.multiplier / conv2.multiplier
|
494
|
-
::
|
493
|
+
::Units::Value.new(value * mult, to_unit)
|
495
494
|
end
|
496
495
|
|
497
496
|
def coerce(other) # :nodoc:
|
498
497
|
if ::Numeric === other
|
499
|
-
[::
|
498
|
+
[::Units::Value.new!(other, ::Units::Unit.new), self]
|
500
499
|
else
|
501
500
|
super
|
502
501
|
end
|
@@ -511,7 +510,7 @@ module Units
|
|
511
510
|
# exception otherwise.
|
512
511
|
def to_f
|
513
512
|
val = simplify
|
514
|
-
if ::
|
513
|
+
if ::Units::Value === val
|
515
514
|
raise TypeError, "Cannot convert to float"
|
516
515
|
else
|
517
516
|
val.to_f
|
@@ -522,7 +521,7 @@ module Units
|
|
522
521
|
# exception otherwise.
|
523
522
|
def to_i
|
524
523
|
val = simplify
|
525
|
-
if ::
|
524
|
+
if ::Units::Value === val
|
526
525
|
raise TypeError, "Cannot convert to integer"
|
527
526
|
else
|
528
527
|
val.to_i
|
@@ -533,7 +532,7 @@ module Units
|
|
533
532
|
# exception otherwise.
|
534
533
|
def to_int
|
535
534
|
val = simplify
|
536
|
-
if ::
|
535
|
+
if ::Units::Value === val
|
537
536
|
raise TypeError, "Cannot convert to integer"
|
538
537
|
else
|
539
538
|
val.to_int
|
@@ -547,7 +546,7 @@ module Units
|
|
547
546
|
if new_unit.unitless?
|
548
547
|
@value * mul
|
549
548
|
else
|
550
|
-
::
|
549
|
+
::Units::Value.new(@value * mul, new_unit)
|
551
550
|
end
|
552
551
|
end
|
553
552
|
|
@@ -560,11 +559,11 @@ module Units
|
|
560
559
|
|
561
560
|
def method_missing(m, *args, &blk)
|
562
561
|
if args.length == 1
|
563
|
-
args[0] = (::
|
564
|
-
return self * ::
|
565
|
-
elsif (::
|
562
|
+
args[0] = (::Units::Converter.converter(args[0]) rescue nil) if not args[0].is_a? ::Units::Converter
|
563
|
+
return self * ::Units::Value.new(1, ::Units::Unit.new({m => 1}, args[0])) if args[0] && args[0].registered?(m)
|
564
|
+
elsif (::Units::Converter.current.registered?(m) rescue false)
|
566
565
|
raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
|
567
|
-
return self * ::
|
566
|
+
return self * ::Units::Value.new(1, ::Units::Unit.new({m => 1}, ::Units::Converter.current))
|
568
567
|
end
|
569
568
|
::Exception.with_clean_backtrace("method_missing") {
|
570
569
|
super
|
@@ -574,9 +573,9 @@ module Units
|
|
574
573
|
private
|
575
574
|
|
576
575
|
def self.decode_string(s, converter)
|
577
|
-
if m = ::
|
576
|
+
if m = ::Units::Regexps::VALUE_REGEXP.match(s)
|
578
577
|
value = m[2].empty? ? Integer(m[1]) : Float(m[1])
|
579
|
-
unit = ::
|
578
|
+
unit = ::Units::Unit.new(m[3], converter)
|
580
579
|
[value, unit]
|
581
580
|
else
|
582
581
|
raise ::ArgumentError, "Illegal value string #{s.dump}"
|
@@ -585,8 +584,8 @@ module Units
|
|
585
584
|
|
586
585
|
def do_additive_op(op, other)
|
587
586
|
other = other.to_value
|
588
|
-
raise TypeError, "cannot convert to Value" unless ::
|
589
|
-
if other.is_a? ::
|
587
|
+
raise TypeError, "cannot convert to Value" unless ::Units::Value === other
|
588
|
+
if other.is_a? ::Units::Value
|
590
589
|
conv1, conv2 = unit.coerce(other.unit)
|
591
590
|
raise TypeError, "incompatible units for #{op}" if conv1.units != conv2.units
|
592
591
|
mult = conv2.multiplier / conv1.multiplier
|
@@ -603,18 +602,18 @@ module Units
|
|
603
602
|
|
604
603
|
def do_multiplicative_op(op, other)
|
605
604
|
case other
|
606
|
-
when ::
|
605
|
+
when ::Units::Value
|
607
606
|
[value.send(op, other.value), unit.send(op, other.unit)]
|
608
|
-
when ::
|
607
|
+
when ::Units::Unit
|
609
608
|
[value, unit.send(op, other)]
|
610
609
|
when ::Numeric
|
611
610
|
[value.send(op, other), unit]
|
612
611
|
else
|
613
|
-
# TODO: How to make this work for
|
612
|
+
# TODO: How to make this work for Units as well?
|
614
613
|
# Problem : how check whether to_value failed without
|
615
614
|
# masking all exceptions?
|
616
615
|
other = other.to_value
|
617
|
-
raise TypeError, "cannot convert to Value" unless ::
|
616
|
+
raise TypeError, "cannot convert to Value" unless ::Units::Value === other
|
618
617
|
do_multiplicative_op(op, other)
|
619
618
|
end
|
620
619
|
end
|
@@ -673,7 +672,7 @@ module Units
|
|
673
672
|
# Included the given converter in the receiver, unless it
|
674
673
|
# was already included.
|
675
674
|
def include(conv)
|
676
|
-
conv = ::
|
675
|
+
conv = ::Units::Converter.converter(conv) if not conv.is_a?(::Units::Converter)
|
677
676
|
raise "Circular include" if conv.includes?(self)
|
678
677
|
@included << conv if not includes? conv
|
679
678
|
self
|
@@ -682,7 +681,7 @@ module Units
|
|
682
681
|
# Returns whether the given converter was included in the
|
683
682
|
# receiver.
|
684
683
|
def includes?(conv)
|
685
|
-
conv = ::
|
684
|
+
conv = ::Units::Converter.converter(conv) if not conv.is_a?(::Units::Converter)
|
686
685
|
return true if conv == self
|
687
686
|
@included.each do |c|
|
688
687
|
return true if conv == c || c.includes?(conv)
|
@@ -714,7 +713,7 @@ module Units
|
|
714
713
|
# Returns the base unit with this name
|
715
714
|
def base_unit(name)
|
716
715
|
if conv = registered?(name)
|
717
|
-
return ::
|
716
|
+
return ::Units::BaseUnit.new(name, conv)
|
718
717
|
end
|
719
718
|
raise "unit #{name.to_s.dump} not registered with #{self}"
|
720
719
|
end
|
@@ -728,7 +727,7 @@ module Units
|
|
728
727
|
def method_missing(m, *args, &blk)
|
729
728
|
if registered?(m)
|
730
729
|
raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
|
731
|
-
return ::
|
730
|
+
return ::Units::Unit.new({m => 1}, self)
|
732
731
|
end
|
733
732
|
::Exception.with_clean_backtrace("method_missing") {
|
734
733
|
super
|
@@ -794,12 +793,12 @@ module Units
|
|
794
793
|
abbrev = info[:abbrev]
|
795
794
|
multiplier = info[:multiplier] || 1
|
796
795
|
power = info[:power] || 1
|
797
|
-
register_unit(pre + unit, :equals => {:unit => ::
|
796
|
+
register_unit(pre + unit, :equals => {:unit => ::Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
|
798
797
|
aliases.each do |a|
|
799
|
-
register_unit(pre + a, :equals => {:unit => ::
|
798
|
+
register_unit(pre + a, :equals => {:unit => ::Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
|
800
799
|
end
|
801
800
|
abbrevs.each do |a|
|
802
|
-
register_unit(abbrev + a, :equals => {:unit => ::
|
801
|
+
register_unit(abbrev + a, :equals => {:unit => ::Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
|
803
802
|
end
|
804
803
|
end
|
805
804
|
end
|
@@ -820,9 +819,9 @@ module Units
|
|
820
819
|
:multiplier => data[:multiplier] || data['multiplier']}
|
821
820
|
end
|
822
821
|
if /^\s*1\s*\// =~ data
|
823
|
-
{:unit => ::
|
824
|
-
elsif m = /^\s*#{::
|
825
|
-
unit = m[3] ? ::
|
822
|
+
{:unit => ::Units::Unit.new(data, self)}
|
823
|
+
elsif m = /^\s*#{::Units::Regexps::NUMBER_REGEXP}(?:\s+(\S.*)$|\s*$)/.match(data)
|
824
|
+
unit = m[3] ? ::Units::Unit.new(m[3], self) : ::Units::Unit.new({}, self)
|
826
825
|
if m[1]
|
827
826
|
multiplier = m[2].empty? ? Integer(m[1]) : Float(m[1])
|
828
827
|
{:unit => unit, :multiplier => multiplier}
|
@@ -830,7 +829,7 @@ module Units
|
|
830
829
|
{:unit => unit}
|
831
830
|
end
|
832
831
|
else
|
833
|
-
{:unit => ::
|
832
|
+
{:unit => ::Units::Unit.new(data, self)}
|
834
833
|
end
|
835
834
|
end
|
836
835
|
|
@@ -847,20 +846,22 @@ module Units
|
|
847
846
|
|
848
847
|
class << self
|
849
848
|
|
849
|
+
THREAD_REFERENCE = 'Units::converter'.to_sym
|
850
|
+
|
850
851
|
private :new
|
851
852
|
|
852
853
|
# Returns the current Converter in the current Thread.
|
853
854
|
# The default converter is the one returned by <code>converter(:default)</code>.
|
854
|
-
# See also
|
855
|
+
# See also Units#with_converter and Converter.converter.
|
855
856
|
def current
|
856
|
-
Thread.current[
|
857
|
+
Thread.current[THREAD_REFERENCE] ||= converter(:default)
|
857
858
|
end
|
858
859
|
|
859
860
|
def with_converter(conv) # :nodoc:
|
860
|
-
conv = converter(conv) if not conv.is_a? ::
|
861
|
-
raise ::ArgumentError, "Converter expected" if not conv.is_a? ::
|
861
|
+
conv = converter(conv) if not conv.is_a? ::Units::Converter
|
862
|
+
raise ::ArgumentError, "Converter expected" if not conv.is_a? ::Units::Converter
|
862
863
|
begin
|
863
|
-
old_conv = Thread.current[
|
864
|
+
old_conv = Thread.current[THREAD_REFERENCE]
|
864
865
|
if old_conv
|
865
866
|
new_conv = Converter.send(:new, nil)
|
866
867
|
new_conv.include(old_conv)
|
@@ -868,10 +869,10 @@ module Units
|
|
868
869
|
else
|
869
870
|
new_conv = conv
|
870
871
|
end
|
871
|
-
Thread.current[
|
872
|
+
Thread.current[THREAD_REFERENCE] = new_conv
|
872
873
|
yield
|
873
874
|
ensure
|
874
|
-
Thread.current[
|
875
|
+
Thread.current[THREAD_REFERENCE] = old_conv
|
875
876
|
end
|
876
877
|
end
|
877
878
|
|
@@ -919,7 +920,7 @@ module Units
|
|
919
920
|
units.each_pair do |u, e|
|
920
921
|
(u.conversion != :none ? other_units : base_units)[u] = e
|
921
922
|
end
|
922
|
-
result = Conversion.new(::
|
923
|
+
result = Conversion.new(::Units::Unit.new(base_units, self), multiplier)
|
923
924
|
other_units.each_pair do |u, e|
|
924
925
|
result *= (u.conversion ** e)
|
925
926
|
end
|
@@ -944,17 +945,16 @@ module Units
|
|
944
945
|
end
|
945
946
|
|
946
947
|
end
|
947
|
-
end
|
948
948
|
|
949
949
|
class Numeric
|
950
950
|
#
|
951
951
|
def method_missing(m, *args, &blk)
|
952
952
|
if args.length == 1
|
953
|
-
args[0] = (::
|
954
|
-
return ::
|
955
|
-
elsif ::
|
953
|
+
args[0] = (::Units::Converter.converter(args[0]) rescue nil) if not args[0].is_a? ::Units::Converter
|
954
|
+
return ::Units::Value.new(self, ::Units::Unit.new({m => 1}, args[0])) if args[0] && args[0].registered?(m)
|
955
|
+
elsif ::Units::Converter.current.registered?(m)
|
956
956
|
raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
|
957
|
-
return ::
|
957
|
+
return ::Units::Value.new(self, ::Units::Unit.new({m => 1}, ::Units::Converter.current))
|
958
958
|
end
|
959
959
|
::Exception.with_clean_backtrace("method_missing") {
|
960
960
|
super
|
@@ -963,7 +963,7 @@ class Numeric
|
|
963
963
|
|
964
964
|
#
|
965
965
|
def to_value(unit)
|
966
|
-
::
|
966
|
+
::Units::Value.new(self, unit)
|
967
967
|
end
|
968
968
|
end
|
969
969
|
|
@@ -971,11 +971,11 @@ end
|
|
971
971
|
class String
|
972
972
|
#
|
973
973
|
def to_unit(converter = nil)
|
974
|
-
::
|
974
|
+
::Units::Unit.new(self, converter)
|
975
975
|
end
|
976
976
|
|
977
977
|
#
|
978
978
|
def to_value(converter = nil)
|
979
|
-
::
|
979
|
+
::Units::Value.new(self, converter)
|
980
980
|
end
|
981
981
|
end
|
data/lib/stick/units/currency.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
require 'stick/units/base'
|
2
2
|
require 'soap/wsdlDriver'
|
3
3
|
|
4
|
-
module Stick
|
5
4
|
module Units
|
6
5
|
|
7
6
|
class CurrencyLoader < Loader
|
7
|
+
THREAD_REFERENCE = 'Units::ce_service'.to_sym
|
8
8
|
|
9
9
|
handles 'ce_service', 'currency_unit'
|
10
10
|
|
11
11
|
def ce_service(converter, name, &blk)
|
12
|
-
old_service = Thread.current[
|
13
|
-
Thread.current[
|
12
|
+
old_service = Thread.current[THREAD_REFERENCE]
|
13
|
+
Thread.current[THREAD_REFERENCE] = ::Units::Converter::ExchangeRate.const_get(name)
|
14
14
|
yield
|
15
15
|
ensure
|
16
|
-
Thread.current[
|
16
|
+
Thread.current[THREAD_REFERENCE] = old_service
|
17
17
|
end
|
18
18
|
|
19
19
|
def currency_unit(converter, name)
|
20
|
-
service = Thread.current[
|
20
|
+
service = Thread.current[THREAD_REFERENCE] || ::Units::Config::DEFAULT_CURRENCY_SERVICE
|
21
21
|
converter.send(:register_unit, name, :equals => service.create_conversion(name, converter))
|
22
22
|
end
|
23
23
|
|
@@ -36,11 +36,11 @@ module Units
|
|
36
36
|
# The only subclasses provided are currently XMethods and CachedXMethods.
|
37
37
|
#
|
38
38
|
# To be found automatically from YAML files, exchange services should
|
39
|
-
# be located under
|
39
|
+
# be located under Units::Converter::ExchangeRate.
|
40
40
|
class ExchangeRate
|
41
41
|
|
42
42
|
def self.create_conversion(curr, converter) # :nodoc:
|
43
|
-
{:unit => ::
|
43
|
+
{:unit => ::Units::Unit.new({'--base-currency--'.to_sym => 1}, converter), :multiplier => self.new(curr)}
|
44
44
|
end
|
45
45
|
|
46
46
|
def initialize(curr) # :nodoc:
|
@@ -104,7 +104,7 @@ module Units
|
|
104
104
|
private
|
105
105
|
|
106
106
|
def data
|
107
|
-
@@data ||= eval(File.read(File.join(::
|
107
|
+
@@data ||= eval(File.read(File.join(::Units::Config::CONFIGDIR, 'xmethods', 'mapping.rb')))
|
108
108
|
end
|
109
109
|
|
110
110
|
def country_mapping
|
@@ -133,7 +133,7 @@ module Units
|
|
133
133
|
private
|
134
134
|
|
135
135
|
def data
|
136
|
-
@@data ||= eval(File.read(File.join(::
|
136
|
+
@@data ||= eval(File.read(File.join(::Units::Config::CONFIGDIR, 'xmethods', 'cached.rb')))
|
137
137
|
end
|
138
138
|
|
139
139
|
end #class CachedXMethods
|
@@ -145,7 +145,7 @@ module Units
|
|
145
145
|
# Trans: What is --base-currency-- all about?
|
146
146
|
#++
|
147
147
|
def conversions(unit)
|
148
|
-
@conversions[unit] || (unit ==
|
148
|
+
@conversions[unit] || (unit == '--base-currency--'.to_sym ? :none : nil)
|
149
149
|
end
|
150
150
|
|
151
151
|
end
|
@@ -153,8 +153,8 @@ module Units
|
|
153
153
|
# Contains some configuration related constants
|
154
154
|
module Config
|
155
155
|
# The standard service used for looking up currency exchange rates
|
156
|
-
DEFAULT_CURRENCY_SERVICE = ::
|
156
|
+
DEFAULT_CURRENCY_SERVICE = ::Units::Converter::ExchangeRate::XMethods
|
157
157
|
end
|
158
158
|
|
159
159
|
end
|
160
|
-
|
160
|
+
|
data/lib/stick/units/loaders.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
module Stick
|
2
1
|
module Units
|
3
2
|
|
4
3
|
class Loader
|
@@ -74,7 +73,7 @@ module Units
|
|
74
73
|
end
|
75
74
|
|
76
75
|
def load_config(file, context)
|
77
|
-
data = File.read(File.join(::
|
76
|
+
data = File.read(File.join(::Units::Config::CONFIGDIR, file)) rescue File.read(file)
|
78
77
|
context.instance_eval { eval data, nil, file }
|
79
78
|
end
|
80
79
|
|
@@ -97,4 +96,4 @@ module Units
|
|
97
96
|
end
|
98
97
|
|
99
98
|
end
|
100
|
-
|
99
|
+
|
data/lib/stick/units/units.rb
CHANGED
data/meta/stick.roll
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
stick 1.3.
|
1
|
+
stick 1.3.2 stable 2007-12-21
|
2
2
|
lib/stick
|
data/test/test_currency.rb
CHANGED
data/test/test_units.rb
CHANGED
metadata
CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.4.6
|
|
3
3
|
specification_version: 2
|
4
4
|
name: stick
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.3.
|
6
|
+
version: 1.3.2
|
7
7
|
date: 2007-12-21 00:00:00 -05:00
|
8
8
|
summary: Stick is an comprehensive science library including a units system providing bot
|
9
9
|
require_paths:
|