mumboe-currency 0.5.2 → 0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/currency.rb +1 -0
- data/lib/currency/config.rb +2 -2
- data/lib/currency/currency/factory.rb +1 -1
- data/lib/currency/currency_version.rb +1 -1
- data/lib/currency/money.rb +1 -1
- data/lib/currency/parser.rb +1 -1
- data/test/string_test.rb +78 -1
- metadata +19 -10
data/lib/currency.rb
CHANGED
@@ -124,6 +124,7 @@ end
|
|
124
124
|
|
125
125
|
$:.unshift(File.expand_path(File.dirname(__FILE__))) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
126
126
|
|
127
|
+
require 'bigdecimal'
|
127
128
|
require 'currency/currency_version'
|
128
129
|
require 'currency/config'
|
129
130
|
require 'currency/exception'
|
data/lib/currency/config.rb
CHANGED
@@ -61,8 +61,8 @@ class Currency::Config
|
|
61
61
|
result
|
62
62
|
end
|
63
63
|
|
64
|
-
|
65
|
-
@@identity = Proc.new { |x| x } # :nodoc:
|
64
|
+
# using BigDecimal to avoid floating point rounding errors.
|
65
|
+
@@identity = Proc.new { |x| BigDecimal.new(x.to_s) } # :nodoc:
|
66
66
|
|
67
67
|
# Returns the current Float conversion filter.
|
68
68
|
# Can be used to set rounding or truncation policies when converting
|
data/lib/currency/money.rb
CHANGED
data/lib/currency/parser.rb
CHANGED
data/test/string_test.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# various tests to verify latest revisions
|
2
2
|
require "test/test_base"
|
3
3
|
require 'currency'
|
4
|
-
|
5
4
|
module Currency
|
6
5
|
|
7
6
|
class StringTest < TestBase
|
@@ -49,6 +48,84 @@ module Currency
|
|
49
48
|
assert_equal "$0.50", "0.5".money.to_s
|
50
49
|
end
|
51
50
|
|
51
|
+
def test_string_number
|
52
|
+
m = "10.12".money*100
|
53
|
+
assert_equal "$1,012.00", m.to_s
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_float_number
|
57
|
+
m = 10.12.money*100
|
58
|
+
assert_equal "$1,012.00", m.to_s
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_bigdecimal
|
62
|
+
assert (BigDecimal.new("1.2") - BigDecimal("1.0")) == BigDecimal("0.2")
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_side_effects
|
66
|
+
m = "1000.00".money
|
67
|
+
s = "500.000".money
|
68
|
+
assert_equal "$1,000.00", m.to_s
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_app_string_not_money
|
72
|
+
value = "xxxx"
|
73
|
+
assert_raise(::Currency::Exception::InvalidMoneyString) {
|
74
|
+
::Currency.Money(value).to_s(:symbol => false, :code => true).split
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_app_money_string
|
79
|
+
value = "1000"
|
80
|
+
arr = ::Currency.Money(value).to_s(:symbol => false, :code => true).split
|
81
|
+
assert_equal 'USD', arr[0]
|
82
|
+
assert_equal '1,000.00', arr[1]
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_app_money_string_with_decimals
|
86
|
+
value = "1000.234"
|
87
|
+
arr = ::Currency.Money(value).to_s(:symbol => false, :code => true).split
|
88
|
+
assert_equal 'USD', arr[0]
|
89
|
+
assert_equal '1,000.234', arr[1]
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_app_money_string_with_dollar
|
93
|
+
value = "$1000.234"
|
94
|
+
arr = ::Currency.Money(value).to_s(:symbol => false, :code => true).split
|
95
|
+
assert_equal 'USD', arr[0]
|
96
|
+
assert_equal '1,000.234', arr[1]
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_app_money_string_with_yuan
|
100
|
+
value = "¥1000.234"
|
101
|
+
arr = ::Currency.Money(value).to_s(:symbol => false, :code => true).split
|
102
|
+
assert_equal 'CNY', arr[0]
|
103
|
+
assert_equal '1,000.234', arr[1]
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_money_string_to_f
|
107
|
+
value = "1000.234"
|
108
|
+
f =::Currency.Money(value).to_f
|
109
|
+
assert_equal 1000.234, f
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_money_int_to_f
|
113
|
+
value = 1000
|
114
|
+
f =::Currency.Money(value).to_f
|
115
|
+
assert_equal 1000.0, f
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_money_to_f
|
119
|
+
value = "$1000.234"
|
120
|
+
f =::Currency.Money(value).to_f
|
121
|
+
assert_equal 1000.234, f
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_money_with_1_decimal_to_f
|
125
|
+
value = "$1000.2"
|
126
|
+
f =::Currency.Money(value).to_f
|
127
|
+
assert_equal 1000.2, f
|
128
|
+
end
|
52
129
|
end
|
53
130
|
|
54
131
|
end
|
metadata
CHANGED
@@ -1,20 +1,24 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mumboe-currency
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 7
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 6
|
9
|
+
version: "0.6"
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
|
-
-
|
12
|
+
- John Liu & Eric Stewart
|
8
13
|
autorequire:
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
13
|
-
default_executable:
|
17
|
+
date: 2011-04-26 00:00:00 Z
|
14
18
|
dependencies: []
|
15
19
|
|
16
20
|
description:
|
17
|
-
email:
|
21
|
+
email: johnl@mumboe.com
|
18
22
|
executables:
|
19
23
|
- currency_historical_rate_load
|
20
24
|
extensions: []
|
@@ -58,7 +62,6 @@ files:
|
|
58
62
|
- lib/currency.rb
|
59
63
|
- test/string_test.rb
|
60
64
|
- test/test_base.rb
|
61
|
-
has_rdoc: true
|
62
65
|
homepage: http://rubyforge.org/projects/currency/
|
63
66
|
licenses: []
|
64
67
|
|
@@ -68,23 +71,29 @@ rdoc_options: []
|
|
68
71
|
require_paths:
|
69
72
|
- lib
|
70
73
|
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
71
75
|
requirements:
|
72
76
|
- - ">="
|
73
77
|
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
74
81
|
version: "0"
|
75
|
-
version:
|
76
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
77
84
|
requirements:
|
78
85
|
- - ">="
|
79
86
|
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
80
90
|
version: "0"
|
81
|
-
version:
|
82
91
|
requirements: []
|
83
92
|
|
84
93
|
rubyforge_project:
|
85
|
-
rubygems_version: 1.
|
94
|
+
rubygems_version: 1.7.2
|
86
95
|
signing_key:
|
87
96
|
specification_version: 3
|
88
|
-
summary:
|
97
|
+
summary: Fixed unwanted side-effects by avoiding shared factory and floating point rounding errors
|
89
98
|
test_files:
|
90
99
|
- test/test_base.rb
|