currency 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,5 +1,8 @@
1
+ 2006-10-30 Kurt A. Stephens <kurt@umleta.com>
2
+
3
+ * Fix gem packaging error.
4
+
1
5
  2006-10-29 Kurt Stephens <kurt@umleta.com>
2
6
 
3
7
  * Initial release
4
8
 
5
-
data/README CHANGED
@@ -8,7 +8,14 @@ gem install currency
8
8
 
9
9
  == For User Documentation, see
10
10
 
11
- This package deals with currencies, conversions between currencies and monetary values in an object-oriented fashion.
11
+ http://rubyforge.org/projects/currency/
12
+
13
+ The RubyForge package currency
14
+ implements an object-oriented representation of currencies, monetary values, foreign exchanges and rates.
15
+
16
+ Currency::Money uses a scaled integer representation of the monetary value and performs accurate conversions from string values.
17
+
18
+ See: http://umleta.com/node/5 for more details.
12
19
 
13
20
  == Home page
14
21
 
data/Rakefile CHANGED
@@ -2,6 +2,18 @@
2
2
  # Adapted from RubyGems/Rakefile
3
3
  # upload_package NOT WORKING YET
4
4
 
5
+ #################################################################
6
+
7
+ PKG_Name = 'Currency'
8
+ def package_version
9
+ '0.1.1'
10
+ end
11
+ def package_description
12
+ %{Currency models currencies, monetary values, foreign exchanges.}
13
+ end
14
+
15
+ #################################################################
16
+
5
17
  #require 'rubygems'
6
18
  require 'rake/clean'
7
19
  require 'rake/testtask'
@@ -9,18 +21,15 @@ require 'rake/packagetask'
9
21
  require 'rake/gempackagetask'
10
22
  require 'rake/rdoctask'
11
23
 
24
+ #################################################################
25
+
12
26
  def announce(msg='')
13
27
  STDERR.puts msg
14
28
  end
15
29
 
16
- PKG_Name = 'Currency'
17
30
  PKG_NAME = PKG_Name.gsub(/[a-z][A-Z]/) {|x| "#{x[0,1]}_#{x[1,1]}"}.downcase
18
31
  RUBY_FORGE_PROJECT = PKG_NAME
19
32
 
20
- def package_version
21
- '0.1.0'
22
- end
23
-
24
33
  if ENV['REL']
25
34
  PKG_VERSION = ENV['REL']
26
35
  CURRENT_VERSION = package_version
@@ -250,8 +259,7 @@ Spec = Gem::Specification.new do |s|
250
259
  s.name = PKG_NAME
251
260
  s.version = PKG_VERSION
252
261
  s.summary = "#{PKG_Name} GEM"
253
- s.description = %{Currency models currencies, monetary values, foreign exchanges.
254
- }
262
+ s.description = package_description
255
263
  s.files = PKG_FILES.to_a
256
264
  s.require_path = 'lib'
257
265
  s.author = "Kurt Stephens"
data/Releases CHANGED
@@ -1,5 +1,9 @@
1
1
  = Currency Release History
2
2
 
3
+ == Release 0.1.1: 2006/10/30
4
+
5
+ * Fixes gem packaging errors.
6
+
3
7
  == Release 0.1.0: 2006/10/29
4
8
 
5
9
  * Initial Release
data/examples/ex1.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
 
3
3
  require 'currency'
4
- require 'currency/currency_exchange_test'
4
+ require 'currency/exchange/test'
5
5
 
6
6
  x = Currency::Money.new("1,203.43", 'USD')
7
7
 
data/examples/xe1.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
 
3
3
  require 'currency'
4
- require 'currency/currency_exchange_xe'
4
+ require 'currency/exchange/xe'
5
5
 
6
- ex = Currency::CurrencyExchangeXe.new()
6
+ ex = Currency::Exchange::Xe.new()
7
7
 
8
8
  puts ex.inspect
9
9
  puts ex.parse_page_rates.inspect
data/lib/currency.rb CHANGED
@@ -7,7 +7,6 @@ require 'currency/money'
7
7
  require 'currency/currency_factory'
8
8
  require 'currency/currency'
9
9
  require 'currency/money'
10
- require 'currency/exchange_rate'
11
- require 'currency/currency_exchange'
10
+ require 'currency/exchange'
12
11
  require 'currency/core_extensions'
13
12
 
@@ -4,9 +4,9 @@ module Currency
4
4
  class Currency
5
5
  # Create a new currency
6
6
  def initialize(code, symbol = nil, scale = 100)
7
- self.code= code
8
- self.symbol= symbol
9
- self.scale= scale
7
+ self.code = code
8
+ self.symbol = symbol
9
+ self.scale = scale
10
10
  end
11
11
 
12
12
  def self.get(code)
@@ -19,6 +19,18 @@ module Currency
19
19
  x
20
20
  end
21
21
 
22
+ def hash
23
+ @code.hash
24
+ end
25
+
26
+ def eql?(x)
27
+ self.class == x.class && @code == x.code
28
+ end
29
+
30
+ def ==(x)
31
+ self.class == x.class && @code == x.code
32
+ end
33
+
22
34
  # Accessors
23
35
  def code
24
36
  @code
@@ -1,10 +1,15 @@
1
1
  module Currency
2
- class InvalidMoneyString < Exception
2
+ class Error < Exception; end
3
+
4
+ class InvalidMoneyString < Error
3
5
  end
4
6
 
5
- class InvalidCurrencyCode < Exception
7
+ class InvalidCurrencyCode < Error
6
8
  end
7
9
 
8
- class IncompatibleCurrency < Exception
10
+ class IncompatibleCurrency < Error
11
+ end
12
+
13
+ class UndefinedExchange < Error
9
14
  end
10
15
  end
@@ -0,0 +1,24 @@
1
+ module Currency
2
+ module Exchange
3
+
4
+ @@default = nil
5
+ def self.default
6
+ @@default ||= Base.new
7
+ end
8
+ def self.default=(x)
9
+ @@default = x
10
+ end
11
+
12
+ @@current = nil
13
+ def self.current
14
+ @@current || self.default || (raise UndefinedExchange, "Currency::Exchange.current not defined")
15
+ end
16
+ def self.current=(x)
17
+ @@current = x
18
+ end
19
+
20
+ end
21
+ end
22
+
23
+ require 'currency/exchange/base'
24
+ require 'currency/exchange/rate'
@@ -1,18 +1,12 @@
1
1
  module Currency
2
+ module Exchange
3
+
2
4
  # Represents a method of converting between two currencies
3
- # TODO:
4
- # Create an ExchangeRateLoader class.
5
- # Create an ExchangeRateLoader subclass that interfaces to xe.com or other FX quote source.
6
- class CurrencyExchange
7
- @@default = nil
8
- def self.default
9
- @@default ||= self.new
10
- end
11
- def self.default=(x)
12
- @@default = x
13
- end
5
+ class Base
6
+ attr_accessor :name
14
7
 
15
8
  def initialize(*opt)
9
+ @name = nil
16
10
  @exchange_rate = { }
17
11
  end
18
12
 
@@ -43,8 +37,14 @@ module Currency
43
37
  def load_exchange_rate(c1, c2)
44
38
  raise "Subclass responsibility: load_exchange_rate"
45
39
  end
40
+
41
+ def to_s
42
+ "#<#{self.class.name} #{self.name && self.name.inspect}>"
43
+ end
44
+
46
45
  end
47
46
 
48
- # END MODULE
49
- end
47
+
48
+ end # module
49
+ end # module
50
50
 
@@ -1,6 +1,7 @@
1
1
  module Currency
2
+ module Exchange
2
3
  # Represents a convertion rate between two currencies
3
- class ExchangeRate
4
+ class Rate
4
5
  def initialize(c1, c2, c1_to_c2_rate, source = "UNKNOWN", date = nil, recip = true)
5
6
  @c1 = c1
6
7
  @c2 = c2
@@ -42,6 +43,8 @@ module Currency
42
43
  end
43
44
  end
44
45
 
45
- # END MODULE
46
- end
46
+
47
+ end # module
48
+ end # module
49
+
47
50
 
@@ -1,11 +1,18 @@
1
1
  module Currency
2
- # This can convert only between USD and CAD
3
- class CurrencyExchangeTest < CurrencyExchange
2
+ module Exchange
3
+
4
+ # This class is a text Exchange.
5
+ # It can convert only between USD and CAD
6
+ class Test < Base
4
7
  @@instance = nil
5
8
  def self.instance(*opts)
6
9
  @@instance ||= self.new(*opts)
7
10
  end
8
11
 
12
+ def initialize(*opts)
13
+ super(*opts)
14
+ end
15
+
9
16
  # Sample constant.
10
17
  def self.USD_CAD; 1.1708; end
11
18
 
@@ -15,13 +22,13 @@ module Currency
15
22
  if ( c1.code == :USD && c2.code == :CAD )
16
23
  rate = self.class.USD_CAD
17
24
  end
18
- rate > 0 ? ExchangeRate.new(c1, c2, rate, self.class.name) : nil
25
+ rate > 0 ? Rate.new(c1, c2, rate, self) : nil
19
26
  end
20
27
  end
21
28
 
22
- # END MODULE
23
- end
29
+ end # module
30
+ end # module
24
31
 
25
32
  # Install as current
26
- Currency::CurrencyExchange.default = Currency::CurrencyExchangeTest.instance
33
+ Currency::Exchange.default = Currency::Exchange::Test.instance
27
34
 
@@ -2,15 +2,19 @@ require 'net/http'
2
2
  require 'open-uri'
3
3
 
4
4
  module Currency
5
+ module Exchange
5
6
  # Represents connects to http://xe.com and groks "XE.com Quick Cross Rates"
6
7
 
7
- class CurrencyExchangeXe < CurrencyExchange
8
+ class Xe < Base
8
9
  @@instance = nil
9
10
  def self.instance(*opts)
10
11
  @@instance ||= self.new(*opts)
11
12
  end
12
13
 
13
14
  attr_accessor :uri
15
+ def name
16
+ uri
17
+ end
14
18
 
15
19
  def initialize(*opt)
16
20
  super(*opt)
@@ -128,13 +132,14 @@ module Currency
128
132
 
129
133
  # $stderr.puts "XE Rate: #{c1.code} / #{c2.code} = #{rate}"
130
134
 
131
- rate > 0 ? ExchangeRate.new(c1, c2, rate, self.class.name, @rate_timestamp) : nil
135
+ rate > 0 ? Rate.new(c1, c2, rate, self, @rate_timestamp) : nil
132
136
  end
133
137
  end
134
138
 
135
- # END MODULE
136
- end
139
+ end # module
140
+ end # module
141
+
137
142
 
138
143
  # Install as current
139
- Currency::CurrencyExchange.default = Currency::CurrencyExchangeXe.instance
144
+ Currency::Exchange.default = Currency::Exchange::Xe.instance
140
145
 
@@ -76,11 +76,15 @@ module Currency
76
76
  if @currency == currency
77
77
  self
78
78
  else
79
- CurrencyExchange.default.convert(self, currency)
79
+ Exchange.current.convert(self, currency)
80
80
  end
81
81
  end
82
82
 
83
83
  # Relational operations on Money values.
84
+ def hash
85
+ @rep.hash ^ @currency.hash
86
+ end
87
+
84
88
  def eql?(x)
85
89
  @rep == x.rep && @currency == x.currency
86
90
  end
@@ -184,7 +188,7 @@ module Currency
184
188
  #def inspect_deep(*opts)
185
189
  # self.class.superclass.instance_method(:inspect).bind(self).call
186
190
  #end
187
- end
188
191
 
189
- # END MODULE
190
- end
192
+ end # class
193
+
194
+ end # module
data/test/money_test.rb CHANGED
@@ -157,7 +157,7 @@ class MoneyTest < TestBase
157
157
  assert_equal_float 3.0, m
158
158
 
159
159
  assert_kind_of Numeric, m = (usd / cad)
160
- assert_equal_float CurrencyExchangeTest.USD_CAD, m, 0.0001
160
+ assert_equal_float Exchange::Test.USD_CAD, m, 0.0001
161
161
  end
162
162
 
163
163
  end
data/test/test_base.rb CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  require 'test/unit'
4
4
  require 'currency'
5
- require 'currency/currency_exchange_test'
5
+ require 'currency/exchange/test'
6
6
 
7
7
  module Currency
8
8
 
9
9
  class TestBase < Test::Unit::TestCase
10
10
  def setup
11
11
  super
12
- # Force XE Exchange.
13
- CurrencyExchange.default = CurrencyExchangeTest.instance
12
+ # Force Test Exchange.
13
+ Exchange.default = Exchange::Test.instance
14
14
  end
15
15
 
16
16
  # Avoid "No test were specified" error.
data/test/xe_test.rb CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  require 'test/test_base'
5
5
  require 'currency' # For :type => :money
6
- require 'currency/currency_exchange_xe'
6
+ require 'currency/exchange/xe'
7
7
 
8
8
  module Currency
9
9
 
@@ -11,11 +11,11 @@ class XeTest < TestBase
11
11
  def setup
12
12
  super
13
13
  # Force XE Exchange.
14
- CurrencyExchange.default = CurrencyExchangeXe.instance
14
+ Exchange.default = Exchange::Xe.instance
15
15
  end
16
16
 
17
17
  def test_xe_usd_cad
18
- assert_not_nil rates = CurrencyExchange.default.rates
18
+ assert_not_nil rates = Exchange.default.rates
19
19
  assert_not_nil rates[:USD]
20
20
  assert_not_nil usd_cad = rates[:USD][:CAD]
21
21
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: currency
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2006-10-29 00:00:00 -04:00
6
+ version: 0.1.1
7
+ date: 2006-10-30 00:00:00 -05:00
8
8
  summary: Currency GEM
9
9
  require_paths:
10
10
  - lib
@@ -25,28 +25,6 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  platform: ruby
26
26
  signing_key:
27
27
  cert_chain:
28
- - |
29
- -----BEGIN CERTIFICATE-----
30
- MIIDNDCCAhygAwIBAgIBADANBgkqhkiG9w0BAQUFADBAMREwDwYDVQQDDAhydWJ5
31
- Z2VtczEWMBQ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U1wk
47
- +Wg1asFvX0E=
48
- -----END CERTIFICATE-----
49
-
50
28
  authors:
51
29
  - Kurt Stephens
52
30
  files:
@@ -66,10 +44,11 @@ files:
66
44
  - lib/currency/money.rb
67
45
  - lib/currency/active_record.rb
68
46
  - lib/currency/currency.rb
69
- - lib/currency/currency_exchange.rb
70
- - lib/currency/exchange_rate.rb
71
- - lib/currency/currency_exchange_test.rb
72
- - lib/currency/currency_exchange_xe.rb
47
+ - lib/currency/exchange.rb
48
+ - lib/currency/exchange/base.rb
49
+ - lib/currency/exchange/test.rb
50
+ - lib/currency/exchange/xe.rb
51
+ - lib/currency/exchange/rate.rb
73
52
  - scripts/gemdoc.rb
74
53
  - test/xe_test.rb
75
54
  - test/money_test.rb
data.tar.gz.sig DELETED
@@ -1 +0,0 @@
1
- )͍�����[�����֕�p|z;���&����ЃV1tD����B��PF�b���&�该0w��%[P�����uy��G��M-���J��D� ��4`"곮��m{QM4O�Uv�D��]_
metadata.gz.sig DELETED
Binary file