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,10 @@
1
+ require 'si/extra'
2
+
3
+ converter "si_misc" do
4
+ unit(:week, :wk){ "7 si_extra:d" }
5
+ #unit(:kilometers_per_hour, :kph){ "km / si_extra:hr" }
6
+ end
7
+
8
+ # INCH_OF_MERCURY = 3.38638815789e3.kg/m*s**2 # kg / m s^2
9
+ # INCH_OF_WATER = 2.490889e2.kg/m*s**2 # kg / m s^2
10
+
@@ -0,0 +1,10 @@
1
+ require 'si/extra'
2
+ require 'uk/base'
3
+ # require 'binary/base'
4
+ converter 'uk' do
5
+ include 'si_base'
6
+ include 'si_derived'
7
+ include 'si_extra'
8
+ include 'uk_base'
9
+ # include 'binary_base'
10
+ end
@@ -0,0 +1,25 @@
1
+ require 'si/base'
2
+
3
+ converter 'uk_base' do
4
+ length_unit(:inch, :in){ "2.54 si_base:cm" }
5
+ length_unit(:foot, :ft){ "12.0 in" }
6
+ length_unit(:yard, :yd){ "3.0 ft" }
7
+ length_unit(:chain ){ "22.0 yd" }
8
+ length_unit(:furlong ){ "10.0 chains" }
9
+ length_unit(:mile, :mi){ "8.0 furlongs" }
10
+
11
+ unit("acre" ){ "10 square_chain" }
12
+ unit("fluid_ounce" , :fl_oz ){ "2.8413062e-2 si_base:dm**3" }
13
+ unit("gill" ){ "5.0 fl_oz" }
14
+ unit("pint" , :pt ){ "4.0 gills" }
15
+ unit("quart" , :qt ){ "2.0 pt" }
16
+ unit("gallon" , :gal ){ "4.0 qt" }
17
+ unit("grain" , :gr ){ "6.479891e-5 si_base:kg" }
18
+ unit("ounce" , :oz ){ "437.5 gr" }
19
+ unit("pound" , :lb ){ "16.0 oz" }
20
+ unit("stone" ){ "14.0 lb" }
21
+ unit("hundredweight", :cwt ){ "8.0 stone" }
22
+ unit("long_ton" , :lt ){ "20.0 cwt" }
23
+ end
24
+
25
+ # UK_TON = 1.0160469088e3.kg # kg
@@ -0,0 +1,12 @@
1
+ require 'si/constants'
2
+ require 'us/base'
3
+ require 'binary/base'
4
+
5
+ converter "default" do
6
+ include 'si_base'
7
+ include 'si_derived'
8
+ include 'si_extra'
9
+ include 'si_constants'
10
+ include 'us_base'
11
+ include 'binary_base'
12
+ end
@@ -0,0 +1,5 @@
1
+ require 'units-default'
2
+ require 'uk'
3
+ require 'us'
4
+ require 'si'
5
+ require 'iec'
@@ -0,0 +1,10 @@
1
+ require 'si/extra'
2
+ require 'us/base'
3
+
4
+ converter 'us' do
5
+ include 'si_base'
6
+ include 'si_derived'
7
+ include 'si_extra'
8
+ include 'us_base'
9
+ include 'binary_base'
10
+ end
@@ -0,0 +1,47 @@
1
+ require 'si/base'
2
+
3
+ converter 'us_base' do
4
+ length_unit( :inch, :in ){ "2.54 si_base:cm" }
5
+ length_unit( :foot, :ft, :feet ){ "12.0 in" }
6
+ length_unit( :yard, :yd ){ "3.0 ft" }
7
+ length_unit( :furlong ){ "220.0 yd" }
8
+ length_unit( :mile, :mi ){ "8.0 furlongs" }
9
+ length_unit( :acre ){ "4840.0 sq_yd" }
10
+
11
+ unit( :section ){ "1.0 sq_mi" }
12
+ unit( :township ){ "36.0 sections" }
13
+
14
+ unit( :fluid_ounce, :fl_oz ){ "2.95735295625e-2 si_base:dm**3" }
15
+ unit( :gill ){ "4.0 fl_oz" }
16
+ unit( :cup ){ "8.0 fl_oz" }
17
+ unit( :pint, :pt ){ "4.0 gills" }
18
+ unit( :quart, :qt ){ "2.0 pt" }
19
+ unit( :gallon, :gal ){ "4.0 qt" }
20
+ unit( :grain, :gr ){ "6.479891e-5 si_base:kg" }
21
+ unit( :ounce, :oz ){ "437.5 gr" }
22
+ unit( :pound, :lb ){ "16.0 oz" }
23
+ unit( :stone ){ "14.0 lb" }
24
+ unit( :hundredweight, :cwt ){ "100.0 lb" }
25
+ unit( :short_ton, :st ){ "20.0 cwt" }
26
+
27
+ unit( :tablespoon, :Tbsp ){ "1.47867647813e-5 si_base:m**3" } # FIXME
28
+ unit( :teaspoon, :tsp ){ "4.92892159375e-6 si_base:m**3" } # FIXME
29
+
30
+ unit( :psi ){ "6.89475729317e3 kg / m s**2" } # FIXME: make per sqr inch
31
+
32
+ unit :footcandle do "1.076e1 cd * sr / m**2" end # cd sr / m^2
33
+ unit :lambert do "1e4.cd sr / m**2" end # cd sr / m^2
34
+ unit :footlambert do "1.07639104e1 cd sr / m**2" end # cd sr / m^2
35
+
36
+ #unit( :miles_per_hour, :mph){ "m / si_extra:hr" }
37
+ end
38
+
39
+
40
+ # POUND_MASS = 4.5359237e-1.kg # kg
41
+ # OUNCE_MASS = 2.8349523125e-2.kg # kg
42
+
43
+ # POUND_FORCE = 4.44822161526e0.kg*m/s**2 # kg m / s^2
44
+ # KILOPOUND_FORCE = 4.44822161526e3.kg*m/s**2 # kg m / s^2
45
+
46
+
47
+
@@ -0,0 +1,5 @@
1
+ converter 'xmethods' do
2
+ ce_service 'XMethods' do
3
+ load 'currency/base.rb'
4
+ end
5
+ end
@@ -0,0 +1,84 @@
1
+ {
2
+ "fim" => 6072.2954,
3
+ "pln" => 9075.811,
4
+ "bbd" => 14871.17,
5
+ "chf" => 23351.135,
6
+ "dem" => 18459.8,
7
+ "sit" => 150.701,
8
+ "usd" => 29742.34,
9
+ "krw" => 28.5984,
10
+ "idr" => 2.9668,
11
+ "mad" => 3289.3904,
12
+ "qar" => 8168.5044,
13
+ "all" => 291.3916,
14
+ "nok" => 4626.3496,
15
+ "jpy" => 257.5987,
16
+ "lbp" => 19.7755,
17
+ "bmd" => 29742.34,
18
+ "egp" => 5145.7334,
19
+ "myr" => 7878.1396,
20
+ "sar" => 7930.2334,
21
+ "php" => 539.4946,
22
+ "sgd" => 17614.652,
23
+ "hrk" => 4885.5645,
24
+ "ats" => 2623.7969,
25
+ "mur" => 974.8391,
26
+ "ars" => 9856.617,
27
+ "itl" => 18.6463,
28
+ "pte" => 180.0871,
29
+ "veb" => 10.3086,
30
+ "huf" => 143.8129,
31
+ "kwd" => 101822.45,
32
+ "aed" => 8097.1196,
33
+ "iep" => 45842.918,
34
+ "clp" => 54.3736,
35
+ "dzd" => 407.6527,
36
+ "ttd" => 4721.0815,
37
+ "crc" => 60.6182,
38
+ "jod" => 41902.42,
39
+ "nlg" => 16383.385,
40
+ "skk" => 923.0273,
41
+ "lkr" => 292.1071,
42
+ "ron" => 1.0,
43
+ "mtl" => 84017.91,
44
+ "cyp" => 62946.754,
45
+ "bef" => 895.0005,
46
+ "mxn" => 2727.2039,
47
+ "luf" => 895.0005,
48
+ "cad" => 25353.627,
49
+ "hkd" => 3835.2964,
50
+ "zar" => 4416.545,
51
+ "tnd" => 22345.861,
52
+ "omr" => 77252.83,
53
+ "xcd" => 11015.681,
54
+ "jmd" => 464.3612,
55
+ "rub" => 1046.0316,
56
+ "thb" => 729.5153,
57
+ "sek" => 3787.4822,
58
+ "kes" => 403.5596,
59
+ "pen" => 8791.706,
60
+ "czk" => 1214.4043,
61
+ "dkk" => 4838.1196,
62
+ "vnd" => 1.8692,
63
+ "pkr" => 497.5299,
64
+ "isk" => 492.1784,
65
+ "bhd" => 78892.15,
66
+ "bdt" => 452.355,
67
+ "grd" => 105.9552,
68
+ "inr" => 660.3539,
69
+ "esp" => 216.9908,
70
+ "cny" => 3678.1604,
71
+ "frf" => 5504.0547,
72
+ "cop" => 12.9704,
73
+ "sdd" => 126.1819,
74
+ "zmk" => 7.0479,
75
+ "eur" => 36104.23,
76
+ "ils" => 6415.5176,
77
+ "dop" => 881.2545,
78
+ "aud" => 22506.023,
79
+ "gbp" => 53021.656,
80
+ "twd" => 882.3264,
81
+ "eek" => 2307.3428,
82
+ "nzd" => 21018.906,
83
+ "brl" => 12985.086
84
+ }
@@ -0,0 +1,87 @@
1
+ {
2
+ 'mapping' => {
3
+ "fim" => "finland",
4
+ "pln" => "poland",
5
+ "bbd" => "barbados",
6
+ "chf" => "switzerland",
7
+ "dem" => "germany",
8
+ "sit" => "slovenia",
9
+ "usd" => "united states",
10
+ "krw" => "korea",
11
+ "idr" => "indonesia",
12
+ "mad" => "morocco",
13
+ "qar" => "qatar",
14
+ "all" => "albania",
15
+ "nok" => "norway",
16
+ "jpy" => "japan",
17
+ "lbp" => "lebanon",
18
+ "bmd" => "bermuda",
19
+ "egp" => "egypt",
20
+ "myr" => "malaysia",
21
+ "sar" => "saudi arabia",
22
+ "php" => "philippines",
23
+ "sgd" => "singapore",
24
+ "hrk" => "croatia",
25
+ "ats" => "austria",
26
+ "mur" => "mauritius",
27
+ "ars" => "argentina",
28
+ "itl" => "italy",
29
+ "pte" => "portugal",
30
+ "veb" => "venezuela",
31
+ "huf" => "hungary",
32
+ "kwd" => "kuwait",
33
+ "aed" => "united arib emirates",
34
+ "iep" => "ireland",
35
+ "clp" => "chile",
36
+ "dzd" => "algeria",
37
+ "ttd" => "trinidad",
38
+ "crc" => "costa rica",
39
+ "jod" => "jordan",
40
+ "nlg" => "netherlands",
41
+ "lkr" => "sri lanka",
42
+ "skk" => "slovakia",
43
+ "ron" => "romania",
44
+ "mtl" => "malta",
45
+ "cyp" => "cyprus",
46
+ "bef" => "belgium",
47
+ "mxn" => "mexico",
48
+ "luf" => "luxembourg",
49
+ "cad" => "canada",
50
+ "hkd" => "hong kong",
51
+ "zar" => "south africa",
52
+ "tnd" => "tunisia",
53
+ "omr" => "oman",
54
+ "xcd" => "east caribbean",
55
+ "jmd" => "jamaica",
56
+ "rub" => "russia",
57
+ "thb" => "thailand",
58
+ "sek" => "sweden",
59
+ "kes" => "kenya",
60
+ "pen" => "peru",
61
+ "czk" => "czech republic",
62
+ "dkk" => "denmark",
63
+ "vnd" => "vietnam",
64
+ "pkr" => "pakistan",
65
+ "isk" => "iceland",
66
+ "bhd" => "bahrain",
67
+ "bdt" => "bangladesh",
68
+ "grd" => "greece",
69
+ "inr" => "india",
70
+ "cny" => "china",
71
+ "frf" => "france",
72
+ "cop" => "colombia",
73
+ "esp" => "spain",
74
+ "sdd" => "sudan",
75
+ "zmk" => "zambia",
76
+ "eur" => "euro",
77
+ "ils" => "israel",
78
+ "dop" => "dominican republic",
79
+ "aud" => "australia",
80
+ "twd" => "taiwan",
81
+ "gbp" => "united kingdom",
82
+ "eek" => "estonia",
83
+ "nzd" => "new zealand",
84
+ "brl" => "brazil"
85
+ },
86
+ 'base' => "ron"
87
+ }
@@ -0,0 +1,100 @@
1
+ module Van
2
+ module Units
3
+
4
+ class Loader
5
+
6
+ class << self
7
+
8
+ def handles(*types)
9
+ handles = @handles ||= []
10
+ unless types.empty?
11
+ res = handles.push(*types)
12
+ Converter.register_loader(self)
13
+ res
14
+ else
15
+ handles
16
+ end
17
+ end
18
+
19
+ def method_added(m)
20
+ eval %{
21
+ def self.#{m}(*a, &b)
22
+ (@instance ||= new).#{m}(*a, &b)
23
+ end
24
+ }
25
+ super
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+
32
+ class Converter
33
+
34
+ @required_configs = {}
35
+
36
+ class << self
37
+
38
+ def require(file)
39
+ @required_configs[file] ||= begin
40
+ load_config(file + ".rb", self)
41
+ true
42
+ end
43
+ end
44
+
45
+ # def load(file)
46
+ # load_config(file, self)
47
+ # end
48
+
49
+ def register_loader(loader)
50
+ loaders[loader] ||= loader
51
+ loader.handles.each do |h|
52
+ loader_hash[h] ||= begin
53
+ eval %{
54
+ module_eval do
55
+ def #{h}(*a, &b)
56
+ self.class.send(:loader_hash)[#{h.inspect}].#{h}(self, *a, &b)
57
+ end
58
+ end
59
+ }
60
+ loader
61
+ end
62
+ end
63
+ @loader_hash
64
+ end
65
+
66
+ private
67
+
68
+ def loaders
69
+ @loaders ||= {}
70
+ end
71
+
72
+ def loader_hash
73
+ @loader_hash ||= {}
74
+ end
75
+
76
+ def load_config(file, context)
77
+ data = File.read(File.join(Units::Config::CONFIGDIR, file)) rescue File.read(file)
78
+ context.instance_eval { eval data, nil, file }
79
+ end
80
+
81
+ end
82
+
83
+ def load(file)
84
+ self.class.send(:load_config, file, self)
85
+ end
86
+
87
+ end
88
+
89
+ class StandardLoader < Loader
90
+
91
+ handles 'unit'
92
+
93
+ def unit(converter, name, *args)
94
+ converter.send(:register_unit, name, *args)
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,111 @@
1
+ require 'van/units/base'
2
+
3
+ module Van
4
+ module Units
5
+
6
+ class SILoader < Loader
7
+
8
+ # The prefixes used for SI units. See also Converter#register_si_unit.
9
+ SI_PREFIXES = {
10
+ 'yotta' => {:abbrev => 'Y', :multiplier => 1e24},
11
+ 'zetta' => {:abbrev => 'Z', :multiplier => 1e21},
12
+ 'exa' => {:abbrev => 'E', :multiplier => 1e18},
13
+ 'peta' => {:abbrev => 'P', :multiplier => 1e14},
14
+ 'tera' => {:abbrev => 'T', :multiplier => 1e12},
15
+ 'giga' => {:abbrev => 'G', :multiplier => 1e9},
16
+ 'mega' => {:abbrev => 'M', :multiplier => 1e6},
17
+ 'kilo' => {:abbrev => 'k', :multiplier => 1e3},
18
+ 'hecto' => {:abbrev => 'h', :multiplier => 1e2},
19
+ 'deca' => {:abbrev => 'da', :multiplier => 1e1},
20
+ 'deci' => {:abbrev => 'd', :multiplier => 1e-1},
21
+ 'centi' => {:abbrev => 'c', :multiplier => 1e-2},
22
+ 'milli' => {:abbrev => 'm', :multiplier => 1e-3},
23
+ 'micro' => {:abbrev => 'u', :multiplier => 1e-6},
24
+ 'nano' => {:abbrev => 'n', :multiplier => 1e-9},
25
+ 'pico' => {:abbrev => 'p', :multiplier => 1e-12},
26
+ 'femto' => {:abbrev => 'f', :multiplier => 1e-15},
27
+ 'atto' => {:abbrev => 'a', :multiplier => 1e-18},
28
+ 'zepto' => {:abbrev => 'z', :multiplier => 1e-21},
29
+ 'yocto' => {:abbrev => 'y', :multiplier => 1e-24}
30
+ }
31
+
32
+ handles 'si_unit'
33
+
34
+ def si_unit(converter, name, *args)
35
+ converter.send(:register_prefixed_unit, name, SI_PREFIXES, *args)
36
+ end
37
+
38
+ end
39
+
40
+ class LengthLoader < Loader
41
+
42
+ # The prefixes used for length units. See also Converter#register_length_unit.
43
+ LENGTH_PREFIXES = {
44
+ 'square_' => {:abbrev => 'sq_', :power => 2},
45
+ 'cubic_' => {:abbrev => 'cu_', :power => 3}
46
+ }
47
+
48
+ handles 'length_unit'
49
+
50
+ def length_unit(converter, name, *args)
51
+ converter.send(:register_prefixed_unit, name, LENGTH_PREFIXES, *args)
52
+ end
53
+
54
+ end
55
+
56
+ class BinaryLoader < Loader
57
+
58
+ # The prefixes used for binary units. See also Converter#register_binary_unit.
59
+ BINARY_PREFIXES = {
60
+ 'yotta' => {:abbrev => 'Y', :multiplier => 1024.0 ** 8},
61
+ 'zetta' => {:abbrev => 'Z', :multiplier => 1024.0 ** 7},
62
+ 'exa' => {:abbrev => 'E', :multiplier => 1024.0 ** 6},
63
+ 'peta' => {:abbrev => 'P', :multiplier => 1024.0 ** 5},
64
+ 'tera' => {:abbrev => 'T', :multiplier => 1024.0 ** 4},
65
+ 'giga' => {:abbrev => 'G', :multiplier => 1024.0 ** 3},
66
+ 'mega' => {:abbrev => 'M', :multiplier => 1024.0 ** 2},
67
+ 'kilo' => {:abbrev => 'k', :multiplier => 1024.0},
68
+ }
69
+
70
+ handles 'binary_unit'
71
+
72
+ def binary_unit(converter, name, *args)
73
+ converter.send(:register_prefixed_unit, name, BINARY_PREFIXES, *args)
74
+ end
75
+
76
+ end
77
+
78
+ class IECBinaryLoader < Loader
79
+
80
+ # The prefixes used for binary units in the IEC system.
81
+ # See also Converter#register_binary_iec_unit.
82
+ BINARY_IEC_PREFIXES = {
83
+ 'yobi' => {:abbrev => 'Yi', :multiplier => 1024.0 ** 8},
84
+ 'zebi' => {:abbrev => 'Zi', :multiplier => 1024.0 ** 7},
85
+ 'exbi' => {:abbrev => 'Ei', :multiplier => 1024.0 ** 6},
86
+ 'pebi' => {:abbrev => 'Pi', :multiplier => 1024.0 ** 5},
87
+ 'tebi' => {:abbrev => 'Ti', :multiplier => 1024.0 ** 4},
88
+ 'gibi' => {:abbrev => 'Gi', :multiplier => 1024.0 ** 3},
89
+ 'mebi' => {:abbrev => 'Mi', :multiplier => 1024.0 ** 2},
90
+ 'kibi' => {:abbrev => 'ki', :multiplier => 1024.0},
91
+
92
+ 'yotta' => {:abbrev => 'Y', :multiplier => 1e24},
93
+ 'zetta' => {:abbrev => 'Z', :multiplier => 1e21},
94
+ 'exa' => {:abbrev => 'E', :multiplier => 1e18},
95
+ 'peta' => {:abbrev => 'P', :multiplier => 1e14},
96
+ 'tera' => {:abbrev => 'T', :multiplier => 1e12},
97
+ 'giga' => {:abbrev => 'G', :multiplier => 1e9},
98
+ 'mega' => {:abbrev => 'M', :multiplier => 1e6},
99
+ 'kilo' => {:abbrev => 'k', :multiplier => 1e3},
100
+ }
101
+
102
+ handles 'binary_iec_unit'
103
+
104
+ def binary_iec_unit(converter, name, *args)
105
+ converter.send(:register_prefixed_unit, name, BINARY_IEC_PREFIXES, *args)
106
+ end
107
+
108
+ end
109
+
110
+ end
111
+ end