stick 1.3.2 → 1.3.3

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.
@@ -1,5 +1,6 @@
1
1
  require 'stick/units/units'
2
2
 
3
+ module Stick
3
4
  module Units
4
5
 
5
6
  # Load conversion units.
@@ -8,6 +9,7 @@ module Units
8
9
  end
9
10
 
10
11
  end
12
+ end
11
13
 
12
14
 
13
15
  # Checkrun
@@ -52,6 +52,8 @@ class Exception
52
52
 
53
53
  end
54
54
 
55
+ module Stick
56
+
55
57
  # The namespace for all unit related classes. Mixing this in has the additional effect
56
58
  # of making Units.with_unit_converter available without the <code>Units.</code> prefix,
57
59
  # as well as the shortcuts for creating Units (see Units#method_missing).
@@ -73,11 +75,11 @@ module Units
73
75
 
74
76
  def method_missing(m, *args, &blk)
75
77
  if args.length == 1
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)
78
+ args[0] = (Units::Converter.converter(args[0]) rescue nil) if not args[0].is_a? Units::Converter
79
+ return Units::Unit.new({m => 1}, args[0]) if args[0] && args[0].registered?(m)
80
+ elsif (Units::Converter.current.registered?(m) rescue false)
79
81
  raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
80
- return ::Units::Unit.new({m => 1}, ::Units::Converter.current)
82
+ return Units::Unit.new({m => 1}, Units::Converter.current)
81
83
  end
82
84
  ::Exception.with_clean_backtrace("method_missing") {
83
85
  super
@@ -85,8 +87,8 @@ module Units
85
87
  end
86
88
 
87
89
  def const_missing(c)
88
- if (::Units::Converter.current.registered?(c) rescue false)
89
- return ::Units::Unit.new({c => 1}, ::Units::Converter.current)
90
+ if (Units::Converter.current.registered?(c) rescue false)
91
+ return Units::Unit.new({c => 1}, Units::Converter.current)
90
92
  else
91
93
  ::Exception.with_clean_backtrace("const_missing") {
92
94
  super
@@ -95,7 +97,7 @@ module Units
95
97
  end
96
98
 
97
99
  def self.append_features(m)
98
- m.send(:extend, ::Units)
100
+ m.send(:extend, Units)
99
101
  super
100
102
  end
101
103
 
@@ -116,7 +118,7 @@ module Units
116
118
  #
117
119
  # See also Converter.current.
118
120
  def with_unit_converter(name, &blk) # :yields:
119
- ::Units::Converter.with_converter(name, &blk)
121
+ Units::Converter.with_converter(name, &blk)
120
122
  end
121
123
 
122
124
  module_function :with_unit_converter
@@ -137,7 +139,7 @@ module Units
137
139
 
138
140
  attr_reader :name, :converter
139
141
 
140
- def initialize(name, converter = ::Units::Converter.current)
142
+ def initialize(name, converter = Units::Converter.current)
141
143
  name = name.to_sym
142
144
  raise ::ArgumentError, "unit #{name.to_s.dump} not registered with #{converter}" if not converter.registered? name
143
145
  @name = name
@@ -149,7 +151,7 @@ module Units
149
151
  end
150
152
 
151
153
  def ==(other)
152
- other.is_a?(::Units::BaseUnit) && other.name == @name && other.converter == @converter
154
+ other.is_a?(Units::BaseUnit) && other.name == @name && other.converter == @converter
153
155
  end
154
156
 
155
157
  alias eql? ==
@@ -159,7 +161,7 @@ module Units
159
161
  end
160
162
 
161
163
  def to_s
162
- if ::Units::Converter.current.includes?(converter)
164
+ if Units::Converter.current.includes?(converter)
163
165
  @name.to_s
164
166
  else
165
167
  "#{@converter}:#{@name}"
@@ -202,13 +204,13 @@ module Units
202
204
  #
203
205
  # See also Converter, Converter.converter
204
206
  def initialize(units = {}, converter = nil)
205
- conv = proc { converter ||= ::Units::Converter.current }
207
+ conv = proc { converter ||= Units::Converter.current }
206
208
  if units.is_a? ::String
207
209
  @units = decode_string(units, conv)
208
210
  else
209
211
  @units = {}
210
212
  units.each_pair do |k, v|
211
- k = conv[].base_unit(k.to_sym) if not k.is_a? ::Units::BaseUnit
213
+ k = conv[].base_unit(k.to_sym) if not k.is_a? Units::BaseUnit
212
214
  @units[k] = v
213
215
  end
214
216
  end
@@ -221,7 +223,7 @@ module Units
221
223
  @units.each_pair do |u, e|
222
224
  result[u] = e * p
223
225
  end
224
- ::Units::Unit.new(result)
226
+ Units::Unit.new(result)
225
227
  end
226
228
 
227
229
  # Multiplies with the given Unit.
@@ -240,18 +242,18 @@ module Units
240
242
  end
241
243
 
242
244
  def coerce(other) # :nodoc:
243
- return [::Units::Unit.new({}), self] if other == 1
244
- ::Units::Converter.coerce_units(self, other)
245
+ return [Units::Unit.new({}), self] if other == 1
246
+ Units::Converter.coerce_units(self, other)
245
247
  end
246
248
 
247
249
  def simplify
248
- ::Units::Converter.simplify_unit(self)
250
+ Units::Converter.simplify_unit(self)
249
251
  end
250
252
 
251
253
  # Returns +true+ iff the two units are equals, <i>i.e.,</i> iff they have
252
254
  # the same exponent for all units, and they both use the same Converter.
253
255
  def ==(other)
254
- other.is_a?(::Units::Unit) && other.units == units
256
+ other.is_a?(Units::Unit) && other.units == units
255
257
  end
256
258
 
257
259
  alias eql? ==
@@ -264,7 +266,7 @@ module Units
264
266
  # Unit. This is less strict than equality because for example hours are
265
267
  # compatible with seconds ("we can add them"), but hours are not seconds.
266
268
  def compatible_with?(other)
267
- conv1, conv2 = ::Units::Converter.coerce_units(self, other)
269
+ conv1, conv2 = Units::Converter.coerce_units(self, other)
268
270
  conv1.units == conv2.units
269
271
  end
270
272
 
@@ -288,11 +290,11 @@ module Units
288
290
 
289
291
  def method_missing(m, *args, &blk)
290
292
  if args.length == 1
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)
293
+ args[0] = (Units::Converter.converter(args[0]) rescue nil) if not args[0].is_a? Units::Converter
294
+ return self * Units::Unit.new({m => 1}, args[0]) if args[0] && args[0].registered?(m)
295
+ elsif (Units::Converter.current.registered?(m) rescue false)
294
296
  raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
295
- return self * ::Units::Unit.new({m => 1}, ::Units::Converter.current)
297
+ return self * Units::Unit.new({m => 1}, Units::Converter.current)
296
298
  end
297
299
  ::Exception.with_clean_backtrace("method_missing") {
298
300
  super
@@ -302,7 +304,7 @@ module Units
302
304
  private
303
305
 
304
306
  def decode_string(s, converter)
305
- if ::Units::Regexps::TOTAL_UNIT_REGEXP =~ s
307
+ if Units::Regexps::TOTAL_UNIT_REGEXP =~ s
306
308
  numerator, denominator = $1, $2
307
309
  units = {}
308
310
  decode_multiplicative_string(numerator, 1, converter, units) if numerator
@@ -316,9 +318,9 @@ module Units
316
318
  end
317
319
 
318
320
  def decode_multiplicative_string(s, multiplier, converter, result)
319
- s.scan(::Units::Regexps::SINGLE_UNIT_REGEXP) do |conv, unit, exp|
321
+ s.scan(Units::Regexps::SINGLE_UNIT_REGEXP) do |conv, unit, exp|
320
322
  if unit
321
- conv = ::Units::Converter.converter(conv)
323
+ conv = Units::Converter.converter(conv)
322
324
  else
323
325
  conv, unit = converter[], conv
324
326
  end
@@ -336,13 +338,13 @@ module Units
336
338
 
337
339
  def do_op(op, dual_op, other)
338
340
  other = other.to_unit
339
- raise TypeError, "cannot convert to Unit" unless ::Units::Unit === other
341
+ raise TypeError, "cannot convert to Unit" unless Units::Unit === other
340
342
  result = @units.dup
341
343
  other.units.each_pair do |u, e|
342
344
  result[u] = 0 if not result[u]
343
345
  result[u] = result[u].send(dual_op, e)
344
346
  end
345
- ::Units::Unit.new(result)
347
+ Units::Unit.new(result)
346
348
  end
347
349
 
348
350
  end
@@ -413,7 +415,7 @@ module Units
413
415
  %w{ * / }.each do |op|
414
416
  eval %{
415
417
  def #{op}(other)
416
- ::Units::Value.new(*do_multiplicative_op(:#{op}, other))
418
+ Units::Value.new(*do_multiplicative_op(:#{op}, other))
417
419
  end
418
420
  }
419
421
  end
@@ -421,14 +423,14 @@ module Units
421
423
  %w{ + - modulo remainder % }.each do |op|
422
424
  eval %{
423
425
  def #{op}(other)
424
- ::Units::Value.new(*do_additive_op(:#{op}, other))
426
+ Units::Value.new(*do_additive_op(:#{op}, other))
425
427
  end
426
428
  }
427
429
  end
428
430
 
429
431
  def divmod(other)
430
432
  (q, r), unit = *do_additive_op(:divmod, other)
431
- [q, ::Units::Value.new(r, unit)]
433
+ [q, Units::Value.new(r, unit)]
432
434
  end
433
435
 
434
436
  def div(other)
@@ -444,7 +446,7 @@ module Units
444
446
  end
445
447
 
446
448
  def **(other) # :nodoc:
447
- ::Units::Value.new(@value ** other, @unit ** other)
449
+ Units::Value.new(@value ** other, @unit ** other)
448
450
  end
449
451
 
450
452
  def <=>(other) # :nodoc:
@@ -466,7 +468,7 @@ module Units
466
468
  %w{ abs ceil floor next round succ truncate }.each do |op|
467
469
  eval %{
468
470
  def #{op}
469
- ::Units::Value.new(@value.#{op}, @unit)
471
+ Units::Value.new(@value.#{op}, @unit)
470
472
  end
471
473
  }
472
474
  end
@@ -486,16 +488,16 @@ module Units
486
488
  def to(to_unit, converter = nil)
487
489
  raise ArgumentError, "Wrong number of arguments" if converter && !(::String === to_unit)
488
490
  to_unit = to_unit.to_unit(converter)
489
- raise TypeError, "cannot convert to Unit" unless ::Units::Unit === to_unit
491
+ raise TypeError, "cannot convert to Unit" unless Units::Unit === to_unit
490
492
  conv1, conv2 = unit.coerce(to_unit)
491
493
  raise TypeError, "incompatible units for operation" if conv1.units != conv2.units
492
494
  mult = conv1.multiplier / conv2.multiplier
493
- ::Units::Value.new(value * mult, to_unit)
495
+ Units::Value.new(value * mult, to_unit)
494
496
  end
495
497
 
496
498
  def coerce(other) # :nodoc:
497
499
  if ::Numeric === other
498
- [::Units::Value.new!(other, ::Units::Unit.new), self]
500
+ [Units::Value.new!(other, Units::Unit.new), self]
499
501
  else
500
502
  super
501
503
  end
@@ -510,7 +512,7 @@ module Units
510
512
  # exception otherwise.
511
513
  def to_f
512
514
  val = simplify
513
- if ::Units::Value === val
515
+ if Units::Value === val
514
516
  raise TypeError, "Cannot convert to float"
515
517
  else
516
518
  val.to_f
@@ -521,7 +523,7 @@ module Units
521
523
  # exception otherwise.
522
524
  def to_i
523
525
  val = simplify
524
- if ::Units::Value === val
526
+ if Units::Value === val
525
527
  raise TypeError, "Cannot convert to integer"
526
528
  else
527
529
  val.to_i
@@ -532,7 +534,7 @@ module Units
532
534
  # exception otherwise.
533
535
  def to_int
534
536
  val = simplify
535
- if ::Units::Value === val
537
+ if Units::Value === val
536
538
  raise TypeError, "Cannot convert to integer"
537
539
  else
538
540
  val.to_int
@@ -546,7 +548,7 @@ module Units
546
548
  if new_unit.unitless?
547
549
  @value * mul
548
550
  else
549
- ::Units::Value.new(@value * mul, new_unit)
551
+ Units::Value.new(@value * mul, new_unit)
550
552
  end
551
553
  end
552
554
 
@@ -559,11 +561,11 @@ module Units
559
561
 
560
562
  def method_missing(m, *args, &blk)
561
563
  if args.length == 1
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)
564
+ args[0] = (Units::Converter.converter(args[0]) rescue nil) if not args[0].is_a? Units::Converter
565
+ return self * Units::Value.new(1, Units::Unit.new({m => 1}, args[0])) if args[0] && args[0].registered?(m)
566
+ elsif (Units::Converter.current.registered?(m) rescue false)
565
567
  raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
566
- return self * ::Units::Value.new(1, ::Units::Unit.new({m => 1}, ::Units::Converter.current))
568
+ return self * Units::Value.new(1, Units::Unit.new({m => 1}, Units::Converter.current))
567
569
  end
568
570
  ::Exception.with_clean_backtrace("method_missing") {
569
571
  super
@@ -573,9 +575,9 @@ module Units
573
575
  private
574
576
 
575
577
  def self.decode_string(s, converter)
576
- if m = ::Units::Regexps::VALUE_REGEXP.match(s)
578
+ if m = Units::Regexps::VALUE_REGEXP.match(s)
577
579
  value = m[2].empty? ? Integer(m[1]) : Float(m[1])
578
- unit = ::Units::Unit.new(m[3], converter)
580
+ unit = Units::Unit.new(m[3], converter)
579
581
  [value, unit]
580
582
  else
581
583
  raise ::ArgumentError, "Illegal value string #{s.dump}"
@@ -584,8 +586,8 @@ module Units
584
586
 
585
587
  def do_additive_op(op, other)
586
588
  other = other.to_value
587
- raise TypeError, "cannot convert to Value" unless ::Units::Value === other
588
- if other.is_a? ::Units::Value
589
+ raise TypeError, "cannot convert to Value" unless Units::Value === other
590
+ if other.is_a? Units::Value
589
591
  conv1, conv2 = unit.coerce(other.unit)
590
592
  raise TypeError, "incompatible units for #{op}" if conv1.units != conv2.units
591
593
  mult = conv2.multiplier / conv1.multiplier
@@ -602,9 +604,9 @@ module Units
602
604
 
603
605
  def do_multiplicative_op(op, other)
604
606
  case other
605
- when ::Units::Value
607
+ when Units::Value
606
608
  [value.send(op, other.value), unit.send(op, other.unit)]
607
- when ::Units::Unit
609
+ when Units::Unit
608
610
  [value, unit.send(op, other)]
609
611
  when ::Numeric
610
612
  [value.send(op, other), unit]
@@ -613,7 +615,7 @@ module Units
613
615
  # Problem : how check whether to_value failed without
614
616
  # masking all exceptions?
615
617
  other = other.to_value
616
- raise TypeError, "cannot convert to Value" unless ::Units::Value === other
618
+ raise TypeError, "cannot convert to Value" unless Units::Value === other
617
619
  do_multiplicative_op(op, other)
618
620
  end
619
621
  end
@@ -672,7 +674,7 @@ module Units
672
674
  # Included the given converter in the receiver, unless it
673
675
  # was already included.
674
676
  def include(conv)
675
- conv = ::Units::Converter.converter(conv) if not conv.is_a?(::Units::Converter)
677
+ conv = Units::Converter.converter(conv) if not conv.is_a?(Units::Converter)
676
678
  raise "Circular include" if conv.includes?(self)
677
679
  @included << conv if not includes? conv
678
680
  self
@@ -681,7 +683,7 @@ module Units
681
683
  # Returns whether the given converter was included in the
682
684
  # receiver.
683
685
  def includes?(conv)
684
- conv = ::Units::Converter.converter(conv) if not conv.is_a?(::Units::Converter)
686
+ conv = Units::Converter.converter(conv) if not conv.is_a?(Units::Converter)
685
687
  return true if conv == self
686
688
  @included.each do |c|
687
689
  return true if conv == c || c.includes?(conv)
@@ -713,7 +715,7 @@ module Units
713
715
  # Returns the base unit with this name
714
716
  def base_unit(name)
715
717
  if conv = registered?(name)
716
- return ::Units::BaseUnit.new(name, conv)
718
+ return Units::BaseUnit.new(name, conv)
717
719
  end
718
720
  raise "unit #{name.to_s.dump} not registered with #{self}"
719
721
  end
@@ -727,7 +729,7 @@ module Units
727
729
  def method_missing(m, *args, &blk)
728
730
  if registered?(m)
729
731
  raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
730
- return ::Units::Unit.new({m => 1}, self)
732
+ return Units::Unit.new({m => 1}, self)
731
733
  end
732
734
  ::Exception.with_clean_backtrace("method_missing") {
733
735
  super
@@ -793,12 +795,12 @@ module Units
793
795
  abbrev = info[:abbrev]
794
796
  multiplier = info[:multiplier] || 1
795
797
  power = info[:power] || 1
796
- register_unit(pre + unit, :equals => {:unit => ::Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
798
+ register_unit(pre + unit, :equals => {:unit => Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
797
799
  aliases.each do |a|
798
- register_unit(pre + a, :equals => {:unit => ::Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
800
+ register_unit(pre + a, :equals => {:unit => Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
799
801
  end
800
802
  abbrevs.each do |a|
801
- register_unit(abbrev + a, :equals => {:unit => ::Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
803
+ register_unit(abbrev + a, :equals => {:unit => Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
802
804
  end
803
805
  end
804
806
  end
@@ -819,9 +821,9 @@ module Units
819
821
  :multiplier => data[:multiplier] || data['multiplier']}
820
822
  end
821
823
  if /^\s*1\s*\// =~ data
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)
824
+ {:unit => Units::Unit.new(data, self)}
825
+ elsif m = /^\s*#{Units::Regexps::NUMBER_REGEXP}(?:\s+(\S.*)$|\s*$)/.match(data)
826
+ unit = m[3] ? Units::Unit.new(m[3], self) : Units::Unit.new({}, self)
825
827
  if m[1]
826
828
  multiplier = m[2].empty? ? Integer(m[1]) : Float(m[1])
827
829
  {:unit => unit, :multiplier => multiplier}
@@ -829,7 +831,7 @@ module Units
829
831
  {:unit => unit}
830
832
  end
831
833
  else
832
- {:unit => ::Units::Unit.new(data, self)}
834
+ {:unit => Units::Unit.new(data, self)}
833
835
  end
834
836
  end
835
837
 
@@ -858,8 +860,8 @@ module Units
858
860
  end
859
861
 
860
862
  def with_converter(conv) # :nodoc:
861
- conv = converter(conv) if not conv.is_a? ::Units::Converter
862
- raise ::ArgumentError, "Converter expected" if not conv.is_a? ::Units::Converter
863
+ conv = converter(conv) if not conv.is_a? Units::Converter
864
+ raise ::ArgumentError, "Converter expected" if not conv.is_a? Units::Converter
863
865
  begin
864
866
  old_conv = Thread.current[THREAD_REFERENCE]
865
867
  if old_conv
@@ -920,7 +922,7 @@ module Units
920
922
  units.each_pair do |u, e|
921
923
  (u.conversion != :none ? other_units : base_units)[u] = e
922
924
  end
923
- result = Conversion.new(::Units::Unit.new(base_units, self), multiplier)
925
+ result = Conversion.new(Units::Unit.new(base_units, self), multiplier)
924
926
  other_units.each_pair do |u, e|
925
927
  result *= (u.conversion ** e)
926
928
  end
@@ -945,16 +947,17 @@ module Units
945
947
  end
946
948
 
947
949
  end
950
+ end
948
951
 
949
952
  class Numeric
950
953
  #
951
954
  def method_missing(m, *args, &blk)
952
955
  if args.length == 1
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
+ args[0] = (Stick::Units::Converter.converter(args[0]) rescue nil) if not args[0].is_a?(Stick::Units::Converter)
957
+ return Stick::Units::Value.new(self, Stick::Units::Unit.new({m => 1}, args[0])) if args[0] && args[0].registered?(m)
958
+ elsif Stick::Units::Converter.current.registered?(m)
956
959
  raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
957
- return ::Units::Value.new(self, ::Units::Unit.new({m => 1}, ::Units::Converter.current))
960
+ return Stick::Units::Value.new(self, Stick::Units::Unit.new({m => 1}, Stick::Units::Converter.current))
958
961
  end
959
962
  ::Exception.with_clean_backtrace("method_missing") {
960
963
  super
@@ -963,7 +966,7 @@ class Numeric
963
966
 
964
967
  #
965
968
  def to_value(unit)
966
- ::Units::Value.new(self, unit)
969
+ Stick::Units::Value.new(self, unit)
967
970
  end
968
971
  end
969
972
 
@@ -971,11 +974,11 @@ end
971
974
  class String
972
975
  #
973
976
  def to_unit(converter = nil)
974
- ::Units::Unit.new(self, converter)
977
+ Stick::Units::Unit.new(self, converter)
975
978
  end
976
979
 
977
980
  #
978
981
  def to_value(converter = nil)
979
- ::Units::Value.new(self, converter)
982
+ Stick::Units::Value.new(self, converter)
980
983
  end
981
984
  end