investtools-money 0.0.2 → 0.0.3

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: 8458cdfb679db2487e1e81878eeaef25157c8ee6
4
- data.tar.gz: 6da5f97104de3b942a00e17271d16f8249e788cf
3
+ metadata.gz: 1b51a40a8b2b92c769f751c4941380b3f4b5c48c
4
+ data.tar.gz: 0a51a081cfc1472e637b15028235c5793d16a82c
5
5
  SHA512:
6
- metadata.gz: c55882b31924e6265a721a8caa17f7fe735698d517fde014e9d57b24f0a42181760920f584cf4bf82ebd6d9cbdc76ab8ef3716afdcec79c4777179b464e5b98b
7
- data.tar.gz: 7e99f98c1c92adea31259773dc0fc8803425bfdf495f8bdd088000690b06fe92f4874da22d70f034488f068ccc25fc1b22b0623e260f7d655c3b3f86ace0cde4
6
+ metadata.gz: e5f69b2abd97bff616361a44df8909bb4ff220e0b05d432ab9a0c1117b23bfe1d2d22867e27b51af247bfb0076ad9cbce6ce4f9450aacede3b0af6957ffec432
7
+ data.tar.gz: 0abf4f8f0ec685dde72fc1904187572ec5ee308dd6c644dd5dda313a375c18e3622663b68426042a78433aa0fcc8cc5fd48e50dec669228eb448a24ae640afe2
@@ -46,9 +46,17 @@ class Money
46
46
  protected
47
47
 
48
48
  def calculate(operation, other)
49
- other = other.amount if other.kind_of?(Money)
49
+ if other.kind_of?(Money)
50
+ if other.date != date and other.date != nil and date != nil
51
+ raise "Can't calculate the amount with different dates."
52
+ end
53
+ new_date = date || other.date
54
+ other = other.exchange_to(currency).amount
55
+ else
56
+ new_date = date
57
+ end
50
58
  if other.kind_of?(Numeric)
51
- Money.new(amount.send(operation, other), currency, date)
59
+ Money.new(amount.send(operation, other), currency, new_date)
52
60
  else
53
61
  raise TypeError, "#{other.class} can't be coerced into Money"
54
62
  end
@@ -1,5 +1,5 @@
1
1
  require 'money/base'
2
2
 
3
3
  class Money
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
@@ -25,6 +25,29 @@ describe Money do
25
25
  expect { Money.new(1_000.0, 'BRL') - 'TEST' }.to raise_error(TypeError)
26
26
  end
27
27
  end
28
+ context 'when the given money is in other currency' do
29
+ it 'converts the given amount before the operation' do
30
+ expect(Money.new(1_150.0, 'BRL') - Money.new(100.0, 'USD')).to eq Money.new(1_000.0, 'BRL')
31
+ end
32
+ end
33
+ context 'when the given money is on different date' do
34
+ it 'raises an error' do
35
+ expect { Money.new(1_000.0, 'BRL', Date.parse('2015-06-24')) - Money.new(100.0, 'BRL', Date.parse('2015-06-25')) }.
36
+ to raise_error("Can't calculate the amount with different dates.")
37
+ end
38
+ end
39
+ context 'when the given money is on nil date' do
40
+ it 'substracts the given money amount from amount and returns a new Money' do
41
+ expect(Money.new(1_000.0, 'BRL', Date.parse('2015-06-24')) - Money.new(100.0, 'BRL')).
42
+ to eq Money.new(900.0, 'BRL', Date.parse('2015-06-24'))
43
+ end
44
+ end
45
+ context 'when the date is nil and the given money has a date' do
46
+ it 'substracts the given money amount from amount and returns a new Money with the given money date' do
47
+ expect(Money.new(1_000.0, 'BRL') - Money.new(100.0, 'BRL', Date.parse('2015-06-24'))).
48
+ to eq Money.new(900.0, 'BRL', Date.parse('2015-06-24'))
49
+ end
50
+ end
28
51
  end
29
52
  describe '#/' do
30
53
  it 'divides the amount by the given value and returns a new object' do
@@ -40,6 +63,23 @@ describe Money do
40
63
  expect { Money.new(1_000.0, 'BRL') / 'TEST' }.to raise_error(TypeError)
41
64
  end
42
65
  end
66
+ context 'when the given money is in other currency' do
67
+ it 'converts the given amount before the operation' do
68
+ expect(Money.new(1_500.0, 'BRL') / Money.new(100.0, 'USD')).to eq Money.new(10.0, 'BRL')
69
+ end
70
+ end
71
+ context 'when the given money is on nil date' do
72
+ it 'divides the amount by the given money amount and returns a new Money' do
73
+ expect(Money.new(1_000.0, 'BRL', Date.parse('2015-06-24')) / Money.new(10.0, 'BRL')).
74
+ to eq Money.new(100.0, 'BRL', Date.parse('2015-06-24'))
75
+ end
76
+ end
77
+ context 'when the date is nil and the given money has a date' do
78
+ it 'divides the amount by the given money amount and returns a new Money with the given money date' do
79
+ expect(Money.new(1_000.0, 'BRL') / Money.new(10.0, 'BRL', Date.parse('2015-06-24'))).
80
+ to eq Money.new(100.0, 'BRL', Date.parse('2015-06-24'))
81
+ end
82
+ end
43
83
  end
44
84
  describe '#*' do
45
85
  it 'multiplies the amount by the given value and returns a new object' do
@@ -55,6 +95,23 @@ describe Money do
55
95
  expect { Money.new(1_000.0, 'BRL') * 'TEST' }.to raise_error(TypeError)
56
96
  end
57
97
  end
98
+ context 'when the given money is in other currency' do
99
+ it 'converts the given amount before the operation' do
100
+ expect(Money.new(1_000.0, 'BRL') * Money.new(1.0, 'USD')).to eq Money.new(1_500.0, 'BRL')
101
+ end
102
+ end
103
+ context 'when the given money is on nil date' do
104
+ it 'multiplies the amount by the given money amount and returns a new Money' do
105
+ expect(Money.new(100.0, 'BRL', Date.parse('2015-06-24')) * Money.new(10.0, 'BRL')).
106
+ to eq Money.new(1_000.0, 'BRL', Date.parse('2015-06-24'))
107
+ end
108
+ end
109
+ context 'when the date is nil and the given money has a date' do
110
+ it 'multiplies the amount by the given money amount and returns a new Money with the given money date' do
111
+ expect(Money.new(100.0, 'BRL') * Money.new(10.0, 'BRL', Date.parse('2015-06-24'))).
112
+ to eq Money.new(1_000.0, 'BRL', Date.parse('2015-06-24'))
113
+ end
114
+ end
58
115
  end
59
116
  describe '#+' do
60
117
  it 'adds the amount to the given value and returns a new object' do
@@ -70,6 +127,23 @@ describe Money do
70
127
  expect { Money.new(1_000.0, 'BRL') + 'TEST' }.to raise_error(TypeError)
71
128
  end
72
129
  end
130
+ context 'when the given money is in other currency' do
131
+ it 'converts the given amount before the operation' do
132
+ expect(Money.new(1_000.0, 'BRL') + Money.new(100.0, 'USD')).to eq Money.new(1_150.0, 'BRL')
133
+ end
134
+ end
135
+ context 'when the given money is on nil date' do
136
+ it 'adds the amount to the given money amount and returns a new Money' do
137
+ expect(Money.new(1_000.0, 'BRL', Date.parse('2015-06-24')) + Money.new(100.0, 'BRL')).
138
+ to eq Money.new(1_100.0, 'BRL', Date.parse('2015-06-24'))
139
+ end
140
+ end
141
+ context 'when the date is nil and the given money has a date' do
142
+ it 'adds the amount to the given money amount and returns a new Money with the given money date' do
143
+ expect(Money.new(1_000.0, 'BRL') + Money.new(100.0, 'BRL', Date.parse('2015-06-24'))).
144
+ to eq Money.new(1_100.0, 'BRL', Date.parse('2015-06-24'))
145
+ end
146
+ end
73
147
  end
74
148
  describe '#coerce' do
75
149
  it 'wraps first operator in a Money' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: investtools-money
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - André Aizim Kelmanson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-11 00:00:00.000000000 Z
11
+ date: 2015-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  version: '0'
126
126
  requirements: []
127
127
  rubyforge_project:
128
- rubygems_version: 2.2.2
128
+ rubygems_version: 2.4.6
129
129
  signing_key:
130
130
  specification_version: 4
131
131
  summary: A Ruby Library for dealing with money and currency conversion.