acvwilson-acvwilson-currency 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/COPYING.txt +339 -0
  2. data/ChangeLog +8 -0
  3. data/LICENSE.txt +65 -0
  4. data/Manifest.txt +58 -0
  5. data/README.txt +51 -0
  6. data/Releases.txt +155 -0
  7. data/TODO.txt +9 -0
  8. data/currency.gemspec +18 -0
  9. data/examples/ex1.rb +13 -0
  10. data/examples/xe1.rb +20 -0
  11. data/lib/currency.rb +143 -0
  12. data/lib/currency/active_record.rb +265 -0
  13. data/lib/currency/config.rb +91 -0
  14. data/lib/currency/core_extensions.rb +48 -0
  15. data/lib/currency/currency.rb +175 -0
  16. data/lib/currency/currency/factory.rb +121 -0
  17. data/lib/currency/currency_version.rb +6 -0
  18. data/lib/currency/exception.rb +119 -0
  19. data/lib/currency/exchange.rb +50 -0
  20. data/lib/currency/exchange/rate.rb +214 -0
  21. data/lib/currency/exchange/rate/deriver.rb +157 -0
  22. data/lib/currency/exchange/rate/source.rb +89 -0
  23. data/lib/currency/exchange/rate/source/base.rb +166 -0
  24. data/lib/currency/exchange/rate/source/failover.rb +63 -0
  25. data/lib/currency/exchange/rate/source/federal_reserve.rb +160 -0
  26. data/lib/currency/exchange/rate/source/historical.rb +79 -0
  27. data/lib/currency/exchange/rate/source/historical/rate.rb +184 -0
  28. data/lib/currency/exchange/rate/source/historical/rate_loader.rb +186 -0
  29. data/lib/currency/exchange/rate/source/historical/writer.rb +220 -0
  30. data/lib/currency/exchange/rate/source/new_york_fed.rb +127 -0
  31. data/lib/currency/exchange/rate/source/provider.rb +120 -0
  32. data/lib/currency/exchange/rate/source/test.rb +50 -0
  33. data/lib/currency/exchange/rate/source/the_financials.rb +191 -0
  34. data/lib/currency/exchange/rate/source/timed_cache.rb +198 -0
  35. data/lib/currency/exchange/rate/source/xe.rb +165 -0
  36. data/lib/currency/exchange/time_quantitizer.rb +111 -0
  37. data/lib/currency/formatter.rb +300 -0
  38. data/lib/currency/macro.rb +321 -0
  39. data/lib/currency/money.rb +296 -0
  40. data/lib/currency/money_helper.rb +13 -0
  41. data/lib/currency/parser.rb +193 -0
  42. data/spec/ar_base_spec.rb +140 -0
  43. data/spec/ar_column_spec.rb +69 -0
  44. data/spec/ar_core_spec.rb +64 -0
  45. data/spec/ar_simple_spec.rb +31 -0
  46. data/spec/config_spec.rb +29 -0
  47. data/spec/federal_reserve_spec.rb +75 -0
  48. data/spec/formatter_spec.rb +72 -0
  49. data/spec/historical_writer_spec.rb +187 -0
  50. data/spec/macro_spec.rb +109 -0
  51. data/spec/money_spec.rb +347 -0
  52. data/spec/new_york_fed_spec.rb +73 -0
  53. data/spec/parser_spec.rb +105 -0
  54. data/spec/spec_helper.rb +25 -0
  55. data/spec/time_quantitizer_spec.rb +115 -0
  56. data/spec/timed_cache_spec.rb +95 -0
  57. data/spec/xe_spec.rb +50 -0
  58. metadata +117 -0
@@ -0,0 +1,50 @@
1
+ # Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
2
+ # See LICENSE.txt for details.
3
+
4
+ require 'currency'
5
+
6
+ # The Currency::Exchange package is responsible for
7
+ # the buying and selling of currencies.
8
+ #
9
+ # This feature is currently unimplemented.
10
+ #
11
+ # Exchange rate sources are configured via Currency::Exchange::Rate::Source.
12
+ #
13
+ module Currency::Exchange
14
+ @@default = nil
15
+ @@current = nil
16
+
17
+ # Returns the default Currency::Exchange object.
18
+ #
19
+ # If one is not specfied an instance of Currency::Exchange::Base is
20
+ # created. Currency::Exchange::Base cannot service any
21
+ # conversion rate requests.
22
+ def self.default
23
+ @@default ||= raise :Currency::Exception::Unimplemented, :default
24
+ end
25
+
26
+ # Sets the default Currency::Exchange object.
27
+ def self.default=(x)
28
+ @@default = x
29
+ end
30
+
31
+ # Returns the current Currency::Exchange object used during
32
+ # explicit and implicit Money trading.
33
+ #
34
+ # If #current= has not been called and #default= has not been called,
35
+ # then UndefinedExchange is raised.
36
+ def self.current
37
+ @@current || self.default || (raise ::Currency::Exception::UndefinedExchange, "Currency::Exchange.current not defined")
38
+ end
39
+
40
+ # Sets the current Currency::Exchange object used during
41
+ # explicit and implicit Money conversions.
42
+ def self.current=(x)
43
+ @@current = x
44
+ end
45
+
46
+ end # module
47
+
48
+ require 'currency/exchange/rate'
49
+ require 'currency/exchange/rate/source'
50
+
@@ -0,0 +1,214 @@
1
+ # Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
2
+ # See LICENSE.txt for details.
3
+
4
+ require 'currency/exchange'
5
+
6
+ # Represents a convertion rate between two currencies at a given period of time.
7
+ class Currency::Exchange::Rate
8
+ # The first Currency.
9
+ attr_reader :c1
10
+
11
+ # The second Currency.
12
+ attr_reader :c2
13
+
14
+ # The rate between c1 and c2.
15
+ #
16
+ # To convert between m1 in c1 and m2 in c2:
17
+ #
18
+ # m2 = m1 * rate.
19
+ attr_reader :rate
20
+
21
+ # The source of the rate.
22
+ attr_reader :source
23
+
24
+ # The Time of the rate.
25
+ attr_reader :date
26
+
27
+ # If the rate is derived from other rates, this describes from where it was derived.
28
+ attr_reader :derived
29
+
30
+ # Average rate over rate samples in a time range.
31
+ attr_reader :rate_avg
32
+
33
+ # Number of rate samples used to calcuate _rate_avg_.
34
+ attr_reader :rate_samples
35
+
36
+ # The rate low between date_0 and date_1.
37
+ attr_reader :rate_lo
38
+
39
+ # The rate high between date_0 and date_1.
40
+ attr_reader :rate_hi
41
+
42
+ # The rate at date_0.
43
+ attr_reader :rate_date_0
44
+
45
+ # The rate at date_1.
46
+ attr_reader :rate_date_1
47
+
48
+ # The lowest date of sampled rates.
49
+ attr_reader :date_0
50
+
51
+ # The highest date of sampled rates.
52
+ # This is non-inclusive during searches to allow seamless tileings of
53
+ # time with rate buckets.
54
+ attr_reader :date_1
55
+
56
+ def initialize(c1, c2, c1_to_c2_rate, source = "UNKNOWN", date = nil, derived = nil, reciprocal = nil, opts = nil)
57
+ @c1 = c1
58
+ @c2 = c2
59
+ @rate = c1_to_c2_rate
60
+ raise ::Currency::Exception::InvalidRate,
61
+ [
62
+ "rate is not positive",
63
+ :rate, @rate,
64
+ :c1, c1,
65
+ :c2, c2,
66
+ ] unless @rate && @rate >= 0.0
67
+ @source = source
68
+ @date = date
69
+ @derived = derived
70
+ @reciprocal = reciprocal
71
+
72
+ #
73
+ @rate_avg =
74
+ @rate_samples =
75
+ @rate_lo =
76
+ @rate_hi =
77
+ @rate_date_0 =
78
+ @rate_date_1 =
79
+ @date_0 =
80
+ @date_1 =
81
+ nil
82
+
83
+ if opts
84
+ opts.each_pair do | k, v |
85
+ self.instance_variable_set("@#{k}", v)
86
+ end
87
+ end
88
+ end
89
+
90
+
91
+ # Returns a cached reciprocal Rate object from c2 to c1.
92
+ def reciprocal
93
+ @reciprocal ||= @rate == 1.0 ? self :
94
+ self.class.new(@c2, @c1,
95
+ 1.0 / @rate,
96
+ @source,
97
+ @date,
98
+ "reciprocal(#{derived || "#{c1.code}#{c2.code}"})", self,
99
+ {
100
+ :rate_avg => @rate_avg && 1.0 / @rate_avg,
101
+ :rate_samples => @rate_samples,
102
+ :rate_lo => @rate_lo && 1.0 / @rate_lo,
103
+ :rate_hi => @rate_hi && 1.0 / @rate_hi,
104
+ :rate_date_0 => @rate_date_0 && 1.0 / @rate_date_0,
105
+ :rate_date_1 => @rate_date_1 && 1.0 / @rate_date_1,
106
+ :date_0 => @date_0,
107
+ :date_1 => @date_1,
108
+ }
109
+ )
110
+ end
111
+
112
+
113
+ # Converts from _m_ in Currency _c1_ to the opposite currency.
114
+ def convert(m, c1)
115
+ m = m.to_f
116
+ if @c1 == c1
117
+ # $stderr.puts "Converting #{@c1} #{m} to #{@c2} #{m * @rate} using #{@rate}"
118
+ m * @rate
119
+ else
120
+ # $stderr.puts "Converting #{@c2} #{m} to #{@c1} #{m / @rate} using #{1.0 / @rate}; recip"
121
+ m / @rate
122
+ end
123
+ end
124
+
125
+
126
+ # Collect rate samples into rate_avg, rate_hi, rate_lo, rate_date_0, rate_date_1, date_0, date_1.
127
+ def collect_rates(rates)
128
+ rates = [ rates ] unless rates.kind_of?(Enumerable)
129
+ rates.each do | r |
130
+ collect_rate(r)
131
+ end
132
+ self
133
+ end
134
+
135
+ # Collect rates samples in to this Rate.
136
+ def collect_rate(rate)
137
+ # Initial.
138
+ @rate_samples ||= 0
139
+ @rate_sum ||= 0
140
+ @source ||= rate.source
141
+ @c1 ||= rate.c1
142
+ @c2 ||= rate.c2
143
+
144
+ # Reciprocal?
145
+ if @c1 == rate.c2 && @c2 == rate.c1
146
+ collect_rate(rate.reciprocal)
147
+ elsif ! (@c1 == rate.c1 && @c2 == rate.c2)
148
+ raise ::Currency::Exception::InvalidRate,
149
+ [
150
+ "Cannot collect rates between different currency pairs",
151
+ :rate1, self,
152
+ :rate2, rate,
153
+ ]
154
+ else
155
+ # Multisource?
156
+ @source = "<<multiple-sources>>" unless @source == rate.source
157
+
158
+ # Calculate rate average.
159
+ @rate_samples += 1
160
+ @rate_sum += rate.rate || (rate.rate_lo + rate.rate_hi) * 0.5
161
+ @rate_avg = @rate_sum / @rate_samples
162
+
163
+ # Calculate rates ranges.
164
+ r = rate.rate_lo || rate.rate
165
+ unless @rate_lo && @rate_lo < r
166
+ @rate_lo = r
167
+ end
168
+ r = rate.rate_hi || rate.rate
169
+ unless @rate_hi && @rate_hi > r
170
+ @rate_hi = r
171
+ end
172
+
173
+ # Calculate rates on date range boundaries
174
+ r = rate.rate_date_0 || rate.rate
175
+ d = rate.date_0 || rate.date
176
+ unless @date_0 && @date_0 < d
177
+ @date_0 = d
178
+ @rate_date_0 = r
179
+ end
180
+
181
+ r = rate.rate_date_1 || rate.rate
182
+ d = rate.date_1 || rate.date
183
+ unless @date_1 && @date_1 > d
184
+ @date_1 = d
185
+ @rate_date_1 = r
186
+ end
187
+
188
+ @date ||= rate.date || rate.date0 || rate.date1
189
+ end
190
+ self
191
+ end
192
+
193
+
194
+ def to_s(extended = false)
195
+ extended = "#{date_0} #{rate_date_0} |< #{rate_lo} #{rate} #{rate_hi} >| #{rate_date_1} #{date_1}" if extended
196
+ extended ||= ''
197
+ "#<#{self.class.name} #{c1.code} #{c2.code} #{rate} #{source.inspect} #{date ? date.strftime('%Y/%m/%d-%H:%M:%S') : 'nil'} #{derived && derived.inspect} #{extended}>"
198
+ end
199
+
200
+ def inspect; to_s; end
201
+
202
+ end # class
203
+
204
+
205
+ class Currency::Exchange::Rate::Writable < Currency::Exchange::Rate
206
+ attr_writer :source
207
+ attr_writer :derived
208
+ attr_writer :rate
209
+ def initialize(c1 = nil, c2 = nil, rate = nil, *opts)
210
+ super(c1, c2, 0.0, *opts)
211
+ @rate = rate
212
+ end
213
+ end # class
214
+
@@ -0,0 +1,157 @@
1
+ # Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
2
+ # See LICENSE.txt for details.
3
+
4
+ require 'currency/exchange/rate'
5
+ require 'currency/exchange/rate/source/base'
6
+
7
+ # The Currency::Exchange::Rate::Deriver class calculates derived rates
8
+ # from base Rates from a rate Source by pivoting against a pivot currency or by
9
+ # generating reciprocals.
10
+ #
11
+ class Currency::Exchange::Rate::Deriver < Currency::Exchange::Rate::Source::Base
12
+
13
+ # The source for base rates.
14
+ attr_accessor :source
15
+
16
+
17
+ def name
18
+ source.name
19
+ end
20
+
21
+
22
+ def initialize(opt = { })
23
+ @source = nil
24
+ @pivot_currency = nil
25
+ @derived_rates = { }
26
+ @all_rates = { }
27
+ super
28
+ end
29
+
30
+
31
+ def pivot_currency
32
+ @pivot_currency || @source.pivot_currency || :USD
33
+ end
34
+
35
+
36
+ # Return all currencies.
37
+ def currencies
38
+ @source.currencies
39
+ end
40
+
41
+
42
+ # Flush all cached Rates.
43
+ def clear_rates
44
+ @derived_rates.clear
45
+ @all_rates.clear
46
+ @source.clear_rates
47
+ super
48
+ end
49
+
50
+
51
+ # Returns all combinations of rates except identity rates.
52
+ def rates(time = nil)
53
+ time = time && normalize_time(time)
54
+ all_rates(time)
55
+ end
56
+
57
+
58
+ # Computes all rates.
59
+ # time is assumed to be normalized.
60
+ def all_rates(time = nil)
61
+ if x = @all_rates["#{time}"]
62
+ return x
63
+ end
64
+
65
+ x = @all_rates["#{time}"] = [ ]
66
+
67
+ currencies = self.currencies
68
+
69
+ currencies.each do | c1 |
70
+ currencies.each do | c2 |
71
+ next if c1 == c2
72
+ c1 = ::Currency::Currency.get(c1)
73
+ c2 = ::Currency::Currency.get(c2)
74
+ rate = rate(c1, c2, time)
75
+ x << rate
76
+ end
77
+ end
78
+
79
+ x
80
+ end
81
+
82
+
83
+ # Determines and creates the Rate between Currency c1 and c2.
84
+ #
85
+ # May attempt to use a pivot currency to bridge between
86
+ # rates.
87
+ #
88
+ def get_rate(c1, c2, time)
89
+ rate = get_rate_reciprocal(c1, c2, time)
90
+
91
+ # Attempt to use pivot_currency to bridge
92
+ # between Rates.
93
+ unless rate
94
+ pc = ::Currency::Currency.get(pivot_currency)
95
+
96
+ if pc &&
97
+ (rate_1 = get_rate_reciprocal(c1, pc, time)) &&
98
+ (rate_2 = get_rate_reciprocal(pc, c2, time))
99
+ c1_to_c2_rate = rate_1.rate * rate_2.rate
100
+ rate = new_rate(c1, c2,
101
+ c1_to_c2_rate,
102
+ rate_1.date || rate_2.date || time,
103
+ "pivot(#{pc.code},#{rate_1.derived || "#{rate_1.c1.code}#{rate_1.c2.code}"},#{rate_2.derived || "#{rate_2.c1}#{rate_2.c2}"})")
104
+ end
105
+ end
106
+
107
+ rate
108
+ end
109
+
110
+
111
+ # Get a matching base rate or its reciprocal.
112
+ def get_rate_reciprocal(c1, c2, time)
113
+ rate = get_rate_base_cached(c1, c2, time)
114
+ unless rate
115
+ if rate = get_rate_base_cached(c2, c1, time)
116
+ rate = (@rate["#{c1}:#{c2}:#{time}"] ||= rate.reciprocal)
117
+ end
118
+ end
119
+
120
+ rate
121
+ end
122
+
123
+
124
+ # Returns a cached base Rate.
125
+ #
126
+ def get_rate_base_cached(c1, c2, time)
127
+ rate = (@rate["#{c1}:#{c2}:#{time}"] ||= get_rate_base(c1, c2, time))
128
+ rate
129
+ end
130
+
131
+
132
+ # Returns a base Rate from the Source.
133
+ def get_rate_base(c1, c2, time)
134
+ if c1 == c2
135
+ # Identity rates are timeless.
136
+ new_rate(c1, c2, 1.0, nil, "identity")
137
+ else
138
+ source.rate(c1, c2, time)
139
+ end
140
+ end
141
+
142
+
143
+ def load_rates(time = nil)
144
+ all_rates(time)
145
+ end
146
+
147
+
148
+ # Returns true if the underlying rate provider is available.
149
+ def available?(time = nil)
150
+ source.available?(time)
151
+ end
152
+
153
+
154
+ end # class
155
+
156
+
157
+
@@ -0,0 +1,89 @@
1
+ # Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
2
+ # See LICENSE.txt for details.
3
+
4
+ require 'currency/exchange/rate'
5
+
6
+ #
7
+ # The Currency::Exchange::Rate::Source package is responsible for
8
+ # providing rates between currencies at a given time.
9
+ #
10
+ # It is not responsible for purchasing or selling actual money.
11
+ # See Currency::Exchange.
12
+ #
13
+ # Currency::Exchange::Rate::Source::Provider subclasses are true rate data
14
+ # providers. See the #load_rates method. They provide groups of rates
15
+ # at a given time.
16
+ #
17
+ # Other Currency::Exchange::Rate::Source::Base subclasses
18
+ # are chained to provide additional rate source behavior,
19
+ # such as caching and derived rates. They provide individual rates between
20
+ # currencies at a given time. See the #rate method. An application
21
+ # will interface directly to a Currency::Exchange::Rate::Source::Base.
22
+ # A rate aggregator like Currency::Exchange::Rate::Historical::Writer will
23
+ # interface directly to a Currency::Exchange::Rate::Source::Provider.
24
+ #
25
+ # == IMPORTANT
26
+ #
27
+ # Rates sources should *never* install themselves
28
+ # as a Currency::Exchange::Rate::Source.current or
29
+ # Currency::Exchange::Rate::Source.default. The application itself is
30
+ # responsible setting up the default rate source.
31
+ # The old auto-installation behavior of rate sources,
32
+ # like Currency::Exchange::Xe, is no longer supported.
33
+ #
34
+ # == Initialization of Rate Sources
35
+ #
36
+ # A typical application will use the following rate source chain:
37
+ #
38
+ # * Currency::Exchange::Rate::Source::TimedCache
39
+ # * Currency::Exchange::Rate::Deriver
40
+ # * a Currency::Exchange::Rate::Source::Provider subclass, like Currency::Exchange::Rate::Source::Xe.
41
+ #
42
+ # Somewhere at initialization of application:
43
+ #
44
+ # require 'currency'
45
+ # require 'currency/exchange/rate/deriver'
46
+ # require 'currency/exchange/rate/source/xe'
47
+ # require 'currency/exchange/rate/source/timed_cache'
48
+ #
49
+ # provider = Currency::Exchange::Rate::Source::Xe.new
50
+ # deriver = Currency::Exchange::Rate::Deriver.new(:source => provider)
51
+ # cache = Currency::Exchange::Rate::Source::TimedCache.new(:source => deriver)
52
+ # Currency::Exchange::Rate::Source.default = cache
53
+ #
54
+ module Currency::Exchange::Rate::Source
55
+
56
+ @@default = nil
57
+ @@current = nil
58
+
59
+ # Returns the default Currency::Exchange::Rate::Source::Base object.
60
+ #
61
+ # If one is not specfied an instance of Currency::Exchange::Rate::Source::Base is
62
+ # created. Currency::Exchange::Rate::Source::Base cannot service any
63
+ # conversion rate requests.
64
+ def self.default
65
+ @@default ||= Base.new
66
+ end
67
+
68
+ # Sets the default Currency::Exchange object.
69
+ def self.default=(x)
70
+ @@default = x
71
+ end
72
+
73
+ # Returns the current Currency::Exchange object used during
74
+ # explicit and implicit Money conversions.
75
+ #
76
+ # If #current= has not been called and #default= has not been called,
77
+ # then UndefinedExchange is raised.
78
+ def self.current
79
+ @@current || self.default || (raise ::Currency::Exception::UndefinedExchange, "Currency::Exchange.current not defined")
80
+ end
81
+
82
+ # Sets the current Currency::Exchange object used during
83
+ # explicit and implicit Money conversions.
84
+ def self.current=(x)
85
+ @@current = x
86
+ end
87
+ end
88
+
89
+ require 'currency/exchange/rate/source/base'