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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 31d48e73a143672af436d2128462573a8f9b7d70
4
- data.tar.gz: 2c8528a4f6163392762d25ddea387eb6649d860a
3
+ metadata.gz: 9dbf6e30305deef2f8b131375fbd5db5431d17f7
4
+ data.tar.gz: eaec499c10c2a72b21b855c099840f53508be107
5
5
  SHA512:
6
- metadata.gz: 9027fda3114e23db23061ab4a88d31e071a89d690a11c4de994c0c8b6517d6a41f5e28aa1620dff27285be12aac433612e95871ff3609d6bc457aeb82d358603
7
- data.tar.gz: 85632bb6ae43b0eac67779762a35909c1d00894ba67c51cb727413f78f977538d3349d1af189c1094e5f2822c55390dfefe098bf2f0dd3b34eedf5bdb3bb3313
6
+ metadata.gz: cf26ece7e7182b27b578456681969abc24586b6ef3d212dc536b086e4f1901b54047d62b037855f8c50d2685c7cd03147263d1c32d47b68fddb8cd9d33262bfe
7
+ data.tar.gz: c133ab32c399853444bcd1e681a283963d32f9ec97c7a08ba578b67752668b1727be287372e8e705affe6a1ba356908eef18035e0f84d0f780b5d6fd45680d90
@@ -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
- # If target_currency is specified, and is in collection,
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 = self.class.convert_to_currency_object(target_currency)
32
- if index = sums_per_currency.find_index{|money| money.currency == target_currency}
33
- money = sums_per_currency.delete_at(index)
34
- sums_per_currency.unshift money
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
- if target_currency.nil?
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
@@ -1,5 +1,5 @@
1
1
  class Money
2
2
  class Collection
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  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.1
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-10-28 00:00:00.000000000 Z
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: money