Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved naming for international use: renamed anything _dollar to _a…

…mount and created a synonym from _dollar to _amount for backwards compatibility

Added myself to AUTHORS and added changelog entry.
  • Loading branch information...
commit 3a06fda38fec675d1039f91f2c1612a543b64ba4 1 parent e57544c
Mikael Wikman authored
Showing with 80 additions and 31 deletions.
  1. +1 −0  AUTHORS
  2. +1 −0  CHANGELOG.md
  3. +32 −8 lib/money/money.rb
  4. +46 −23 spec/money_spec.rb
View
1  AUTHORS
@@ -35,6 +35,7 @@ Matt Jankowski
Matthew McEachen
Michael Irwin
Michael Reinsch
+Mikael Wikman
Mike Połétyn
Neil Middleton
Olek Janiszewski
View
1  CHANGELOG.md
@@ -2,6 +2,7 @@
## master
+- Added Money.new_with_amount and Money#amount. Money.new_with_dollars and Money#dollars remain as synonyms.
- Calling Bank::Base.instance doesn't make Bank::VariableExchange.instance
return Bank::Base.instance anymore (semaperepelitsa)
- Update Turkmenistan manat from TMM to TMT currency (GH-181). [Thanks @Exoth]
View
40 lib/money/money.rb
@@ -152,37 +152,43 @@ def self.euro(cents)
Money.new(cents, "EUR")
end
- # Creates a new Money object of +amount+ value in dollars,
+ # Creates a new Money object of +amount+ value ,
# with given +currency+.
#
- # The amount value is expressed in +dollars+
- # where the +dollar+ is the main monetary unit,
+ # The amount value is expressed in the main monetary unit,
# opposite to the subunit-based representation
# used internally by this library called +cents+.
#
- # @param [Numeric] amount The money amount, in dollars.
+ # @param [Numeric] amount The money amount, in main monetary unit.
# @param [Currency, String, Symbol] currency The currency format.
# @param [Money::Bank::*] bank The exchange bank to use.
#
# @return [Money]
#
# @example
- # Money.new_with_dollars(100)
+ # Money.new_with_amount(100)
# #=> #<Money @cents=10000 @currency="USD">
- # Money.new_with_dollars(100, "USD")
+ # Money.new_with_amount(100, "USD")
# #=> #<Money @cents=10000 @currency="USD">
- # Money.new_with_dollars(100, "EUR")
+ # Money.new_with_amount(100, "EUR")
# #=> #<Money @cents=10000 @currency="EUR">
#
# @see Money.new
#
- def self.new_with_dollars(amount, currency = Money.default_currency, bank = Money.default_bank)
+ def self.new_with_amount(amount, currency = Money.default_currency, bank = Money.default_bank)
money = from_numeric(amount, currency)
# Hack! You can't change a bank
money.instance_variable_set("@bank", bank)
money
end
+ # Synonym of #new_with_amount
+ #
+ # @see Money.new_with_amount
+ def self.new_with_dollars(*args)
+ self.new_with_amount(*args)
+ end
+
# Adds a new exchange rate to the default bank and return the rate.
#
# @param [Currency, String, Symbol] from_currency Currency to exchange from.
@@ -235,16 +241,34 @@ def initialize(cents, currency = Money.default_currency, bank = Money.default_ba
# Returns the value of the money in dollars,
# instead of in cents.
#
+ # Synonym of #amount
+ #
# @return [Float]
#
# @example
# Money.new(100).dollars # => 1.0
# Money.new_with_dollars(1).dollar # => 1.0
#
+ # @see #amount
# @see #to_f
# @see #cents
#
def dollars
+ amount
+ end
+
+ # Returns the numerical value of the money
+ #
+ # @return [Float]
+ #
+ # @example
+ # Money.new(100).amount # => 1.0
+ # Money.new_with_amount(1).amount # => 1.0
+ #
+ # @see #to_f
+ # @see #cents
+ #
+ def amount
to_f
end
View
69 spec/money_spec.rb
@@ -41,45 +41,55 @@
end
describe ".new_with_dollars" do
+ it "is synonym of #new_with_amount" do
+ MoneyExpectation = Class.new(Money)
+ def MoneyExpectation.new_with_amount *args
+ args
+ end
+ MoneyExpectation.new_with_dollars("expectation").should == ["expectation"]
+ end
+ end
+
+ describe ".new_with_amount" do
it "converts given amount to cents" do
- Money.new_with_dollars(1).should == Money.new(100)
- Money.new_with_dollars(1, "USD").should == Money.new(100, "USD")
- Money.new_with_dollars(1, "EUR").should == Money.new(100, "EUR")
+ Money.new_with_amount(1).should == Money.new(100)
+ Money.new_with_amount(1, "USD").should == Money.new(100, "USD")
+ Money.new_with_amount(1, "EUR").should == Money.new(100, "EUR")
end
it "respects :subunit_to_unit currency property" do
- Money.new_with_dollars(1, "USD").should == Money.new(1_00, "USD")
- Money.new_with_dollars(1, "TND").should == Money.new(1_000, "TND")
- Money.new_with_dollars(1, "CLP").should == Money.new(1, "CLP")
+ Money.new_with_amount(1, "USD").should == Money.new(1_00, "USD")
+ Money.new_with_amount(1, "TND").should == Money.new(1_000, "TND")
+ Money.new_with_amount(1, "CLP").should == Money.new(1, "CLP")
end
it "does not loose precision" do
- Money.new_with_dollars(1234).cents.should == 1234_00
- Money.new_with_dollars(100.37).cents.should == 100_37
- Money.new_with_dollars(BigDecimal.new('1234')).cents.should == 1234_00
+ Money.new_with_amount(1234).cents.should == 1234_00
+ Money.new_with_amount(100.37).cents.should == 100_37
+ Money.new_with_amount(BigDecimal.new('1234')).cents.should == 1234_00
end
it "accepts optional currency" do
- m = Money.new_with_dollars(1)
+ m = Money.new_with_amount(1)
m.currency.should == Money.default_currency
- m = Money.new_with_dollars(1, Money::Currency.wrap("EUR"))
+ m = Money.new_with_amount(1, Money::Currency.wrap("EUR"))
m.currency.should == Money::Currency.wrap("EUR")
- m = Money.new_with_dollars(1, "EUR")
+ m = Money.new_with_amount(1, "EUR")
m.currency.should == Money::Currency.wrap("EUR")
end
it "accepts optional bank" do
- m = Money.new_with_dollars(1)
+ m = Money.new_with_amount(1)
m.bank.should == Money.default_bank
- m = Money.new_with_dollars(1, "EUR", bank = Object.new)
+ m = Money.new_with_amount(1, "EUR", bank = Object.new)
m.bank.should == bank
end
it "is associated to the singleton instance of Bank::VariableExchange by default" do
- Money.new_with_dollars(0).bank.should be(Money::Bank::VariableExchange.instance)
+ Money.new_with_amount(0).bank.should be(Money::Bank::VariableExchange.instance)
end
end
@@ -165,21 +175,34 @@
end
end
- describe "#dollars" do
+ describe "#amount" do
it "returns the amount of cents as dollars" do
- Money.new(1_00).dollars.should == 1
- Money.new_with_dollars(1).dollars.should == 1
+ Money.new(1_00).amount.should == 1
+ Money.new_with_amount(1).amount.should == 1
end
it "respects :subunit_to_unit currency property" do
- Money.new(1_00, "USD").dollars.should == 1
- Money.new(1_000, "TND").dollars.should == 1
- Money.new(1, "CLP").dollars.should == 1
+ Money.new(1_00, "USD").amount.should == 1
+ Money.new(1_000, "TND").amount.should == 1
+ Money.new(1, "CLP").amount.should == 1
end
it "does not loose precision" do
- Money.new(100_37).dollars.should == 100.37
- Money.new_with_dollars(100.37).dollars.should == 100.37
+ Money.new(100_37).amount.should == 100.37
+ Money.new_with_amount(100.37).amount.should == 100.37
+ end
+ end
+
+ describe "#dollars" do
+ it "is synonym of #amount" do
+ m = Money.new(0)
+
+ # Make a small expectation
+ def m.amount
+ 5
+ end
+
+ m.dollars.should == 5
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.