lib/money/money.rb in ShadowBelmolve-money-2.3.1 vs lib/money/money.rb in ShadowBelmolve-money-2.3.3
- old
+ new
@@ -26,24 +26,24 @@
# the default currency, which is used when <tt>Money.new</tt> is called
# without an explicit currency argument. The default value is "USD".
attr_accessor :default_currency
end
+ self.default_bank = VariableExchangeBank.instance
+ self.default_currency = "USD"
+
CURRENCIES = {
"USD" => { :delimiter => ",", :separator => ".", :symbol => "$" },
"CAD" => { :delimiter => ",", :separator => ".", :symbol => "$" },
"HKD" => { :delimiter => ",", :separator => ".", :symbol => "$" },
"SGD" => { :delimiter => ",", :separator => ".", :symbol => "$" },
"BRL" => { :delimiter => ".", :separator => ",", :symbol => "R$" },
"EUR" => { :delimiter => ",", :separator => ".", :symbol => '€', :html => '€' },
"GBP" => { :delimiter => ",", :separator => ".", :symbol => '£', :html => '£' },
"JPY" => { :delimiter => ".", :separator => ".", :symbol => '¥', :html => '¥' },
}
- self.default_bank = VariableExchangeBank.instance
- self.default_currency = "USD"
-
# Create a new money object with value 0.
def self.empty(currency = default_currency)
Money.new(0, currency)
end
@@ -65,23 +65,23 @@
# Creates a new Money object of the given value, using the Brazilian Real currency.
def self.real(cents)
Money.new(cents, "BRL")
end
- def self.add_rate(from_currency, to_currency, rate)
- Money.default_bank.add_rate(from_currency, to_currency, rate)
+ def self.add_rate(currency, rate)
+ Money.default_bank.add_rate(currency, rate)
end
# Creates a new money object.
# Money.new(100)
#
# Alternativly you can use the convinience methods like
# Money.ca_dollar and Money.us_dollar
- def initialize(cents, currency = Money.default_currency, bank = Money.default_bank)
- @cents = cents.round
- @currency = currency
- @bank = bank
+ def initialize(cents, currency = nil, bank = nil)
+ @cents = cents.to_i
+ @currency = currency || Money.default_currency
+ @bank = bank || Money.default_bank
end
# Do two money objects equal? Only works if both objects are of the same currency
def ==(other_money)
cents == other_money.cents && bank.same_currency?(currency, other_money.currency)
@@ -94,18 +94,20 @@
cents <=> other_money.exchange_to(currency).cents
end
end
def +(other_money)
+ other_money = Money.new(other_money) unless other_money.is_a? Money
if 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)
+ other_money = Money.new(other_money) unless other_money.is_a? Money
if 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
@@ -136,38 +138,39 @@
cents == 0
end
# Calculates compound interest
# Returns a money object with the sum of self + it
- def compound_interest(rate,count=1)
- Money.new(cents * ((1 + rate / 100.0 / 12) ** count - 1))
+ def compound_interest(rate, count = 1, period = 12)
+ Money.new(cents * ((1 + rate / 100.0 / period) ** count - 1))
end
# Calculate self + simple interest
- def simple_interest(rate,count=1)
- Money.new(rate/100/12*cents*count)
+ def simple_interest(rate, count = 1, period = 12)
+ Money.new(rate / 100 / period * cents * count)
end
# Split money in number of installments
#
# Money.new(10_00).split_in_installments(3)
# => [ 3.34, 3.33, 3.33 ] (All Money instances)
#
- def split_in_installments(fixnum,extra=nil,*opts)
+ def split_in_installments(fixnum, order=false)
wallet = Wallet.new(fixnum, Money.new(cents/fixnum,currency))
to_add = cents % fixnum
to_add.times { |m| wallet[m] += Money.new(1) }
+ wallet.reverse! if order
wallet
end
# Split money in installments based on payment value
#
# Money.new(1000_00).split_in_installments(Money.new(300_00))
# => [ 334_00, 333_00, 333_00 ] (All Money instances)
#
- def in_installments_of(other_money,first=false)
- split_in_installments(cents/other_money.cents,first)
+ def in_installments_of(other_money, order=false)
+ split_in_installments(cents/other_money.cents, order)
end
# Just a helper if you got tax inputs in percentage.
# Ie. add_tax(20) => cents * 1.20
def add_tax(tax)
@@ -218,10 +221,11 @@
symbol = ""
end
else
symbol = CURRENCIES[currency][:symbol]
end
+ self.currency
if rules[:no_cents]
formatted = sprintf("#{symbol}%d", cents.to_f / 100)
formatted.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{CURRENCIES[currency][:delimiter]}")
else
@@ -237,12 +241,13 @@
formatted << " "
formatted << '<span class="currency">' if rules[:html]
formatted << currency
formatted << '</span>' if rules[:html]
end
+ formatted.gsub!(CURRENCIES[currency][:symbol],CURRENCIES[currency][:html]) if rules[:html]
formatted
- end
+ end
def normalize_formatting_rules(rules)
if rules.size == 1
rules = rules.pop
rules = { rules => true } if rules.is_a?(Symbol)
@@ -257,9 +262,14 @@
# Money.ca_dollar(100).to_s => "1.00"
def to_s
sprintf("%.2f", cents / 100.0)
+ end
+
+ # Money.new(123).to_i => "100"
+ def to_i
+ cents
end
# Money.ca_dollar(100).to_f => "1.0"
def to_f
cents / 100.0