lib/money/money.rb in ShadowBelmolve-money-2.3.3 vs lib/money/money.rb in ShadowBelmolve-money-2.3.4
- old
+ new
@@ -82,11 +82,12 @@
@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)
+ other_money.respond_to?(:cents) && cents == other_money.cents &&
+ other_money.respond_to?(:currency) && bank.same_currency?(currency, other_money.currency)
end
def <=>(other_money)
if bank.same_currency?(currency, other_money.currency)
cents <=> other_money.cents
@@ -147,10 +148,44 @@
# Calculate self + simple interest
def simple_interest(rate, count = 1, period = 12)
Money.new(rate / 100 / period * cents * count)
end
+ #Round to nearest coin value
+ # basically, we don't have coins for cents in CZK,
+ # our smallest fraction is 0.50CZK
+ #
+ #Money.new(14_58).round_to_coin(50) => 14.50
+ def round_to_coin(coin)
+ coef = 1.0/coin
+ val = (cents * coef).round / coef
+ Money.new(val, currency)
+ end
+
+ #Returns array a where
+ # a[0] is price _after_ applying tax (tax base)
+ # a[1] is tax
+ def tax_brakedown(tax)
+ _tax = (cents * (tax / 100.0)).round
+ [Money.new(cents + _tax, currency), Money.new(_tax, currency)]
+ end
+
+ #Returns array a where
+ # a[0] is price _before_ applying tax (tax base)
+ # a[1] is tax
+ def tax_reverse_brakedown(tax)
+ coef = tax/100.0
+ [Money.new((cents / (1+coef)).round, currency),
+ Money.new((cents*coef/(1+coef)).round, currency) ]
+ end
+
+ # Just a helper if you got tax inputs in percentage.
+ # Ie. add_tax(20) => cents * 1.20
+ def add_tax(tax)
+ tax_brakedown(tax)[0]
+ end
+
# Split money in number of installments
#
# Money.new(10_00).split_in_installments(3)
# => [ 3.34, 3.33, 3.33 ] (All Money instances)
#
@@ -167,15 +202,9 @@
# 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, 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)
- Money.new(cents + cents / 100 * tax)
end
# Format the price according to several rules
# Currently supported are :with_currency, :no_cents, :symbol and :html
#