mroch-BigMoney 0.1.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.
- data/History.txt +4 -0
- data/LICENSE.txt +20 -0
- data/Manifest.txt +10 -0
- data/README.txt +70 -0
- data/Rakefile +11 -0
- data/bigmoney.gemspec +27 -0
- data/lib/big_money/big_money.rb +83 -0
- data/lib/big_money/core_extensions.rb +11 -0
- data/lib/big_money.rb +5 -0
- data/test/test_big_money.rb +128 -0
- metadata +62 -0
data/History.txt
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Marshall Roch
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
= BigMoney
|
2
|
+
|
3
|
+
== DESCRIPTION:
|
4
|
+
|
5
|
+
Represents an amount of money in a particular currency. Backed by BigDecimal,
|
6
|
+
so is safe from float rounding errors.
|
7
|
+
|
8
|
+
== FEATURES:
|
9
|
+
|
10
|
+
* Encapsulates an amount with its currency into a single object.
|
11
|
+
|
12
|
+
* Backed by BigDecimal, so it can store arbitrary-precision values without
|
13
|
+
rounding errors. Useful if you're dealing with fractional cents.
|
14
|
+
|
15
|
+
== PROBLEMS:
|
16
|
+
|
17
|
+
* Does not implement all of Numeric, so doesn't quite act like a real number
|
18
|
+
|
19
|
+
* Doesn't allow currency conversion (patches welcome)
|
20
|
+
|
21
|
+
* Has a slightly different API than the Money package
|
22
|
+
(http://dist.leetsoft.com/api/money/)
|
23
|
+
|
24
|
+
== SYNOPSIS:
|
25
|
+
|
26
|
+
bm = BigMoney.new('3.99')
|
27
|
+
bm.amount #=> BigDecimal.new('3.99')
|
28
|
+
bm.currency #=> :USD
|
29
|
+
bm.to_s #=> '3.99'
|
30
|
+
bm.to_formatted_s('$%.2f') #=> '$3.99'
|
31
|
+
bm.to_formatted_s('$%.2f %s') #=> '$3.99 USD'
|
32
|
+
|
33
|
+
bm2 = BigMoney.new(1)
|
34
|
+
bm + bm2 #=> BigMoney.new(4.99)
|
35
|
+
bm + 1 #=> BigMoney.new(4.99)
|
36
|
+
|
37
|
+
== INSTALL:
|
38
|
+
|
39
|
+
* Via git:
|
40
|
+
|
41
|
+
git clone git://github.com/mroch/bigmoney.git
|
42
|
+
|
43
|
+
* From RubyForge: TBA
|
44
|
+
|
45
|
+
* Via gem: TBA
|
46
|
+
|
47
|
+
== LICENSE:
|
48
|
+
|
49
|
+
(The MIT License)
|
50
|
+
|
51
|
+
Copyright (c) 2008 Marshall Roch
|
52
|
+
|
53
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
54
|
+
a copy of this software and associated documentation files (the
|
55
|
+
'Software'), to deal in the Software without restriction, including
|
56
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
57
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
58
|
+
permit persons to whom the Software is furnished to do so, subject to
|
59
|
+
the following conditions:
|
60
|
+
|
61
|
+
The above copyright notice and this permission notice shall be
|
62
|
+
included in all copies or substantial portions of the Software.
|
63
|
+
|
64
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
65
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
66
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
67
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
68
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
69
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
70
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/bigmoney.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "BigMoney"
|
3
|
+
s.version = "0.1.0"
|
4
|
+
s.date = "2008-06-21"
|
5
|
+
s.summary = "Represents an amount of money in a particular currency."
|
6
|
+
s.email = "mroch@cmu.edu"
|
7
|
+
s.homepage = "http://github.com/mroch/bigmoney"
|
8
|
+
s.description = "Represents an amount of money in a particular currency. Backed by BigDecimal, so is safe from float rounding errors."
|
9
|
+
s.has_rdoc = false
|
10
|
+
s.authors = ["Marshall Roch"]
|
11
|
+
s.files = [
|
12
|
+
"bigmoney.gemspec",
|
13
|
+
"History.txt",
|
14
|
+
"Manifest.txt",
|
15
|
+
"LICENSE.txt",
|
16
|
+
"Rakefile",
|
17
|
+
"README.txt",
|
18
|
+
"lib/big_money.rb",
|
19
|
+
"lib/big_money/big_money.rb",
|
20
|
+
"lib/big_money/core_extensions.rb",
|
21
|
+
"test/test_big_money.rb"
|
22
|
+
]
|
23
|
+
s.test_files = ["test/test_big_money.rb"]
|
24
|
+
# s.rdoc_options = ["--main", "README.txt"]
|
25
|
+
# s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
26
|
+
# s.add_dependency("mime-types", ["> 0.0.0"])
|
27
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
class BigMoney
|
4
|
+
VERSION = '0.1.0'
|
5
|
+
|
6
|
+
class MoneyError < StandardError ; end
|
7
|
+
class UncomparableCurrency < MoneyError ; end
|
8
|
+
|
9
|
+
@@default_currency = :USD
|
10
|
+
def self.default_currency ; @@default_currency ; end
|
11
|
+
def self.default_currency=(c) ; @@default_currency = c ; end
|
12
|
+
def default_currency ; self.class.default_currency ; end
|
13
|
+
def default_currency=(c) ; self.class.default_currency = c ; end
|
14
|
+
|
15
|
+
attr_reader :amount, :currency
|
16
|
+
|
17
|
+
def initialize(amount, currency = nil)
|
18
|
+
@amount = amount.class == BigDecimal ? amount : BigDecimal.new(amount.to_s)
|
19
|
+
@currency = currency || self.default_currency
|
20
|
+
end
|
21
|
+
|
22
|
+
def eql?(other_money)
|
23
|
+
currency == other_money.currency && amount == other_money.amount
|
24
|
+
end
|
25
|
+
|
26
|
+
def ==(other_money)
|
27
|
+
eql?(other_money)
|
28
|
+
end
|
29
|
+
|
30
|
+
def <=>(other_money)
|
31
|
+
raise UncomparableCurrency, "Cannot compare #{currency} to #{other_money.currency}" \
|
32
|
+
unless currency == other_money.currency
|
33
|
+
|
34
|
+
amount <=> other_money.amount
|
35
|
+
end
|
36
|
+
|
37
|
+
def -@
|
38
|
+
BigMoney.new(-amount, currency)
|
39
|
+
end
|
40
|
+
|
41
|
+
def +(val)
|
42
|
+
op(:+, val)
|
43
|
+
end
|
44
|
+
|
45
|
+
def -(val)
|
46
|
+
op(:-, val)
|
47
|
+
end
|
48
|
+
|
49
|
+
def *(val)
|
50
|
+
op(:*, val)
|
51
|
+
end
|
52
|
+
|
53
|
+
def /(val)
|
54
|
+
op(:/, val)
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_s
|
58
|
+
to_formatted_s('%.2f')
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_formatted_s(format)
|
62
|
+
format.sub(/%s/, currency.to_s.upcase) % amount
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_i
|
66
|
+
amount.to_i
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_f
|
70
|
+
amount.to_f
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
def op(s, val)
|
75
|
+
if val.class == BigMoney
|
76
|
+
raise UncomparableCurrency, "Cannot compare #{currency} to #{val.currency}" \
|
77
|
+
unless currency == val.currency
|
78
|
+
BigMoney.new(amount.send(s, val.amount), currency)
|
79
|
+
else
|
80
|
+
BigMoney.new(amount.send(s, val), currency)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
data/lib/big_money.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'big_money'
|
3
|
+
require 'bigdecimal'
|
4
|
+
|
5
|
+
class TestBigMoney < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_initialize_with_bigdecimal
|
8
|
+
val = BigDecimal.new("1.005")
|
9
|
+
assert_same val, BigMoney.new(val).amount, 'Should not create a clone of the input'
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_initialize_with_string
|
13
|
+
assert_equal BigDecimal.new("1.005"), BigMoney.new("1.005").amount
|
14
|
+
assert_equal BigDecimal.new("10"), BigMoney.new("10").amount
|
15
|
+
assert_equal BigDecimal.new("0"), BigMoney.new("0").amount
|
16
|
+
assert_equal BigDecimal.new("0"), BigMoney.new("foo").amount
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_initialize_with_integer
|
20
|
+
assert_equal BigDecimal.new("100"), BigMoney.new(100).amount
|
21
|
+
assert_equal BigDecimal.new("0"), BigMoney.new(0).amount
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_initialize_with_float
|
25
|
+
assert_equal BigDecimal.new("1.005"), BigMoney.new(1.005).amount
|
26
|
+
assert_equal BigDecimal.new("0"), BigMoney.new(0.0).amount
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_eql?
|
30
|
+
assert BigMoney.new("1.00", :usd).eql?(BigMoney.new("1.00", :usd))
|
31
|
+
assert !BigMoney.new("1.00", :usd).eql?(BigMoney.new("1.01", :usd))
|
32
|
+
assert !BigMoney.new("1.00", :usd).eql?(BigMoney.new("1.00", :cad))
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_compare
|
36
|
+
m1, m2 = BigMoney.new("1.00", :usd), BigMoney.new("1.50", :usd)
|
37
|
+
assert_equal(-1, (m1 <=> m2))
|
38
|
+
assert_equal(0, (m1 <=> m1))
|
39
|
+
assert_equal(1, (m2 <=> m1))
|
40
|
+
|
41
|
+
m3 = BigMoney.new("1.00", :cad)
|
42
|
+
assert_raises BigMoney::UncomparableCurrency do
|
43
|
+
m1 <=> m3
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_add
|
48
|
+
m1, m2, m3 = BigMoney.new(1.00, :usd), BigMoney.new(1.50, :usd), BigMoney.new(0, :usd)
|
49
|
+
assert_equal BigDecimal.new("2.50"), (m1 + m2).amount
|
50
|
+
assert_equal BigDecimal.new("2.50"), (m2 + m1).amount
|
51
|
+
assert_equal BigDecimal.new("1.00"), (m1 + m3).amount
|
52
|
+
assert_equal BigDecimal.new("1.00"), (m3 + m1).amount
|
53
|
+
assert_not_same m1, m1 + m3
|
54
|
+
assert_not_same m1, m3 + m1
|
55
|
+
assert_equal BigDecimal.new("5.50"), (m2 + 4).amount
|
56
|
+
assert_equal BigDecimal.new("5.50"), (m2 + 4.00).amount
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_subtract
|
60
|
+
m1, m2, m3 = BigMoney.new(1.00, :usd), BigMoney.new(1.50, :usd), BigMoney.new(0, :usd)
|
61
|
+
assert_equal BigDecimal.new("-0.50"), (m1 - m2).amount
|
62
|
+
assert_equal BigDecimal.new("0.50"), (m2 - m1).amount
|
63
|
+
assert_equal BigDecimal.new("1.00"), (m1 - m3).amount
|
64
|
+
assert_equal BigDecimal.new("-1.00"), (m3 - m1).amount
|
65
|
+
assert_equal BigDecimal.new("-2.50"), (m2 - 4).amount
|
66
|
+
assert_equal BigDecimal.new("-2.50"), (m2 - 4.00).amount
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_multiply
|
70
|
+
m1, m2, m3 = BigMoney.new(2.00, :usd), BigMoney.new(1.50, :usd), BigMoney.new(0, :usd)
|
71
|
+
assert_equal BigDecimal.new("4.00"), (m1 * m1).amount
|
72
|
+
assert_equal BigDecimal.new("3.00"), (m1 * m2).amount
|
73
|
+
assert_equal BigDecimal.new("3.00"), (m2 * m1).amount
|
74
|
+
assert_equal BigDecimal.new("0"), (m1 * m3).amount
|
75
|
+
assert_equal BigDecimal.new("0"), (m3 * m1).amount
|
76
|
+
assert_equal BigDecimal.new("0"), (m3 * m3).amount
|
77
|
+
assert_equal BigDecimal.new("3.00"), (m2 * 2).amount
|
78
|
+
assert_equal BigDecimal.new("3.00"), (m2 * 2.00).amount
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_divide
|
82
|
+
m1, m2, m3 = BigMoney.new(2.00, :usd), BigMoney.new(1.50, :usd), BigMoney.new(0, :usd)
|
83
|
+
assert_equal BigDecimal.new("1.00"), (m1 / m1).amount
|
84
|
+
assert_equal BigDecimal.new("1.3333333333333334"), (m1 / m2).amount
|
85
|
+
assert_equal BigDecimal.new("0.75"), (m2 / m1).amount
|
86
|
+
assert_equal BigDecimal.new("0"), (m3 / m1).amount
|
87
|
+
assert((m1 / m3).amount.infinite?)
|
88
|
+
assert((m3 / m3).amount.nan?)
|
89
|
+
assert((m1 / 0).amount.infinite?)
|
90
|
+
assert((m3 / 0).amount.nan?)
|
91
|
+
assert_equal BigDecimal.new("0.75"), (m2 / 2).amount
|
92
|
+
assert_equal BigDecimal.new("0.75"), (m2 / 2.00).amount
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_to_s
|
96
|
+
assert_equal '1.00', BigMoney.new(1).to_s
|
97
|
+
assert_equal '1.50', BigMoney.new(1.5).to_s
|
98
|
+
assert_equal '-11.50', BigMoney.new(-11.5).to_s
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_to_formatted_s
|
102
|
+
assert_equal '1.00', BigMoney.new(1).to_formatted_s("%.2f")
|
103
|
+
assert_equal '$1.00', BigMoney.new(1).to_formatted_s("$%.2f")
|
104
|
+
assert_equal 'USD $1.00', BigMoney.new(1).to_formatted_s("%s $%.2f")
|
105
|
+
assert_equal '$1.00 USD', BigMoney.new(1).to_formatted_s("$%.2f %s")
|
106
|
+
assert_equal '$1 USD', BigMoney.new(1).to_formatted_s("$%.0f %s")
|
107
|
+
assert_equal '$1.00 <span class="currency">USD</span>', BigMoney.new(1).to_formatted_s('$%.2f <span class="currency">%s</span>')
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_to_i
|
111
|
+
assert_equal(1, BigMoney.new(1).to_i)
|
112
|
+
assert_equal(1, BigMoney.new(1.5).to_i)
|
113
|
+
assert_equal(-11, BigMoney.new(-11.5).to_i)
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_to_f
|
117
|
+
assert_in_delta(1.0, BigMoney.new(1).to_f, 0.000001)
|
118
|
+
assert_in_delta(1.5, BigMoney.new(1.5).to_f, 0.000001)
|
119
|
+
assert_in_delta(-11.5, BigMoney.new(-11.5).to_f, 0.000001)
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_neg
|
123
|
+
assert_equal BigMoney.new(-1), -BigMoney.new(1)
|
124
|
+
assert_equal BigMoney.new(1), -BigMoney.new(-1)
|
125
|
+
assert_equal BigMoney.new(1), -BigMoney.new(-1)
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mroch-BigMoney
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marshall Roch
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-21 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Represents an amount of money in a particular currency. Backed by BigDecimal, so is safe from float rounding errors.
|
17
|
+
email: mroch@cmu.edu
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- bigmoney.gemspec
|
26
|
+
- History.txt
|
27
|
+
- Manifest.txt
|
28
|
+
- LICENSE.txt
|
29
|
+
- Rakefile
|
30
|
+
- README.txt
|
31
|
+
- lib/big_money.rb
|
32
|
+
- lib/big_money/big_money.rb
|
33
|
+
- lib/big_money/core_extensions.rb
|
34
|
+
- test/test_big_money.rb
|
35
|
+
has_rdoc: false
|
36
|
+
homepage: http://github.com/mroch/bigmoney
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.0.1
|
58
|
+
signing_key:
|
59
|
+
specification_version: 2
|
60
|
+
summary: Represents an amount of money in a particular currency.
|
61
|
+
test_files:
|
62
|
+
- test/test_big_money.rb
|