| Class | Globalize::Currency |
| In: |
vendor/plugins/globalize/lib/globalize/models/currency.rb
|
| Parent: | Object |
This is what you use for representing money in your ActiveRecord models. It stores values as integers internally and in the database, to safeguard precision and rounding.
More importantly for globalization freaks, it prints out the amount correctly in the current locale, via the handy format method. Try it!
Example:
class Product < ActiveRecord::Base
composed_of :price, :class_name => "Globalize::Currency",
:mapping => [ %w(price cents) ]
end
product.price -> "SFr. 483'232.43"
| cents | [R] |
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 35
35: def self.no_cents; @@no_cents end
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 36
36: def self.no_cents=(val); @@no_cents = val end
Parse a string or number into a currency object. Easier to use than new.
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 144
144: def self.parse(num)
145: case
146: when num.is_a?(String)
147: raise ArgumentError, "Not an amount (#{num})" if num.delete("^0-9").empty?
148: _dollars, _cents = num.delete("^0-9.").split('.', 2)
149: _cents = 0 if !_cents
150: Currency.new(_dollars.to_i * 100 + _cents.to_i)
151: when num.is_a?(Numeric)
152: Currency.new(num * 100)
153: when num.is_a?(NilClass)
154: Currency.na
155: else
156: raise ArgumentError, "Unrecognized object #{num.class.name} for Currency"
157: end
158: end
Multiply money by amount
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 83
83: def *(amount)
84: return Currency.new(nil) if na?
85: new_cents = amount * cents;
86: new_cents = new_cents.round if new_cents.respond_to? :round
87: Currency.new(new_cents)
88: end
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 68
68: def +(other_money)
69: raise TypeError, "parameter to Currency#+ must be Currency object" unless
70: other_money.kind_of? Currency
71: (na? || other_money.na?) ? Currency.na :
72: Currency.new(cents + other_money.cents)
73: end
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 75
75: def -(other_money)
76: raise TypeError, "parameter to Currency#+ must be Currency object" unless
77: other_money.kind_of? Currency
78: (na? || other_money.na?) ? Currency.na :
79: Currency.new(cents - other_money.cents)
80: end
Divide money by amount
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 91
91: def /(amount)
92: return Currency.new(nil) if na?
93: new_cents = cents / amount;
94: new_cents = new_cents.round if new_cents.respond_to? :round
95: Currency.new(new_cents)
96: end
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 54
54: def <=>(other)
55: if other.respond_to? :cents
56: if na?
57: other.na? ? 0 : 1
58: else
59: other.na? ? -1 : cents <=> other.cents
60: end
61: elsif other.kind_of? Integer
62: na? ? 1 : cents <=> other
63: else
64: raise "can only compare with money or integer"
65: end
66: end
Returns the formatted version of the amount, but without the currency symbol. If unlocalized is true, do not format the number according to the current locale, so Currency.new(1234567) -> 12345.67. This is useful for sending the amount to payment gateways.
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 124
124: def amount(unlocalized = false, force_cents = false)
125: return nil if na?
126: decimal_sep = unlocalized ?
127: '.' :
128: (Locale.active? ?
129: (Locale.active.currency_decimal_sep || Locale.active.decimal_sep || '.') :
130: '.')
131: dollar_str = unlocalized ? dollar_part.to_s : dollar_part.localize
132: result = dollar_str
133: result << decimal_sep + sprintf("%02d", cent_part) unless
134: self.class.no_cents && !force_cents
135: return result
136: end
Is the value 0? Is it free?
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 166
166: def empty?
167: cents == 0
168: end
Returns the formatted version of the amount in local currency. If :code => true is specified, format using international 3-letter currency code. If :country is specified as well, use that country’s currency code for formatting.
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 102
102: def format(options = {})
103: return :no_price_available.t("call for price") if na?
104:
105: if options[:code]
106: currency_code = options[:country] ?
107: options[:country].currency_code :
108: ( Locale.active? ? Locale.active.currency_code : nil )
109: currency_code ? self.amount + " " + currency_code : self.amount
110: else
111: if Locale.active?
112: fmt = Locale.active.currency_format || '$%n'
113: fmt.sub('%n', self.amount)
114: else
115: self.amount
116: end
117: end
118: end
Is the value unknown?
# File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 171
171: def na?
172: cents.nil?
173: end