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,140 @@
1
+ # Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
2
+ # See LICENSE.txt for details.
3
+
4
+ require 'test/test_base'
5
+ require 'currency'
6
+
7
+ require 'rubygems'
8
+ require 'active_record'
9
+ require 'active_record/migration'
10
+ require 'currency/active_record'
11
+
12
+ AR_M = ActiveRecord::Migration
13
+ AR_B = ActiveRecord::Base
14
+
15
+ module Currency
16
+
17
+ class ArTestBase < TestBase
18
+
19
+ ##################################################
20
+
21
+ before do
22
+ super
23
+ AR_B.establish_connection(database_spec)
24
+
25
+ # Subclasses can override this.
26
+ @currency_test_migration ||= nil
27
+ @currency_test ||= nil
28
+
29
+ # schema_down
30
+
31
+ schema_up
32
+ end
33
+
34
+
35
+ def teardown
36
+ super
37
+ # schema_down
38
+ end
39
+
40
+
41
+ def database_spec
42
+ # TODO: Get from ../config/database.yml:test
43
+ # Create test database on:
44
+
45
+ # MYSQL:
46
+ #
47
+ # sudo mysqladmin create test;
48
+ # sudo mysql
49
+ # grant all on test.* to test@localhost identified by 'test';
50
+ # flush privileges;
51
+
52
+ # POSTGRES:
53
+ #
54
+ # CREATE USER test PASSWORD 'test';
55
+ # CREATE DATABASE test WITH OWNER = test;
56
+ #
57
+
58
+ @database_spec = {
59
+ :adapter => ENV['TEST_DB_ADAPTER'] || 'mysql',
60
+ :host => ENV['TEST_DB_HOST'] || 'localhost',
61
+ :username => ENV['TEST_DB_USER'] || 'test',
62
+ :password => ENV['TEST_DB_PASS'] || 'test',
63
+ :database => ENV['TEST_DB_TEST'] || 'test'
64
+ }
65
+ end
66
+
67
+
68
+ def schema_up
69
+ return unless @currency_test_migration
70
+ begin
71
+ @currency_test_migration.migrate(:up)
72
+ rescue Object =>e
73
+ $stderr.puts "Warning: #{e}"
74
+ end
75
+ end
76
+
77
+
78
+ def schema_down
79
+ return unless @currency_test_migration
80
+ begin
81
+ @currency_test_migration.migrate(:down)
82
+ rescue Object => e
83
+ $stderr.puts "Warning: #{e}"
84
+ end
85
+ end
86
+
87
+
88
+ ##################################################
89
+ # Scaffold
90
+ #
91
+
92
+ def insert_records
93
+ delete_records
94
+
95
+ @currency_test.reset_column_information
96
+
97
+ @usd = @currency_test.new(:name => '#1: USD', :amount => Money.new("12.34", :USD))
98
+ @usd.save
99
+
100
+ @cad = @currency_test.new(:name => '#2: CAD', :amount => Money.new("56.78", :CAD))
101
+ @cad.save
102
+ end
103
+
104
+
105
+ def delete_records
106
+ @currency_test.destroy_all
107
+ end
108
+
109
+
110
+ ##################################################
111
+
112
+
113
+ def assert_equal_money(a,b)
114
+ a.should_not be_nil
115
+ b.should_not be_nil
116
+ # Make sure a and b are not the same object.
117
+ b.object_id.should_not == a.object_id
118
+ b.id.should == a.id
119
+ a.amount.should.not == nil
120
+ a.amount.should be_kind_of(Money)
121
+ b.amount.should.not == nil
122
+ b.amount.should be_kind_of(Money)
123
+ # Make sure that what gets stored in the database comes back out
124
+ # when converted back to the original currency.
125
+ b.amount.rep.should == a.amount.convert(b.amount.currency).rep
126
+ end
127
+
128
+
129
+ def assert_equal_currency(a,b)
130
+ assert_equal_money a, b
131
+
132
+ b.amount.rep.should == a.amount.rep
133
+ b.amount.currency.should == a.amount.currency
134
+ b.amount.currency.code.should == a.amount.currency.code
135
+
136
+ end
137
+ end
138
+
139
+ end # module
140
+
@@ -0,0 +1,69 @@
1
+ # Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
2
+ # See LICENSE.txt for details.
3
+
4
+ require 'test/ar_test_core'
5
+ require 'currency'
6
+
7
+ require 'rubygems'
8
+ require 'active_record'
9
+ require 'active_record/migration'
10
+ require 'currency/active_record'
11
+
12
+ module Currency
13
+
14
+ class ArFieldTest < ArTestCore
15
+
16
+ ##################################################
17
+ # Basic CurrenyTest AR::B class
18
+ #
19
+
20
+ TABLE_NAME = 'currency_column_test'
21
+
22
+ class CurrencyColumnTestMigration < AR_M
23
+ def self.up
24
+ create_table TABLE_NAME.intern do |t|
25
+ t.column :name, :string
26
+ t.column :amount, :integer # Money
27
+ t.column :amount_currency, :string, :size => 3 # Money.currency.code
28
+ end
29
+ end
30
+
31
+ def self.down
32
+ drop_table TABLE_NAME.intern
33
+ end
34
+ end
35
+
36
+ class CurrencyColumnTest < AR_B
37
+ set_table_name TABLE_NAME
38
+ attr_money :amount, :currency_column => true
39
+ end
40
+
41
+ ##################################################
42
+
43
+ before do
44
+ @currency_test_migration ||= CurrencyColumnTestMigration
45
+ @currency_test ||= CurrencyColumnTest
46
+ super
47
+ end
48
+
49
+ def teardown
50
+ super
51
+ end
52
+
53
+ ##################################################
54
+
55
+
56
+ it "field" do
57
+ insert_records
58
+
59
+ usd = @currency_test.find(@usd.id).should.not == nil
60
+ assert_equal_currency usd, @usd
61
+
62
+ cad = @currency_test.find(@cad.id).should.not == nil
63
+ assert_equal_currency cad, @cad
64
+ end
65
+
66
+ end
67
+
68
+ end # module
69
+
@@ -0,0 +1,64 @@
1
+ # Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
2
+ # See LICENSE.txt for details.
3
+
4
+ require 'test/ar_test_base'
5
+
6
+ module Currency
7
+
8
+ class ArTestCore < ArTestBase
9
+
10
+ ##################################################
11
+ # Basic CurrenyTest AR::B class
12
+ #
13
+
14
+ TABLE_NAME = 'currency_test'
15
+
16
+ class CurrencyTestMigration < AR_M
17
+ def self.up
18
+ create_table TABLE_NAME.intern do |t|
19
+ t.column :name, :string
20
+ t.column :amount, :integer # Money
21
+ end
22
+ end
23
+
24
+ def self.down
25
+ drop_table TABLE_NAME.intern
26
+ end
27
+ end
28
+
29
+
30
+ class CurrencyTest < AR_B
31
+ set_table_name TABLE_NAME
32
+ attr_money :amount
33
+ end
34
+
35
+
36
+ ##################################################
37
+
38
+
39
+ before do
40
+ @currency_test_migration ||= CurrencyTestMigration
41
+ @currency_test ||= CurrencyTest
42
+ super
43
+ end
44
+
45
+
46
+ def teardown
47
+ super
48
+ # schema_down
49
+ end
50
+
51
+
52
+ ##################################################
53
+ #
54
+ #
55
+
56
+
57
+ it "insert" do
58
+ insert_records
59
+ end
60
+
61
+ end
62
+
63
+ end # module
64
+
@@ -0,0 +1,31 @@
1
+ # Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
2
+ # See LICENSE.txt for details.
3
+
4
+ require 'test/ar_test_core'
5
+ require 'currency'
6
+
7
+ require 'rubygems'
8
+ require 'active_record'
9
+ require 'active_record/migration'
10
+ require 'currency/active_record'
11
+
12
+ module Currency
13
+
14
+ class ArSimpleTest < ArTestCore
15
+
16
+ it "simple" do
17
+ insert_records
18
+
19
+ usd = @currency_test.find(@usd.id).should.not == nil
20
+ assert_equal_currency usd, @usd
21
+
22
+ cad = @currency_test.find(@cad.id).should.not == nil
23
+ assert_equal_money cad, @cad
24
+
25
+ :USD.should == cad.amount.currency.code
26
+ end
27
+
28
+ end
29
+
30
+ end # module
31
+
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Currency::Config do
4
+
5
+ it "should truncate" do
6
+ Currency::Config.configure do | c |
7
+ c.float_ref_filter = Proc.new { | x | x }
8
+
9
+ m = Currency::Money.new(1.999999999)
10
+ m.should be_kind_of(Currency::Money)
11
+ m.rep.should == 1999999
12
+ end
13
+ end
14
+
15
+ it "should round" do
16
+ Currency::Config.configure do | c |
17
+ c.float_ref_filter = Proc.new { | x | x.round }
18
+
19
+ m = Currency::Money.new(1.99999999)
20
+ m.should be_kind_of(Currency::Money)
21
+ m.rep.should == 2000000
22
+ end
23
+
24
+ end
25
+
26
+ end # class
27
+
28
+
29
+
@@ -0,0 +1,75 @@
1
+ # Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
2
+ # See LICENSE.txt for details.
3
+
4
+ require 'test/test_base'
5
+
6
+ require 'currency' # For :type => :money
7
+ require 'currency/exchange/rate/source/federal_reserve'
8
+
9
+ module Currency
10
+
11
+ class FederalReserveTest < TestBase
12
+ before do
13
+ super
14
+ end
15
+
16
+
17
+ @@available = nil
18
+
19
+ # New York Fed rates are not available on Saturday and Sunday.
20
+ def available?
21
+ if @@available == nil
22
+ @@available = @source.available?
23
+ STDERR.puts "Warning: FederalReserve unavailable on Saturday and Sunday, skipping tests." unless @@available
24
+ end
25
+ @@available
26
+ end
27
+
28
+
29
+ def get_rate_source
30
+ # Force FederalReserve Exchange.
31
+ verbose = false
32
+ source = @source = Exchange::Rate::Source::FederalReserve.new(:verbose => verbose)
33
+ deriver = Exchange::Rate::Deriver.new(:source => source, :verbose => source.verbose)
34
+ end
35
+
36
+
37
+
38
+ it "usd cad" do
39
+ return unless available?
40
+
41
+ # yesterday = Time.now.to_date - 1
42
+
43
+ rates = Exchange::Rate::Source.default.source.raw_rates.should.not == nil
44
+ #assert_not_nil rates[:USD]
45
+ #assert_not_nil usd_cad = rates[:USD][:CAD]
46
+
47
+ usd = Money.new(123.45, :USD).should.not == nil
48
+ cad = usd.convert(:CAD).should.not == nil
49
+
50
+ # assert_kind_of Numeric, m = (cad.to_f / usd.to_f)
51
+ # $stderr.puts "m = #{m}"
52
+ # assert_equal_float usd_cad, m, 0.001
53
+ end
54
+
55
+
56
+ it "cad eur" do
57
+ return unless available?
58
+
59
+ rates = Exchange::Rate::Source.default.source.raw_rates.should.not == nil
60
+ #assert_not_nil rates[:USD]
61
+ #assert_not_nil usd_cad = rates[:USD][:CAD]
62
+ #assert_not_nil usd_eur = rates[:USD][:EUR]
63
+
64
+ cad = Money.new(123.45, :CAD).should.not == nil
65
+ eur = cad.convert(:EUR).should.not == nil
66
+
67
+ #assert_kind_of Numeric, m = (eur.to_f / cad.to_f)
68
+ # $stderr.puts "m = #{m}"
69
+ #assert_equal_float (1.0 / usd_cad) * usd_eur, m, 0.001
70
+ end
71
+
72
+ end
73
+
74
+ end # module
75
+
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Currency::Formatter do
4
+ before(:each) do
5
+ @time = nil
6
+ @money = Currency::Money.new_rep(1234567890000, :USD, @time)
7
+
8
+ end
9
+
10
+ it "can convert to string" do
11
+ @money.should be_kind_of(Currency::Money)
12
+ @money.currency.should == Currency::Currency.default
13
+ @money.currency.code.should == :USD
14
+ @money.to_s.should == "$1,234,567.890000"
15
+ @money.time.should == @time
16
+ end
17
+
18
+
19
+ it "handles thousands options" do
20
+ @money.to_s(:thousands => false).should == "$1234567.890000"
21
+ @money.to_s(:thousands => true).should == "$1,234,567.890000"
22
+ end
23
+
24
+
25
+ it "handles cents options" do
26
+ @money.to_s(:cents => false).should == "$1,234,567"
27
+ @money.to_s(:cents => true).should == "$1,234,567.890000"
28
+ end
29
+
30
+
31
+ it "handles symbol options" do
32
+ @money.to_s(:symbol => false).should == "1,234,567.890000"
33
+ @money.to_s(:symbol => true).should == "$1,234,567.890000"
34
+ end
35
+
36
+
37
+ it "handles code options" do
38
+ @money.to_s(:code => false).should == "$1,234,567.890000"
39
+ @money.to_s(:code => true).should == "USD $1,234,567.890000"
40
+ end
41
+
42
+
43
+ it "handles html and more" do
44
+ m = ::Currency::Money(12.45, :USD)
45
+ money_string = m.to_s(:html => true, :code => true)
46
+ money_string.should == "<span class=\"currency_code\">USD</span> $12.450000"
47
+
48
+
49
+ m = ::Currency::Money(12.45, :EUR)
50
+ money_string = m.to_s(:html => true, :code => true)
51
+ money_string.should == "<span class=\"currency_code\">EUR</span> &#8364;12.450000"
52
+
53
+ m = ::Currency::Money(12345.45, :EUR)
54
+ money_string = m.to_s(:html => true, :code => true, :thousands_separator => '_')
55
+ money_string.should == "<span class=\"currency_code\">EUR</span> &#8364;12_345.450000"
56
+ end
57
+
58
+
59
+ it "handles time options" do
60
+ time = Time.new
61
+ m = Currency::Money.new_rep(1234567890000, :USD, time)
62
+ m.to_s(:time => false).should == "$1,234,567.890000"
63
+ m.to_s(:time => true).should == "$1,234,567.890000 #{time.getutc.xmlschema(4)}"
64
+ end
65
+
66
+ it "handles decimal options" do
67
+ @money = Currency::Money.new_rep(1234567890000, :USD, @time)
68
+ @money.to_s(:decimals => 2).should == "$1,234,567.89"
69
+ @money.to_s(:decimals => 3).should == "$1,234,567.890"
70
+ @money.to_s(:decimals => 4).should == "$1,234,567.8900"
71
+ end
72
+ end