Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add Bank#exchange_with

  • Loading branch information...
commit f62d576cb6e2f0f86387d774218f4b8f6378fdb8 1 parent 4d688e7
@semmons99 semmons99 authored
Showing with 68 additions and 0 deletions.
  1. +22 −0 lib/money/bank.rb
  2. +46 −0 test/bank_spec.rb
View
22 lib/money/bank.rb
@@ -34,6 +34,28 @@ def exchange(cents, from_currency, to_currency, &block)
ex.to_s.to_i
end
+ def exchange_with(from, to_currency, &block)
+ return from if same_currency?(from.currency, to_currency)
+
+ rate = get_rate(from.currency, to_currency)
+ unless rate
+ raise Money::Bank::UnknownRate, "No conversion rate known for '#{from.currency.iso_code}' -> '#{to_currency}'"
+ end
+ _to_currency_ = Currency.wrap(to_currency)
+
+ cents = from.cents / (from.currency.subunit_to_unit.to_f / _to_currency_.subunit_to_unit.to_f)
+
+ ex = cents * rate
+ ex = if block_given?
+ block.call(ex)
+ elsif @rounding_method
+ @rounding_method.call(ex)
+ else
+ ex.to_s.to_i
+ end
+ Money.new(ex, _to_currency_)
+ end
+
private
def rate_key_for(from, to)
View
46 test/bank_spec.rb
@@ -137,6 +137,41 @@
@bank.exchange(10, 'USD', 'EUR', &proc).should == 14
end
end
+
+ describe '#exchange_with' do
+ before :each do
+ @bank.send(:set_rate, 'USD', 'EUR', 1.33)
+ end
+
+ it 'should accept str' do
+ lambda{@bank.exchange_with(Money.new(100, 'USD'), 'EUR')}.should_not raise_exception
+ end
+
+ it 'should accept currency' do
+ lambda{@bank.exchange_with(Money.new(100, 'USD'), Money::Currency.wrap('EUR'))}.should_not raise_exception
+ end
+
+ it 'should exchange one currency to another' do
+ @bank.exchange_with(Money.new(100, 'USD'), 'EUR').should == Money.new(133, 'EUR')
+ end
+
+ it 'should truncate extra digits' do
+ @bank.exchange_with(Money.new(10, 'USD'), 'EUR').should == Money.new(13, 'EUR')
+ end
+
+ it 'should raise an UnknownCurrency exception when an unknown currency is requested' do
+ lambda{@bank.exchange_with(Money.new(100, 'USD'), 'BBB')}.should raise_exception(Money::Currency::UnknownCurrency)
+ end
+
+ it 'should raise an UnknownRate exception when an unknown rate is requested' do
+ lambda{@bank.exchange_with(Money.new(100, 'USD'), 'JPY')}.should raise_exception(Money::Bank::UnknownRate)
+ end
+
+ it 'should accept a custom truncation method' do
+ proc = Proc.new{|n| n.ceil}
+ @bank.exchange_with(Money.new(10, 'USD'), 'EUR', &proc).should == Money.new(14, 'EUR')
+ end
+ end
end
describe '#new with &block' do
@@ -156,5 +191,16 @@
@bank.exchange(10, 'USD', 'EUR', &proc).should == 15
end
end
+
+ describe '#exchange_with' do
+ it 'should use a stored truncation method' do
+ @bank.exchange_with(Money.new(10, 'USD'), 'EUR').should == Money.new(14, 'EUR')
+ end
+
+ it 'should use a custom truncation method over a stored one' do
+ proc = Proc.new{|n| n.ceil+1}
+ @bank.exchange_with(Money.new(10, 'USD'), 'EUR', &proc).should == Money.new(15, 'EUR')
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.