bd_money 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/bd_money.gemspec +2 -2
- data/lib/bd_money/bd_money.rb +14 -3
- data/spec/bd_money_spec.rb +13 -0
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.10
|
data/bd_money.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{bd_money}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.10"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Adrian Madrid"]
|
12
|
-
s.date = %q{2012-03-
|
12
|
+
s.date = %q{2012-03-22}
|
13
13
|
s.description = %q{This library makes it easier to deal with Money values, storing them as BigDecimal to avoid floating-point math errors.}
|
14
14
|
s.email = %q{aemadrid@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/bd_money/bd_money.rb
CHANGED
@@ -39,7 +39,9 @@ class Money
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def amount=(value)
|
42
|
-
if value.
|
42
|
+
if value.respond_to?(:to_big_decimal)
|
43
|
+
@amount = value.to_big_decimal
|
44
|
+
elsif value.is_a?(BigDecimal)
|
43
45
|
@amount = value
|
44
46
|
else
|
45
47
|
str = self.class.clean value
|
@@ -149,6 +151,14 @@ class Money
|
|
149
151
|
amount == 0
|
150
152
|
end
|
151
153
|
|
154
|
+
def to_money
|
155
|
+
self
|
156
|
+
end
|
157
|
+
|
158
|
+
def to_big_decimal
|
159
|
+
amount
|
160
|
+
end
|
161
|
+
|
152
162
|
def round_amount(this_precision = precision, this_round_mode = round_mode)
|
153
163
|
this_round_mode = BigDecimal.const_get("ROUND_#{this_round_mode.to_s.upcase}") if this_round_mode.is_a?(Symbol)
|
154
164
|
amount.round this_precision, this_round_mode
|
@@ -183,8 +193,9 @@ class Money
|
|
183
193
|
defaults = args.first.is_a?(::Symbol) ? FORMATS[args.shift] : FORMATS[:default]
|
184
194
|
options = args.last.is_a?(::Hash) ? args.pop : { }
|
185
195
|
|
186
|
-
unit
|
187
|
-
spacer
|
196
|
+
unit = options[:unit] || defaults[:unit]
|
197
|
+
spacer = options[:spacer] || defaults[:spacer]
|
198
|
+
spacer = '' if unit.to_s.empty?
|
188
199
|
delimiter = options[:delimiter] || defaults[:delimiter]
|
189
200
|
separator = options[:separator] || defaults[:separator]
|
190
201
|
separator = '' if precision == 0
|
data/spec/bd_money_spec.rb
CHANGED
@@ -297,6 +297,15 @@ describe Money do
|
|
297
297
|
end
|
298
298
|
end
|
299
299
|
|
300
|
+
describe "to_big_decimal" do
|
301
|
+
let(:bd_amt) { BigDecimal.new(amt) }
|
302
|
+
it { bd_amt.should == subject.to_big_decimal }
|
303
|
+
end
|
304
|
+
|
305
|
+
describe "to_money" do
|
306
|
+
it { should == subject.to_money }
|
307
|
+
end
|
308
|
+
|
300
309
|
describe "format" do
|
301
310
|
let(:amt) { '1234567.12' }
|
302
311
|
let(:neg_amt) { '-1234567.12' }
|
@@ -305,11 +314,15 @@ describe Money do
|
|
305
314
|
it { subject.formatted(:no_commas).should == '$ 1234567.12' }
|
306
315
|
it { subject.formatted(:precision => 1).should == '$ 1,234,567.1' }
|
307
316
|
it { subject.formatted(:no_commas, :precision => 1).should == '$ 1234567.1' }
|
317
|
+
it { subject.formatted(:general, :last => '%').should == '1234567.12%' }
|
318
|
+
it { subject.formatted(:general, :spacer => " ", :last => '%').should == '1234567.12%' }
|
308
319
|
it { neg_subject.formatted().should == '$ -1,234,567.12' }
|
309
320
|
it { neg_subject.formatted(:no_cents).should == '$ -1,234,567' }
|
310
321
|
it { neg_subject.formatted(:no_commas).should == '$ -1234567.12' }
|
311
322
|
it { neg_subject.formatted(:precision => 1).should == '$ -1,234,567.1' }
|
312
323
|
it { neg_subject.formatted(:no_commas, :precision => 1).should == '$ -1234567.1' }
|
324
|
+
it { neg_subject.formatted(:general, :last => '%').should == '-1234567.12%' }
|
325
|
+
it { neg_subject.formatted(:general, :spacer => " ", :last => '%').should == '-1234567.12%' }
|
313
326
|
end
|
314
327
|
|
315
328
|
describe "forwarded" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bd_money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 10
|
10
|
+
version: 0.0.10
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adrian Madrid
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-03-
|
18
|
+
date: 2012-03-22 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|