Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
git-svn-id: svn://leetsoft.com/money/trunk@10 da9ee5fb-4aef-0310-b2bf…
Browse files Browse the repository at this point in the history
…-b6c93f260786
  • Loading branch information
tobi committed Jul 21, 2005
1 parent 7c4efbf commit 65588d7
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README
Expand Up @@ -22,7 +22,7 @@ Alternatively you can get the library packed


http://dist.leetsoft.com/pkg/ http://dist.leetsoft.com/pkg/


== Useage == Usage


Use the compose_of helper to let active record deal with embedding the money Use the compose_of helper to let active record deal with embedding the money
object in your models. The following example requires a cents and a currency field. object in your models. The following example requires a cents and a currency field.
Expand Down
6 changes: 4 additions & 2 deletions Rakefile
Expand Up @@ -5,7 +5,7 @@ require 'rake/rdoctask'
require 'rake/gempackagetask' require 'rake/gempackagetask'
require 'rake/contrib/rubyforgepublisher' require 'rake/contrib/rubyforgepublisher'


PKG_VERSION = "1.3.2" PKG_VERSION = "1.5"
PKG_NAME = "money" PKG_NAME = "money"
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}" PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"


Expand Down Expand Up @@ -79,7 +79,9 @@ spec = Gem::Specification.new do |s|
s.name = PKG_NAME s.name = PKG_NAME
s.version = PKG_VERSION s.version = PKG_VERSION
s.summary = "Class aiding in the handling of Money." s.summary = "Class aiding in the handling of Money."
s.description = "Can be used as composite in ActiveRecord tables." s.description = ['Class aiding in the handling of Money and Currencies.',
'It supports easy pluggable bank objects for customized exchange strategies.'
'Can be used as composite in ActiveRecord tables.'].join('\n')
s.has_rdoc = true s.has_rdoc = true


s.files = %w(README MIT-LICENSE) + Dir['lib/**/*'] s.files = %w(README MIT-LICENSE) + Dir['lib/**/*']
Expand Down
1 change: 1 addition & 0 deletions lib/bank/no_exchange_bank.rb
@@ -1,6 +1,7 @@
class NoExchangeBank# :nodoc: class NoExchangeBank# :nodoc:


def reduce(money, currency) def reduce(money, currency)
return money if money.currency == currency
raise Money::MoneyError.new("Current Money::bank does not support money exchange. Please implement a bank object that does and assign it to the Money class.") raise Money::MoneyError.new("Current Money::bank does not support money exchange. Please implement a bank object that does and assign it to the Money class.")
end end


Expand Down
7 changes: 7 additions & 0 deletions lib/money/core_extensions.rb
Expand Up @@ -23,4 +23,11 @@ def to_money


Money.new(cents, currency) Money.new(cents, currency)
end end
end

# Allow writing of Nil.to_money => Money.empty
class NilClass
def to_money
Money.empty
end
end end
8 changes: 4 additions & 4 deletions lib/money/money.rb
Expand Up @@ -66,16 +66,16 @@ def <=>(other_money)
end end


def +(other_money) def +(other_money)
if currency == other_money.currency if self.cents == 0 or currency == other_money.currency
Money.new(cents + other_money.cents,currency) Money.new(cents + other_money.cents, other_money.currency)
else else
Money.new(cents + other_money.exchange_to(currency).cents,currency) Money.new(cents + other_money.exchange_to(currency).cents,currency)
end end
end end


def -(other_money) def -(other_money)
if currency == other_money.currency if self.cents == 0 or currency == other_money.currency
Money.new(cents - other_money.cents, currency) Money.new(cents - other_money.cents, other_money.currency)
else else
Money.new(cents - other_money.exchange_to(currency).cents, currency) Money.new(cents - other_money.exchange_to(currency).cents, currency)
end end
Expand Down
5 changes: 4 additions & 1 deletion tests/core_extensions_test.rb
Expand Up @@ -21,6 +21,9 @@ def test_string_conversion
assert_equal Money.new(100, 'CAD'), "CAD $1.00".to_money assert_equal Money.new(100, 'CAD'), "CAD $1.00".to_money
assert_equal Money.new(-10000), "-100".to_money assert_equal Money.new(-10000), "-100".to_money
end end


def test_nil
assert_equal Money.empty, nil.to_money
end


end end
5 changes: 5 additions & 0 deletions tests/money_test.rb
Expand Up @@ -69,6 +69,11 @@ def test_divide
assert_equal Money.ca_dollar(100), Money.ca_dollar(200) / 2 assert_equal Money.ca_dollar(100), Money.ca_dollar(200) / 2
end end


def test_empty_can_exchange_currency
assert_equal Money.ca_dollar(100), Money.empty + Money.ca_dollar(100)
assert_equal Money.us_dollar(-100), Money.empty - Money.us_dollar(100)
end

def test_formatting def test_formatting


assert_equal "free", Money.ca_dollar(0).format assert_equal "free", Money.ca_dollar(0).format
Expand Down
6 changes: 6 additions & 0 deletions tests/no_exchange_bank_test.rb
Expand Up @@ -14,5 +14,11 @@ def test_exchange
@bank.reduce(Money.us_dollar(100), "CAD") @bank.reduce(Money.us_dollar(100), "CAD")
end end
end end

def test_work_if_no_exchange_is_required
assert_nothing_raised do
@bank.reduce(Money.ca_dollar(100), "CAD")
end
end


end end
2 changes: 1 addition & 1 deletion tests/variable_exchange_bank_test.rb
Expand Up @@ -3,7 +3,7 @@
require 'test/unit' require 'test/unit'
require 'money' require 'money'


class NoExchangeBankTest < Test::Unit::TestCase class VariableExchangeBankTest < Test::Unit::TestCase


def setup def setup
@bank = VariableExchangeBank.new @bank = VariableExchangeBank.new
Expand Down

0 comments on commit 65588d7

Please sign in to comment.