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"

Methods

*   +   -   /   <=>   amount   empty?   format   free   na   na?   new   no_cents   no_cents=   parse   to_currency   to_s  

Included Modules

Comparable

Attributes

cents  [R] 

Public Class methods

Returns the Currency object representing a price of 0.

[Source]

    # File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 42
42:     def self.free
43:       @@free
44:     end

Returns the Currency object representing an unknown price.

[Source]

    # File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 50
50:     def self.na
51:       @@na
52:     end

Creates a new Currency object, given cents. This is used to tie into the ActiveRecord aggregation stuff.

[Source]

    # File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 30
30:     def initialize(cents)
31:       @cents = cents.nil? ? nil : cents.to_i
32:     end

[Source]

    # File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 35
35:     def self.no_cents; @@no_cents end

[Source]

    # 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.

[Source]

     # 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

Public Instance methods

Multiply money by amount

[Source]

    # 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

[Source]

    # 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

[Source]

    # 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

[Source]

    # 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

[Source]

    # 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.

[Source]

     # 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?

[Source]

     # 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.

[Source]

     # 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?

[Source]

     # File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 171
171:     def na?
172:       cents.nil?
173:     end

Conversion to self

[Source]

     # File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 161
161:     def to_currency
162:       self
163:     end

Same as format with no arguments.

[Source]

     # File vendor/plugins/globalize/lib/globalize/models/currency.rb, line 139
139:     def to_s
140:       self.format
141:     end

[Validate]