cashrb 1.0.3 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +7 -0
- data/README.md +12 -0
- data/cashrb.gemspec +1 -1
- data/lib/cashrb/cash.rb +9 -25
- data/test/test_cash.rb +24 -17
- metadata +2 -2
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
cashrb v1.1.0
|
2
|
+
=============
|
3
|
+
- use "include Comparable" instead of custom functions
|
4
|
+
- #== no longer throws an IncompatibleCurrency exception
|
5
|
+
- cleanup tests that we auto passing
|
6
|
+
- if currency implements :cents_in_whole, use it
|
7
|
+
|
1
8
|
cashrb v1.0.3
|
2
9
|
=============
|
3
10
|
- change to "require 'cashrb'" to avoid clash with 'cash' gem
|
data/README.md
CHANGED
@@ -58,3 +58,15 @@ Usage
|
|
58
58
|
|
59
59
|
n = Cash.new(1.9)
|
60
60
|
n.cents #=> 1
|
61
|
+
|
62
|
+
# If your currency object implements :cents_in_whole, we'll go ahead and
|
63
|
+
# use that.
|
64
|
+
|
65
|
+
module MyCurrency
|
66
|
+
def self.cents_in_whole
|
67
|
+
10
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
n = Cash.new(9, :currency => MyCurrency)
|
72
|
+
n.to_f #=> 0.9
|
data/cashrb.gemspec
CHANGED
data/lib/cashrb/cash.rb
CHANGED
@@ -3,15 +3,15 @@ require 'bigdecimal'
|
|
3
3
|
##
|
4
4
|
# Provides methods for performing financial calculations without using floats.
|
5
5
|
class Cash
|
6
|
+
include Comparable
|
7
|
+
|
6
8
|
class IncompatibleCurrency < ArgumentError; end
|
7
9
|
|
8
10
|
DEFAULT_CENTS_IN_WHOLE = 100
|
9
11
|
DEFAULT_ROUNDING_METHOD = BigDecimal::ROUND_HALF_UP
|
10
12
|
DEFAULT_CURRENCY = nil
|
11
13
|
|
12
|
-
CURRENCY_AWARE_METHODS = [
|
13
|
-
:+, :-, :/, :%, :divmod, :==, :<=>, :>, :<, :>=, :<=
|
14
|
-
]
|
14
|
+
CURRENCY_AWARE_METHODS = [:+, :-, :/, :%, :divmod, :<=>]
|
15
15
|
|
16
16
|
class << self
|
17
17
|
attr_accessor :default_cents_in_whole
|
@@ -70,30 +70,10 @@ class Cash
|
|
70
70
|
[Cash.new(quotient), Cash.new(remainder)]
|
71
71
|
end
|
72
72
|
|
73
|
-
def ==(value)
|
74
|
-
@cents == value.cents
|
75
|
-
end
|
76
|
-
|
77
73
|
def <=>(value)
|
78
74
|
@cents <=> value.cents
|
79
75
|
end
|
80
76
|
|
81
|
-
def >(value)
|
82
|
-
@cents > value.cents
|
83
|
-
end
|
84
|
-
|
85
|
-
def <(value)
|
86
|
-
@cents < value.cents
|
87
|
-
end
|
88
|
-
|
89
|
-
def >=(value)
|
90
|
-
@cents >= value.cents
|
91
|
-
end
|
92
|
-
|
93
|
-
def <=(value)
|
94
|
-
@cents <= value.cents
|
95
|
-
end
|
96
|
-
|
97
77
|
def to_s
|
98
78
|
return self.cents.to_s if @cents_in_whole == 1
|
99
79
|
|
@@ -138,10 +118,14 @@ class Cash
|
|
138
118
|
:currency => self.class.default_currency,
|
139
119
|
}.merge(options)
|
140
120
|
|
141
|
-
@
|
121
|
+
@currency = opts[:currency]
|
122
|
+
@cents_in_whole = if @currency.respond_to? :cents_in_whole
|
123
|
+
@currency.cents_in_whole
|
124
|
+
else
|
125
|
+
opts[:cents_in_whole]
|
126
|
+
end
|
142
127
|
@decimal_places = decimal_places(@cents_in_whole)
|
143
128
|
@rounding_method = opts[:rounding_method]
|
144
|
-
@currency = opts[:currency]
|
145
129
|
nil
|
146
130
|
end
|
147
131
|
|
data/test/test_cash.rb
CHANGED
@@ -50,6 +50,14 @@ class TestCash < MiniTest::Unit::TestCase
|
|
50
50
|
assert_equal :usd, rs.currency
|
51
51
|
end
|
52
52
|
|
53
|
+
def test_new_with_currency_respond_to_cents_in_whole
|
54
|
+
currency = MiniTest::Mock.new
|
55
|
+
currency.expect(:cents_in_whole, 10)
|
56
|
+
|
57
|
+
rs = Cash.new(9, :currency => currency)
|
58
|
+
assert_equal 0.9, rs.to_f
|
59
|
+
end
|
60
|
+
|
53
61
|
def test_plus_with_Cash_Cash
|
54
62
|
rs = Cash.new(6) + Cash.new(4)
|
55
63
|
assert_equal Cash.new(10), rs
|
@@ -127,18 +135,17 @@ class TestCash < MiniTest::Unit::TestCase
|
|
127
135
|
|
128
136
|
def test_equal_to_when_equal_to
|
129
137
|
rs = Cash.new(6) == Cash.new(6)
|
130
|
-
assert
|
138
|
+
assert rs
|
131
139
|
end
|
132
140
|
|
133
141
|
def test_equal_to_when_not_equal_to
|
134
142
|
rs = Cash.new(6) == Cash.new(4)
|
135
|
-
refute
|
143
|
+
refute rs
|
136
144
|
end
|
137
145
|
|
138
146
|
def test_equal_to_with_different_currencies
|
139
|
-
|
140
|
-
|
141
|
-
end
|
147
|
+
rs = Cash.new(6) == Cash.new(6, :currency => :usd)
|
148
|
+
refute rs
|
142
149
|
end
|
143
150
|
|
144
151
|
def test_compare_when_greater_than
|
@@ -164,17 +171,17 @@ class TestCash < MiniTest::Unit::TestCase
|
|
164
171
|
|
165
172
|
def test_greater_than_when_greater_than
|
166
173
|
rs = Cash.new(6) > Cash.new(4)
|
167
|
-
assert
|
174
|
+
assert rs
|
168
175
|
end
|
169
176
|
|
170
177
|
def test_greater_than_when_less_than
|
171
178
|
rs = Cash.new(4) > Cash.new(6)
|
172
|
-
refute
|
179
|
+
refute rs
|
173
180
|
end
|
174
181
|
|
175
182
|
def test_greater_than_when_equal_to
|
176
183
|
rs = Cash.new(6) > Cash.new(6)
|
177
|
-
refute
|
184
|
+
refute rs
|
178
185
|
end
|
179
186
|
|
180
187
|
def test_greater_than_with_different_currencies
|
@@ -185,17 +192,17 @@ class TestCash < MiniTest::Unit::TestCase
|
|
185
192
|
|
186
193
|
def test_less_than_when_greater_than
|
187
194
|
rs = Cash.new(6) < Cash.new(4)
|
188
|
-
refute
|
195
|
+
refute rs
|
189
196
|
end
|
190
197
|
|
191
198
|
def test_less_than_when_less_than
|
192
199
|
rs = Cash.new(4) < Cash.new(6)
|
193
|
-
assert
|
200
|
+
assert rs
|
194
201
|
end
|
195
202
|
|
196
203
|
def test_less_than_when_equal_to
|
197
204
|
rs = Cash.new(6) < Cash.new(6)
|
198
|
-
refute
|
205
|
+
refute rs
|
199
206
|
end
|
200
207
|
|
201
208
|
def test_less_than_with_different_currencies
|
@@ -206,17 +213,17 @@ class TestCash < MiniTest::Unit::TestCase
|
|
206
213
|
|
207
214
|
def test_greater_than_or_equal_to_when_greater_than
|
208
215
|
rs = Cash.new(6) >= Cash.new(4)
|
209
|
-
assert
|
216
|
+
assert rs
|
210
217
|
end
|
211
218
|
|
212
219
|
def test_greater_than_or_equal_to_when_less_than
|
213
220
|
rs = Cash.new(4) >= Cash.new(6)
|
214
|
-
refute
|
221
|
+
refute rs
|
215
222
|
end
|
216
223
|
|
217
224
|
def test_greater_than_or_equal_to_when_equal_to
|
218
225
|
rs = Cash.new(6) >= Cash.new(6)
|
219
|
-
assert
|
226
|
+
assert rs
|
220
227
|
end
|
221
228
|
|
222
229
|
def test_greater_than_or_equal_to_with_different_currencies
|
@@ -227,17 +234,17 @@ class TestCash < MiniTest::Unit::TestCase
|
|
227
234
|
|
228
235
|
def test_less_than_or_equal_to_when_greater_than
|
229
236
|
rs = Cash.new(6) <= Cash.new(4)
|
230
|
-
refute
|
237
|
+
refute rs
|
231
238
|
end
|
232
239
|
|
233
240
|
def test_less_than_or_equal_to_when_less_than
|
234
241
|
rs = Cash.new(4) <= Cash.new(6)
|
235
|
-
assert
|
242
|
+
assert rs
|
236
243
|
end
|
237
244
|
|
238
245
|
def test_less_than_or_equal_to_when_equal_to
|
239
246
|
rs = Cash.new(6) <= Cash.new(6)
|
240
|
-
assert
|
247
|
+
assert rs
|
241
248
|
end
|
242
249
|
|
243
250
|
def test_less_than_or_equal_to_with_different_currencies
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cashrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-04-01 00:00:00.000000000 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
description: Dead simple gem to work with Money/Currency without the hassle of Floats
|