vanunits 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/HISTORY +16 -0
  2. data/HOWITWORKS.rdoc +72 -0
  3. data/LICENSE +23 -0
  4. data/README.rdoc +68 -0
  5. data/lib/van/units.rb +116 -0
  6. data/lib/van/units/base.rb +991 -0
  7. data/lib/van/units/constants.rb +2 -0
  8. data/lib/van/units/constants/cgs.rb +122 -0
  9. data/lib/van/units/constants/math.rb +3 -0
  10. data/lib/van/units/constants/math/cgs.rb +125 -0
  11. data/lib/van/units/constants/math/mks.rb +126 -0
  12. data/lib/van/units/constants/math/natural.rb +33 -0
  13. data/lib/van/units/constants/mks.rb +122 -0
  14. data/lib/van/units/currency.rb +160 -0
  15. data/lib/van/units/data/binary/base.rb +4 -0
  16. data/lib/van/units/data/cex.rb +5 -0
  17. data/lib/van/units/data/currency-default.rb +5 -0
  18. data/lib/van/units/data/currency-standard.rb +2 -0
  19. data/lib/van/units/data/currency/base.rb +89 -0
  20. data/lib/van/units/data/iec.rb +5 -0
  21. data/lib/van/units/data/iec_binary/base.rb +6 -0
  22. data/lib/van/units/data/si.rb +8 -0
  23. data/lib/van/units/data/si/base.rb +11 -0
  24. data/lib/van/units/data/si/constants.rb +88 -0
  25. data/lib/van/units/data/si/derived.rb +33 -0
  26. data/lib/van/units/data/si/extra.rb +35 -0
  27. data/lib/van/units/data/si/misc.rb +10 -0
  28. data/lib/van/units/data/uk.rb +10 -0
  29. data/lib/van/units/data/uk/base.rb +25 -0
  30. data/lib/van/units/data/units-default.rb +12 -0
  31. data/lib/van/units/data/units-standard.rb +5 -0
  32. data/lib/van/units/data/us.rb +10 -0
  33. data/lib/van/units/data/us/base.rb +47 -0
  34. data/lib/van/units/data/xmethods.rb +5 -0
  35. data/lib/van/units/data/xmethods/cached.rb +84 -0
  36. data/lib/van/units/data/xmethods/mapping.rb +87 -0
  37. data/lib/van/units/loaders.rb +100 -0
  38. data/lib/van/units/units.rb +111 -0
  39. data/lib/van/units_currency.rb +12 -0
  40. data/meta/author +1 -0
  41. data/meta/collection +1 -0
  42. data/meta/contact +1 -0
  43. data/meta/created +1 -0
  44. data/meta/description +5 -0
  45. data/meta/homepage +1 -0
  46. data/meta/name +1 -0
  47. data/meta/summary +1 -0
  48. data/meta/title +1 -0
  49. data/meta/version +1 -0
  50. data/qed/conversion.rdoc +14 -0
  51. data/qed/equality.rdoc +150 -0
  52. data/qed/operations.rdoc +74 -0
  53. data/test/test_constants.rb +12 -0
  54. data/test/test_currency.rb +26 -0
  55. data/test/test_units.rb +205 -0
  56. metadata +123 -0
@@ -0,0 +1,160 @@
1
+ require 'van/units/base'
2
+ require 'soap/wsdlDriver'
3
+
4
+ module Van
5
+ module Units
6
+
7
+ class CurrencyLoader < Loader
8
+ THREAD_REFERENCE = 'Units::ce_service'.to_sym
9
+
10
+ handles 'ce_service', 'currency_unit'
11
+
12
+ def ce_service(converter, name, &blk)
13
+ old_service = Thread.current[THREAD_REFERENCE]
14
+ Thread.current[THREAD_REFERENCE] = Units::Converter::ExchangeRate.const_get(name)
15
+ yield
16
+ ensure
17
+ Thread.current[THREAD_REFERENCE] = old_service
18
+ end
19
+
20
+ def currency_unit(converter, name)
21
+ service = Thread.current[THREAD_REFERENCE] || Units::Config::DEFAULT_CURRENCY_SERVICE
22
+ converter.send(:register_unit, name, :equals => service.create_conversion(name, converter))
23
+ end
24
+ end
25
+
26
+ class Converter
27
+
28
+ # Encapsulates a service for retrieving exchange rates.
29
+ # This is used by Converter.register_currency.
30
+ # An instance of this class behaves like a Numeric when used
31
+ # in calculations. This class is used to look up exchange rates
32
+ # lazily.
33
+ #
34
+ # This class is not supposed to be instantiated by itself. Instead a
35
+ # subclass should be created that defines the method +get_rate+.
36
+ # The only subclasses provided are currently XMethods and CachedXMethods.
37
+ #
38
+ # To be found automatically from YAML files, exchange services should
39
+ # be located under Units::Converter::ExchangeRate.
40
+ class ExchangeRate
41
+
42
+ def self.create_conversion(curr, converter) # :nodoc:
43
+ {:unit => Units::Unit.new({'--base-currency--'.to_sym => 1}, converter), :multiplier => self.new(curr)}
44
+ end
45
+
46
+ def initialize(curr) # :nodoc:
47
+ @curr = curr
48
+ end
49
+
50
+ # This method should be overridden in subclasses to return the
51
+ # exchange rate represented by this object. The unit in question
52
+ # is available as a String in the instance variable <code>@curr</code>.
53
+ # The rate should be calculated against an arbitrary but fixed base currency.
54
+ # The rate should be such that the following would be true
55
+ # 1 * curr = rate * base_curr
56
+ # This function should return +nil+ if the currency is not supported by this
57
+ # retrieval service. It should <em>not</em> raise an exception.
58
+ def get_rate
59
+ raise NoMethodError, "undefined method `get_rate' for #{to_s}:#{self.class}"
60
+ end
61
+
62
+ def value # :nodoc:
63
+ @value ||= get_rate or raise TypeError, "currency not supported by current service: #{@curr.to_s.dump}"
64
+ end
65
+
66
+ def coerce(other) # :nodoc:
67
+ [value, other]
68
+ end
69
+
70
+ def *(other) # :nodoc:
71
+ value * other
72
+ end
73
+
74
+ def /(other) # :nodoc:
75
+ value / other
76
+ end
77
+
78
+ def +(other) # :nodoc:
79
+ value + other
80
+ end
81
+
82
+ def -(other) # :nodoc:
83
+ value - other
84
+ end
85
+
86
+ def **(other) # :nodoc:
87
+ value ** other
88
+ end
89
+
90
+ # Exchange rate retrieval service that uses a service from http://www.xmethods.com.
91
+ # See http://rubyurl.com/7uq.
92
+ class XMethods < ExchangeRate
93
+
94
+ # This is the only method that a subclass of ExchangeRate
95
+ # needs to implement. This is a good example to follow.
96
+ def get_rate
97
+ driver.getRate(country_mapping[@curr], country_mapping[base])
98
+ rescue
99
+ p $!
100
+ puts $!.backtrace
101
+ nil
102
+ end
103
+
104
+ private
105
+
106
+ def data
107
+ @@data ||= eval(File.read(File.join(Units::Config::CONFIGDIR, 'xmethods', 'mapping.rb')))
108
+ end
109
+
110
+ def country_mapping
111
+ @@country_mapping ||= data[:mapping]
112
+ end
113
+
114
+ def base
115
+ @@base ||= data[:base]
116
+ end
117
+
118
+ def driver
119
+ @@driver ||= SOAP::WSDLDriverFactory.new("http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl").create_rpc_driver
120
+ end
121
+
122
+ end
123
+
124
+ # Cached values for the XMethods exchange rate service.
125
+ class CachedXMethods < ExchangeRate
126
+
127
+ # This is the only method that a subclass of ExchangeRate
128
+ # needs to implement. This is a good example to follow.
129
+ def get_rate
130
+ data[@curr]
131
+ end
132
+
133
+ private
134
+
135
+ def data
136
+ @@data ||= eval(File.read(File.join(Units::Config::CONFIGDIR, 'xmethods', 'cached.rb')))
137
+ end
138
+
139
+ end #class CachedXMethods
140
+ end
141
+
142
+ private
143
+
144
+ #--
145
+ # Trans: What is --base-currency-- all about?
146
+ #++
147
+ def conversions(unit)
148
+ @conversions[unit] || (unit == '--base-currency--'.to_sym ? :none : nil)
149
+ end
150
+
151
+ end
152
+
153
+ # Contains some configuration related constants
154
+ module Config
155
+ # The standard service used for looking up currency exchange rates
156
+ DEFAULT_CURRENCY_SERVICE = Units::Converter::ExchangeRate::XMethods
157
+ end
158
+
159
+ end
160
+ end
@@ -0,0 +1,4 @@
1
+ converter 'binary_base' do
2
+ binary_unit('bit', 'b')
3
+ binary_unit('byte', 'B'){ '8.0 b' }
4
+ end
@@ -0,0 +1,5 @@
1
+ converter 'cex' do
2
+ ce_service 'CachedXMethods' do
3
+ load 'currency/base.rb'
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'cex'
2
+
3
+ converter "default" do
4
+ include 'cex'
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'currency-default'
2
+ require 'xmethods'
@@ -0,0 +1,89 @@
1
+ currency_unit "aed"
2
+ currency_unit "afa"
3
+ currency_unit "all"
4
+ currency_unit "ars"
5
+ currency_unit "ats"
6
+ currency_unit "aud"
7
+ currency_unit "bbd"
8
+ currency_unit "bdt"
9
+ currency_unit "bef"
10
+ currency_unit "bgn"
11
+ currency_unit "bhd"
12
+ currency_unit "bmd"
13
+ currency_unit "brl"
14
+ currency_unit "bsd"
15
+ currency_unit "cad"
16
+ currency_unit "chf"
17
+ currency_unit "clp"
18
+ currency_unit "cny"
19
+ currency_unit "cop"
20
+ currency_unit "crc"
21
+ currency_unit "cyp"
22
+ currency_unit "czk"
23
+ currency_unit "dem"
24
+ currency_unit "dkk"
25
+ currency_unit "dop"
26
+ currency_unit "dzd"
27
+ currency_unit "eek"
28
+ currency_unit "egp"
29
+ currency_unit "esp"
30
+ currency_unit "eur"
31
+ currency_unit "fim"
32
+ currency_unit "fjd"
33
+ currency_unit "frf"
34
+ currency_unit "gbp"
35
+ currency_unit "grd"
36
+ currency_unit "hkd"
37
+ currency_unit "hrk"
38
+ currency_unit "huf"
39
+ currency_unit "idr"
40
+ currency_unit "iep"
41
+ currency_unit "ils"
42
+ currency_unit "inr"
43
+ currency_unit "iqd"
44
+ currency_unit "irr"
45
+ currency_unit "isk"
46
+ currency_unit "itl"
47
+ currency_unit "jmd"
48
+ currency_unit "jod"
49
+ currency_unit "jpy"
50
+ currency_unit "kes"
51
+ currency_unit "krw"
52
+ currency_unit "kwd"
53
+ currency_unit "lbp"
54
+ currency_unit "lkr"
55
+ currency_unit "luf"
56
+ currency_unit "mad"
57
+ currency_unit "mtl"
58
+ currency_unit "mur"
59
+ currency_unit "mxn"
60
+ currency_unit "myr"
61
+ currency_unit "nlg"
62
+ currency_unit "nok"
63
+ currency_unit "nzd"
64
+ currency_unit "omr"
65
+ currency_unit "pen"
66
+ currency_unit "php"
67
+ currency_unit "pkr"
68
+ currency_unit "pln"
69
+ currency_unit "pte"
70
+ currency_unit "qar"
71
+ currency_unit "ron"
72
+ currency_unit "rub"
73
+ currency_unit "sar"
74
+ currency_unit "sdd"
75
+ currency_unit "sek"
76
+ currency_unit "sgd"
77
+ currency_unit "sit"
78
+ currency_unit "skk"
79
+ currency_unit "thb"
80
+ currency_unit "tnd"
81
+ currency_unit "try"
82
+ currency_unit "ttd"
83
+ currency_unit "twd"
84
+ currency_unit "usd"
85
+ currency_unit "veb"
86
+ currency_unit "vnd"
87
+ currency_unit "xcd"
88
+ currency_unit "zar"
89
+ currency_unit "zmk"
@@ -0,0 +1,5 @@
1
+ require 'iec_binary/base'
2
+
3
+ converter 'iec' do
4
+ include 'binary_iec_base'
5
+ end
@@ -0,0 +1,6 @@
1
+ require 'binary/base'
2
+
3
+ converter 'binary_iec_base' do
4
+ binary_iec_unit('bit', :b){ 'binary_base:b' }
5
+ binary_iec_unit('byte', :B){ '8.0 b' }
6
+ end
@@ -0,0 +1,8 @@
1
+ require 'si/constants'
2
+
3
+ converter 'si' do
4
+ include 'si_base'
5
+ include 'si_derived'
6
+ include 'si_extra'
7
+ include 'si_constants'
8
+ end
@@ -0,0 +1,11 @@
1
+ # http://physics.nist.gov/cuu/Units/units.html
2
+
3
+ converter 'si_base' do
4
+ si_unit :meter , :m
5
+ si_unit :gram , :g
6
+ si_unit :second , :s
7
+ si_unit :ampere , :A
8
+ si_unit :kelvin , :K
9
+ si_unit :mole , :mol , :moles
10
+ si_unit :candela, :cd
11
+ end
@@ -0,0 +1,88 @@
1
+ require 'si/extra'
2
+
3
+ # TODO: Some of these arn't natural constants, and should be put in a separate converter.
4
+
5
+ converter "si_constants" do
6
+ unit :gravitational_constant, :G do "6.673e-11 si_base::m**3 / si_base:kg si_base:s**2" end
7
+
8
+ # TODO: name? earth_nominal_gravity
9
+ #unit :gravitational_acceleration = 9.80665e0.m/s**2 # m / s^2
10
+
11
+ unit(:lightspeed, :c, :speed_of_light){ "2.99792458e8 si_base:m / si_base:s" }
12
+
13
+ unit :lightyear do "9.46053620707e15 si_base:m" end
14
+
15
+ #unit :astronomical_unit, :au do "1.49597870691e11 si_base:m" end
16
+ unit :parsec do "3.08567758135e16 si_base:m" end
17
+
18
+ unit :planks_constant, :h do "6.62606876e-34 si_base:kg si_base:m**2 / si_base:s" end
19
+ unit :planks_constant_bar, :hbar do "1.05457159642e-34 si_base:kg si_base:m**2 / si_base:s" end
20
+
21
+ unit :vacuum_permeability do "1.25663706144e-6 si_base:kg si_base:m / si_base:A**2 * si_base:s**2" end
22
+ unit :vacuum_permittivity do "8.854187817e-12 A**2 s**4 / kg m**3" end
23
+
24
+ #unit :electronvolt, :eV do "1.602176462e-19 si_derived:J" end
25
+ unit :electroncharge do "1.602176462e-19 A * s" end
26
+
27
+ unit "electronmass" do "9.10938188e-31 kg" end
28
+ unit "muonmass" do "1.88353109e-28 kg" end
29
+ unit "protonmass" do "1.67262158e-27 kg" end
30
+ unit "neutronmass" do "1.67492716e-27 kg" end
31
+
32
+ unit "rydberg" do "2.17987190389e-18 kg * m**2 / s**2" end
33
+ unit "boltzmann" do "1.3806503e-23 kg m**2 / K s**2" end
34
+
35
+ unit "bohr_magneton" do "9.27400899e-24 A m**2" end
36
+ unit "nuclear_magneton" do "5.05078317e-27 A m**2" end
37
+ unit :electron_magnetic_moment do "9.28476362e-24 A m**2" end
38
+ unit :proton_magnetic_moment do "1.410606633e-26 A m**2" end
39
+
40
+ unit :bohr_radius, [], "bohr_radii" do "5.291772083e-11 m" end
41
+
42
+ #unit :unified_atomic_mass, "1.66053873e-27 si_base:kg", :abbrev => "u"
43
+ #unit :atomic_mass "1.66053873e-27 si_base:kg"
44
+ #unit :angstrom, "100.0 si_derived:kPa"
45
+
46
+ unit :molar_gas do "8.314472e0 kg m**2 / K mol s**2" end
47
+ unit :standard_gas_volume do "2.2710981e-2 m**3 / mol" end
48
+
49
+ unit :solar_mass do "1.98892e30 kg" end
50
+
51
+ unit :carat do "2e-4 si_base:kg" end
52
+
53
+ unit :fathom do "1.8288e0 m" end
54
+ unit :mil do "2.54e-5 m" end
55
+ unit :point do "3.52777777778e-4 m" end
56
+ unit :texpoint do "3.51459803515e-4 m" end
57
+
58
+ unit :micron do "1e-6 m" end
59
+
60
+ unit :ton do "9.0718474e2 kg" end
61
+ unit :troy_ounce do "3.1103475e-2 kg" end
62
+
63
+ unit :gram_force do "9.80665e-3 kg m / s**2" end
64
+ unit :poundal do "1.38255e-1 kg m / s**2" end
65
+ unit :calorie do "4.1868e0 kg m**2 / s**2" end
66
+ unit :btu do "1.05505585262e3 kg m**2 / s**2" end
67
+ unit :therm do "1.05506e8 kg m**2 / s**2" end
68
+
69
+ unit :horsepower do "7.457e2 kg m**2 / s**3" end
70
+ unit :standard_atmosphere do "1.01325e5 kg / m s**2" end
71
+ unit :torr do "1.33322368421e2 kg / m s**2" end
72
+ unit :meter_of_mercury do "1.33322368421e5 kg / m s**2" end
73
+
74
+ unit :poise do "1e-1 kg m**-1 s**-1" end
75
+ unit :stokes do "1e-4 m**2 / s" end
76
+ unit :faraday do "9.6485341472e4 A s / mol" end
77
+
78
+ unit :stilb do "1e4 cd / m**2" end # cd / m^2
79
+ unit :phot do "1e4 cd * sr / m**2" end # cd sr / m^2
80
+ #unit :lumen do "1e0 cd * sr" end # cd sr
81
+ #unit :lux do "1e0 cd * sr / m**2" end # cd sr / m^2
82
+
83
+ unit :dyne do "1e-5 kg m / s**2" end
84
+ unit :gauss do "1e-4 kg / A s**2" end
85
+ unit :erg do "1e-7 kg m**2 / s**2" end
86
+
87
+ end
88
+
@@ -0,0 +1,33 @@
1
+ # http://physics.nist.gov/cuu/Units/units.html
2
+
3
+ require 'si/base'
4
+
5
+ converter 'si_derived' do
6
+ si_unit( :radian, :rad ){ "" }
7
+ si_unit( :steradian, :sr ){ "" }
8
+
9
+ si_unit( :hertz, :Hz ){ "1 / si_base:s" }
10
+ si_unit( :newton, :N ){ "si_base:kg si_base:m / si_base:s**2" }
11
+ si_unit( :pascal, :Pa ){ "N / si_base:m**2" }
12
+ si_unit( :joule, :J ){ "N * si_base:m" }
13
+ si_unit( :watt, :W ){ "J / si_base:s" }
14
+ si_unit( :coulomb, :C ){ "si_base:A * si_base:s" }
15
+ si_unit( :volt, :V ){ "W / si_base:A" }
16
+ si_unit( :farad, :F ){ "C / V" }
17
+ si_unit( :ohm ){ "V / si_base:A" }
18
+ si_unit( :siemens, :S ){ "si_base:A / V" }
19
+ si_unit( :weber, :Wb ){ "V * si_base:s" }
20
+ si_unit( :tesla, :T ){ "Wb / si_base:m**2" }
21
+
22
+ si_unit( :henry, :H , :henries ){ "Wb / si_base:A" }
23
+ si_unit( :lumen, :lm ){ "si_base:cd sr" }
24
+ si_unit( :lux, :lx , :luxen ){ "lm / si_base:m**2" }
25
+
26
+ si_unit( :becquerel, :Bq ){ "1 / si_base:s" }
27
+ si_unit( :gray, :Gy ){ "J / si_base:kg" }
28
+ si_unit( :sievert, :Sv ){ "J / si_base:kg" }
29
+ si_unit( :katal, :kat ){ "si_base:mol / si_base:s" }
30
+
31
+ si_unit( :bar ){ "100.0 kPa" } # TODO: should this be in si_extra ?
32
+ end
33
+
@@ -0,0 +1,35 @@
1
+ # http://physics.nist.gov/cuu/Units/outside.html
2
+
3
+ require 'si/derived'
4
+
5
+ converter "si_extra" do
6
+ unit(:minute, :min){ "60.0 si_base:s" }
7
+ unit(:hour, :hr ){ "60.0 min" }
8
+ unit(:day, :d ){ "24.0 hr" }
9
+ unit(:liter, :L ){ "si_base:dm**3" }
10
+
11
+ unit(:neper, :Np ){ "" }
12
+ #unit( :bel, :B { "(1/2) ln 10 Np" }
13
+ #unit( :decibel, :dB ){ "10 B" }
14
+
15
+ unit(:are, :a ){ "si_base:dam**2" }
16
+ unit(:hectare, :ha ){ "si_base:hm**2" }
17
+ #unit(:bar ){ "100.0 si_derived:kPa" }
18
+ unit(:barn ){ "100.0 si_base:fm**2" }
19
+ unit(:curie, :Ci ){ "3.7e10 si_derived:Bq" }
20
+ unit(:roentgen, :R ){ "2.58e-4 si_derived:C / si_base:kg" }
21
+
22
+ unit(:metric_ton, :t ){ "1000.0 si_base:kg" }
23
+ unit(:nautical_mile ){ "1852.0 si_base:m" }
24
+ unit(:knot ){ "nautical_mile / hr" }
25
+
26
+ unit(:rad){ "1e-2 si_derived:Gy" }
27
+ unit(:rem){ "1e-2 si_derived:Sv" }
28
+
29
+ unit(:degree){ "0.0174532925199433 si_derived:rad" }
30
+ unit(:angstrom){ "10e-10 m" }
31
+ unit(:electronvolt, :eV){ "1.60218e-19 si_derived:J" }
32
+ unit(:astronomical_unit, :ua){ "1.49598e11 si_base:m" }
33
+ unit(:unified_atomic_mass_unit, :u){ "1.66054e-27 si_base:kg" }
34
+ end
35
+