Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

git-svn-id: svn://leetsoft.com/money/trunk@10 da9ee5fb-4aef-0310-b2bf…

…-b6c93f260786
  • Loading branch information...
commit 65588d71f92519d979a3a8040722a8a4b8219bf7 1 parent 7c4efbf
tobi authored
2  README
View
@@ -22,7 +22,7 @@ Alternatively you can get the library packed
http://dist.leetsoft.com/pkg/
-== Useage
+== Usage
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.
6 Rakefile
View
@@ -5,7 +5,7 @@ require 'rake/rdoctask'
require 'rake/gempackagetask'
require 'rake/contrib/rubyforgepublisher'
-PKG_VERSION = "1.3.2"
+PKG_VERSION = "1.5"
PKG_NAME = "money"
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
@@ -79,7 +79,9 @@ spec = Gem::Specification.new do |s|
s.name = PKG_NAME
s.version = PKG_VERSION
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.files = %w(README MIT-LICENSE) + Dir['lib/**/*']
1  lib/bank/no_exchange_bank.rb
View
@@ -1,6 +1,7 @@
class NoExchangeBank# :nodoc:
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.")
end
7 lib/money/core_extensions.rb
View
@@ -23,4 +23,11 @@ def to_money
Money.new(cents, currency)
end
+end
+
+# Allow writing of Nil.to_money => Money.empty
+class NilClass
+ def to_money
+ Money.empty
+ end
end
8 lib/money/money.rb
View
@@ -66,16 +66,16 @@ def <=>(other_money)
end
def +(other_money)
- if currency == other_money.currency
- Money.new(cents + other_money.cents,currency)
+ if self.cents == 0 or currency == other_money.currency
+ Money.new(cents + other_money.cents, other_money.currency)
else
Money.new(cents + other_money.exchange_to(currency).cents,currency)
end
end
def -(other_money)
- if currency == other_money.currency
- Money.new(cents - other_money.cents, currency)
+ if self.cents == 0 or currency == other_money.currency
+ Money.new(cents - other_money.cents, other_money.currency)
else
Money.new(cents - other_money.exchange_to(currency).cents, currency)
end
5 tests/core_extensions_test.rb
View
@@ -21,6 +21,9 @@ def test_string_conversion
assert_equal Money.new(100, 'CAD'), "CAD $1.00".to_money
assert_equal Money.new(-10000), "-100".to_money
end
-
+
+ def test_nil
+ assert_equal Money.empty, nil.to_money
+ end
end
5 tests/money_test.rb
View
@@ -69,6 +69,11 @@ def test_divide
assert_equal Money.ca_dollar(100), Money.ca_dollar(200) / 2
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
assert_equal "free", Money.ca_dollar(0).format
6 tests/no_exchange_bank_test.rb
View
@@ -14,5 +14,11 @@ def test_exchange
@bank.reduce(Money.us_dollar(100), "CAD")
end
end
+
+ def test_work_if_no_exchange_is_required
+ assert_nothing_raised do
+ @bank.reduce(Money.ca_dollar(100), "CAD")
+ end
+ end
end
2  tests/variable_exchange_bank_test.rb
View
@@ -3,7 +3,7 @@
require 'test/unit'
require 'money'
-class NoExchangeBankTest < Test::Unit::TestCase
+class VariableExchangeBankTest < Test::Unit::TestCase
def setup
@bank = VariableExchangeBank.new
Please sign in to comment.
Something went wrong with that request. Please try again.