stick 1.2.0

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.
Files changed (56) hide show
  1. data/CHANGES +7 -0
  2. data/COPYING +344 -0
  3. data/README +110 -0
  4. data/lib/stick/constants.rb +3 -0
  5. data/lib/stick/constants/cgs.rb +151 -0
  6. data/lib/stick/constants/mks.rb +158 -0
  7. data/lib/stick/constants/number.rb +33 -0
  8. data/lib/stick/constants/typeless_cgs.rb +141 -0
  9. data/lib/stick/constants/typeless_mks.rb +142 -0
  10. data/lib/stick/currency.rb +8 -0
  11. data/lib/stick/mapcar.rb +61 -0
  12. data/lib/stick/matrix.rb +1022 -0
  13. data/lib/stick/quaternion.rb +562 -0
  14. data/lib/stick/times.rb +441 -0
  15. data/lib/stick/units.rb +112 -0
  16. data/lib/stick/units/base.rb +980 -0
  17. data/lib/stick/units/currency.rb +159 -0
  18. data/lib/stick/units/data/binary/base.rb +4 -0
  19. data/lib/stick/units/data/cex.rb +5 -0
  20. data/lib/stick/units/data/currency-default.rb +5 -0
  21. data/lib/stick/units/data/currency-standard.rb +2 -0
  22. data/lib/stick/units/data/currency/base.rb +89 -0
  23. data/lib/stick/units/data/iec.rb +5 -0
  24. data/lib/stick/units/data/iec_binary/base.rb +6 -0
  25. data/lib/stick/units/data/si.rb +7 -0
  26. data/lib/stick/units/data/si/base.rb +9 -0
  27. data/lib/stick/units/data/si/derived.rb +26 -0
  28. data/lib/stick/units/data/si/extra.rb +22 -0
  29. data/lib/stick/units/data/uk.rb +10 -0
  30. data/lib/stick/units/data/uk/base.rb +22 -0
  31. data/lib/stick/units/data/units-default.rb +11 -0
  32. data/lib/stick/units/data/units-standard.rb +5 -0
  33. data/lib/stick/units/data/us.rb +10 -0
  34. data/lib/stick/units/data/us/base.rb +23 -0
  35. data/lib/stick/units/data/xmethods.rb +5 -0
  36. data/lib/stick/units/data/xmethods/cached.rb +84 -0
  37. data/lib/stick/units/data/xmethods/mapping.rb +87 -0
  38. data/lib/stick/units/loaders.rb +98 -0
  39. data/lib/stick/units/units.rb +109 -0
  40. data/meta/MANIFEST +76 -0
  41. data/meta/ROLLRC +2 -0
  42. data/meta/icli.yaml +16 -0
  43. data/meta/project.yaml +18 -0
  44. data/task/clobber/package +10 -0
  45. data/task/publish +57 -0
  46. data/task/release +10 -0
  47. data/task/setup +1616 -0
  48. data/task/test +25 -0
  49. data/test/spec_matrix.rb +342 -0
  50. data/test/test_currency.rb +26 -0
  51. data/test/test_matrix.rb +359 -0
  52. data/test/test_units.rb +205 -0
  53. data/work/TODO +20 -0
  54. data/work/bytes.rb +231 -0
  55. data/work/multipliers.rb +195 -0
  56. metadata +138 -0
@@ -0,0 +1,159 @@
1
+ require 'stick/units/base'
2
+
3
+ require 'soap/wsdlDriver'
4
+
5
+ module Stick
6
+
7
+ class CurrencyLoader < Loader
8
+
9
+ handles 'ce_service', 'currency_unit'
10
+
11
+ def ce_service(converter, name, &blk)
12
+ old_service = Thread.current[:'Stick::ce_service']
13
+ Thread.current[:'Stick::ce_service'] = ::Stick::Converter::ExchangeRate.const_get(name)
14
+ yield
15
+ ensure
16
+ Thread.current[:'Stick::ce_service'] = old_service
17
+ end
18
+
19
+ def currency_unit(converter, name)
20
+ service = Thread.current[:'Stick::ce_service'] || ::Stick::Config::DEFAULT_CURRENCY_SERVICE
21
+ converter.send(:register_unit, name, :equals => service.create_conversion(name, converter))
22
+ end
23
+
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 Stick::Converter::ExchangeRate.
40
+ class ExchangeRate
41
+
42
+ def self.create_conversion(curr, converter) # :nodoc:
43
+ {:unit => ::Stick::Unit.new({:'--base-currency--' => 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(::Stick::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(::Stick::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--' ? :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 = ::Stick::Converter::ExchangeRate::XMethods
157
+ end
158
+
159
+ end
@@ -0,0 +1,4 @@
1
+ converter 'binary_base' do
2
+ binary_unit 'bit', :abbrev => 'b', :alias => 'bits'
3
+ binary_unit 'byte', :abbrev => 'B', :alias => 'bytes', :equals => '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', :abbrev => 'b', :alias => 'bits', :equals => 'binary_base:b'
5
+ binary_iec_unit 'byte', :abbrev => 'B', :alias => 'bytes', :equals => '8.0 b'
6
+ end
@@ -0,0 +1,7 @@
1
+ require 'si/extra'
2
+
3
+ converter 'si' do
4
+ include 'si_base'
5
+ include 'si_derived'
6
+ include 'si_extra'
7
+ end
@@ -0,0 +1,9 @@
1
+ converter 'si_base' do
2
+ si_unit 'meter', :abbrev => 'm', :alias => 'meters'
3
+ si_unit 'gram', :abbrev => 'g', :alias => 'grams'
4
+ si_unit 'second', :abbrev => 's', :alias => 'seconds'
5
+ si_unit 'ampere', :abbrev => 'A', :alias => 'amperes'
6
+ si_unit 'kelvin', :abbrev => 'K', :alias => 'kelvins'
7
+ si_unit 'mole', :abbrev => 'mol', :alias => 'moles'
8
+ si_unit 'candela', :abbrev => 'cd', :alias => 'candelas'
9
+ end
@@ -0,0 +1,26 @@
1
+ require 'si/base'
2
+
3
+ converter 'si_derived' do
4
+ si_unit "radian", :abbrev => "rad", :equals => "", :alias => "radians"
5
+ si_unit "steradian", :abbrev => "sr", :equals => "", :alias => "steradians"
6
+ si_unit "hertz", :abbrev => "Hz", :equals => "1 / si_base:s"
7
+ si_unit "newton", :abbrev => "N", :equals => "si_base:kg si_base:m / si_base:s**2", :alias => "newtons"
8
+ si_unit "pascal", :abbrev => "Pa", :equals => "N / si_base:m**2", :alias => "pascals"
9
+ si_unit "joule", :abbrev => "J", :equals => "N * si_base:m", :alias => "joules"
10
+ si_unit "watt", :abbrev => "W", :equals => "J / si_base:s", :alias => "watts"
11
+ si_unit "coulomb", :abbrev => "C", :equals => "si_base:A * si_base:s", :alias => "coulombs"
12
+ si_unit "volt", :abbrev => "V", :equals => "W / si_base:A", :alias => "volts"
13
+ si_unit "farad", :abbrev => "F", :equals => "C / V", :alias => "farads"
14
+ si_unit "ohm", :equals => "V / si_base:A", :alias => "ohms"
15
+ si_unit "siemens", :abbrev => "S", :equals => "si_base:A / V"
16
+ si_unit "weber", :abbrev => "Wb", :equals => "V * si_base:s", :alias => "webers"
17
+ si_unit "tesla", :abbrev => "T", :equals => "Wb / si_base:m**2", :alias => "teslas"
18
+ si_unit "henry", :abbrev => "H", :equals => "Wb / si_base:A", :alias => "henries"
19
+ si_unit "lumen", :abbrev => "lm", :equals => "si_base:cd sr", :alias => "lumens"
20
+ si_unit "lux", :abbrev => "lx", :equals => "lm / si_base:m**2", :alias => "luxen"
21
+ si_unit "becquerel", :abbrev => "Bq", :equals => "1 / si_base:s", :alias => "becquerels"
22
+ si_unit "gray", :abbrev => "Gy", :equals => "J / si_base:kg", :alias => "grays"
23
+ si_unit "sievert", :abbrev => "Sv", :equals => "J / si_base:kg", :alias => "sieverts"
24
+ si_unit "katal", :abbrev => "kat", :equals => "si_base:mol / si_base:s", :alias => "katals"
25
+ si_unit "bar", :abbrev => "bar", :equals => "100.0 kPa", :alias => "bars"
26
+ end
@@ -0,0 +1,22 @@
1
+ require 'si/derived'
2
+
3
+ converter "si_extra" do
4
+ unit "minute", :abbrev => "min", :equals => "60.0 si_base:s", :alias => "minutes"
5
+ unit "hour", :abbrev => "h", :equals => "60.0 min", :alias => "hours"
6
+ unit "day", :abbrev => "d", :equals => "24.0 h", :alias => "days"
7
+ unit "liter", :abbrev => "L", :equals => "si_base:dm**3", :alias => "liters"
8
+ unit "neper", :abbrev => "Np", :equals => "", :alias => "nepers"
9
+ unit "electronvolt", :abbrev => "eV", :equals => "1.60218e-19 si_derived:J", :alias => "electronvolts"
10
+ unit "are", :abbrev => "a", :equals => "si_base:dam**2", :alias => "ares"
11
+ unit "hectare", :abbrev => "ha", :equals => "si_base:hm**2", :alias => "hectares"
12
+ unit "barn", :equals => "100.0 si_base:fm**2", :alias => "barns"
13
+ unit "curie", :abbrev => "Ci", :equals => "3.7e10 si_derived:Bq", :alias => "curies"
14
+ unit "roentgen", :abbrev => "R", :equals => "2.58e-4 si_base:kg", :alias => "roentgens"
15
+ unit "metric_ton", :abbrev => "t", :equals => "1000.0 si_base:kg", :alias => "metric_tons"
16
+ unit "degree", :equals => "0.0174532925199433 si_derived:rad", :alias => "degrees"
17
+ unit "nautical_mile", :equals => "1852.0 si_base:m", :alias => "nautical_miles"
18
+ unit "knot", :equals => "nautical_mile / h", :alias => "knots"
19
+ unit "angstrom", :equals => "100.0 si_derived:kPa", :alias => "angstroms"
20
+ unit "unified_atomic_mass_unit", :abbrev => "u", :equals => "1.66054e-27 si_base:kg", :alias => "unified_atomic_mass_units"
21
+ unit "astronomical_unit", :abbrev => "ua", :equals => "1.49598e11 si_base:m", :alias => "astronomical_units"
22
+ end
@@ -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,22 @@
1
+ require 'si/base'
2
+
3
+ converter 'uk_base' do
4
+ length_unit "inch", :abbrev => "in", :equals => "2.54 si_base:cm", :alias => "inches"
5
+ length_unit "foot", :abbrev => "ft", :equals => "12.0 in", :alias => "feet"
6
+ length_unit "yard", :abbrev => "yd", :equals => "3.0 ft", :alias => "yards"
7
+ length_unit "chain", :abbrev => nil, :equals => "22.0 yd", :alias => "chains"
8
+ length_unit "furlong", :abbrev => nil, :equals => "10.0 chains", :alias => "furlongs"
9
+ length_unit "mile", :abbrev => "mi", :equals => "8.0 furlongs", :alias => "miles"
10
+ unit "acre", :abbrev => nil, :equals => "10 square_chain", :alias => "acres"
11
+ unit "fluid_ounce", :abbrev => "fl_oz", :equals => "2.8413062e-2 si_base:dm**3", :alias => "fluid_ounces"
12
+ unit "gill", :abbrev => nil, :equals => "5.0 fl_oz", :alias => "gills"
13
+ unit "pint", :abbrev => "pt", :equals => "4.0 gills", :alias => "pints"
14
+ unit "quart", :abbrev => "qt", :equals => "2.0 pt", :alias => "quarts"
15
+ unit "gallon", :abbrev => "gal", :equals => "4.0 qt", :alias => "gallons"
16
+ unit "grain", :abbrev => "gr", :equals => "6.479891e-5 si_base:kg", :alias => "grains"
17
+ unit "ounce", :abbrev => "oz", :equals => "437.5 gr", :alias => "ounces"
18
+ unit "pound", :abbrev => "lb", :equals => "16.0 oz", :alias => "pounds"
19
+ unit "stone", :abbrev => nil, :equals => "14.0 lb", :alias => "stones"
20
+ unit "hundredweight", :abbrev => "cwt", :equals => "8.0 stone", :alias => "hundredweights"
21
+ unit "long_ton", :abbrev => "lt", :equals => "20.0 cwt", :alias => "long_tons"
22
+ end
@@ -0,0 +1,11 @@
1
+ require 'si/extra'
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 'us_base'
10
+ include 'binary_base'
11
+ 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,23 @@
1
+ require 'si/base'
2
+
3
+ converter 'us_base' do
4
+ length_unit "inch", :abbrev => "in", :equals => "2.54 si_base:cm", :alias => "inches"
5
+ length_unit "foot", :abbrev => "ft", :equals => "12.0 in", :alias => "feet"
6
+ length_unit "yard", :abbrev => "yd", :equals => "3.0 ft", :alias => "yards"
7
+ length_unit "furlong", :equals => "220.0 yd", :alias => "furlongs"
8
+ length_unit "mile", :abbrev => "mi", :equals => "8.0 furlongs", :alias => "miles"
9
+ length_unit "acre", :equals => "4840.0 sq_yd", :alias => "acres"
10
+ unit "section", :equals => "1.0 sq_mi", :alias => "sections"
11
+ unit "township", :equals => "36.0 sections", :alias => "townships"
12
+ unit "fluid_ounce", :abbrev => "fl_oz", :equals => "2.95735295625e-2 si_base:dm**3", :alias => "fluid_ounces"
13
+ unit "gill", :equals => "4.0 fl_oz", :alias => "gills"
14
+ unit "pint", :abbrev => "pt", :equals => "4.0 gills", :alias => "pints"
15
+ unit "quart", :abbrev => "qt", :equals => "2.0 pt", :alias => "quarts"
16
+ unit "gallon", :abbrev => "gal", :equals => "4.0 qt", :alias => "gallons"
17
+ unit "grain", :abbrev => "gr", :equals => "6.479891e-5 si_base:kg", :alias => "grains"
18
+ unit "ounce", :abbrev => "oz", :equals => "437.5 gr", :alias => "ounces"
19
+ unit "pound", :abbrev => "lb", :equals => "16.0 oz", :alias => "pounds"
20
+ unit "stone", :equals => "14.0 lb", :alias => "stones"
21
+ unit "hundredweight", :abbrev => "cwt", :equals => "100.0 lb", :alias => "hundredweights"
22
+ unit "short_ton", :abbrev => "st", :equals => "20.0 cwt", :alias => "short_tons"
23
+ end