money-collection 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/money/collection.rb +9 -21
- data/lib/money/collection/version.rb +1 -1
- data/spec/money/collection_spec.rb +21 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9dbf6e30305deef2f8b131375fbd5db5431d17f7
|
4
|
+
data.tar.gz: eaec499c10c2a72b21b855c099840f53508be107
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf26ece7e7182b27b578456681969abc24586b6ef3d212dc536b086e4f1901b54047d62b037855f8c50d2685c7cd03147263d1c32d47b68fddb8cd9d33262bfe
|
7
|
+
data.tar.gz: c133ab32c399853444bcd1e681a283963d32f9ec97c7a08ba578b67752668b1727be287372e8e705affe6a1ba356908eef18035e0f84d0f780b5d6fd45680d90
|
data/lib/money/collection.rb
CHANGED
@@ -20,29 +20,26 @@ class Money
|
|
20
20
|
|
21
21
|
if @group_by_currency.size == 1
|
22
22
|
sum = self.class.sum_single_currency(@collection)
|
23
|
+
if target_currency
|
24
|
+
sum = sum.exchange_to(target_currency)
|
25
|
+
end
|
23
26
|
else
|
24
27
|
sums_per_currency = @group_by_currency.values.map{|moneys|
|
25
28
|
self.class.sum_single_currency(moneys)
|
26
29
|
}
|
27
30
|
|
28
|
-
#
|
29
|
-
# move it to the front so it has precedence over other currencies.
|
31
|
+
# Convert to target_currency if specified
|
30
32
|
if target_currency
|
31
|
-
target_currency =
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
33
|
+
target_currency = Money::Currency.wrap(target_currency)
|
34
|
+
sums_per_currency.map!{|s|
|
35
|
+
s.exchange_to(target_currency)
|
36
|
+
}
|
36
37
|
end
|
37
38
|
|
38
39
|
sum = self.class.sum_basic(sums_per_currency)
|
39
40
|
end
|
40
41
|
|
41
|
-
|
42
|
-
return sum
|
43
|
-
else
|
44
|
-
return sum.exchange_to(target_currency)
|
45
|
-
end
|
42
|
+
sum
|
46
43
|
end
|
47
44
|
|
48
45
|
def max
|
@@ -112,14 +109,5 @@ class Money
|
|
112
109
|
total_fractional = moneys.reduce(0){|fractional, money| fractional += money.fractional }
|
113
110
|
Money.new(total_fractional, moneys[0].currency)
|
114
111
|
end
|
115
|
-
|
116
|
-
def self.convert_to_currency_object(currency)
|
117
|
-
case currency
|
118
|
-
when String, Symbol
|
119
|
-
::Money::Currency.new(currency)
|
120
|
-
when ::Money::Currency
|
121
|
-
currency
|
122
|
-
end
|
123
|
-
end
|
124
112
|
end
|
125
113
|
end
|
@@ -12,6 +12,16 @@ describe Money::Collection do
|
|
12
12
|
|
13
13
|
Money.add_rate("FOO", "USD", 0.5)
|
14
14
|
Money.add_rate("USD", "FOO", 2)
|
15
|
+
|
16
|
+
|
17
|
+
bar = {
|
18
|
+
:iso_code => "BAR",
|
19
|
+
:subunit_to_unit => 100,
|
20
|
+
}
|
21
|
+
Money::Currency.register(bar)
|
22
|
+
|
23
|
+
Money.add_rate("BAR", "FOO", 1)
|
24
|
+
Money.add_rate("FOO", "BAR", nil) # Disallow conversion
|
15
25
|
end
|
16
26
|
|
17
27
|
it 'has version' do
|
@@ -89,6 +99,17 @@ describe Money::Collection do
|
|
89
99
|
c.sum('usd').must_equal Money.new(11,:usd)
|
90
100
|
end
|
91
101
|
|
102
|
+
it 'returns sum in the specified currency and without unnecessary intermediate conversion' do
|
103
|
+
ary = [
|
104
|
+
Money.new(10,:usd),
|
105
|
+
Money.new(1,:bar),
|
106
|
+
]
|
107
|
+
|
108
|
+
c = Money::Collection.new(ary)
|
109
|
+
|
110
|
+
c.sum('foo').must_equal Money.new(21,:foo)
|
111
|
+
end
|
112
|
+
|
92
113
|
it 'returns sum large number of Money' do
|
93
114
|
10.times do
|
94
115
|
# force first bunch to be FOO, the a bunch of USD,
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: money-collection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lulalala
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: money
|