investtools-money 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/money/money.rb +10 -2
- data/lib/money/version.rb +1 -1
- data/spec/lib/money/money_spec.rb +74 -0
- metadata +3 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 1b51a40a8b2b92c769f751c4941380b3f4b5c48c
         | 
| 4 | 
            +
              data.tar.gz: 0a51a081cfc1472e637b15028235c5793d16a82c
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: e5f69b2abd97bff616361a44df8909bb4ff220e0b05d432ab9a0c1117b23bfe1d2d22867e27b51af247bfb0076ad9cbce6ce4f9450aacede3b0af6957ffec432
         | 
| 7 | 
            +
              data.tar.gz: 0abf4f8f0ec685dde72fc1904187572ec5ee308dd6c644dd5dda313a375c18e3622663b68426042a78433aa0fcc8cc5fd48e50dec669228eb448a24ae640afe2
         | 
    
        data/lib/money/money.rb
    CHANGED
    
    | @@ -46,9 +46,17 @@ class Money | |
| 46 46 | 
             
              protected
         | 
| 47 47 |  | 
| 48 48 | 
             
              def calculate(operation, other)
         | 
| 49 | 
            -
                 | 
| 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,  | 
| 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
         | 
    
        data/lib/money/version.rb
    CHANGED
    
    
| @@ -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. | 
| 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- | 
| 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. | 
| 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.
         |