Class Money
In: lib/money/money.rb
Parent: Object

Usage with ActiveRecord

Use the compose_of helper to let active record deal with embedding the money object in your models. The following example requires a cents and a currency field.

  class ProductUnit < ActiveRecord::Base
    belongs_to :product
    composed_of :price, :class_name => "Money", :mapping => [ %w(cents cents), %w(currency currency) ]

    private
      validate :cents_not_zero

      def cents_not_zero
        errors.add("cents", "cannot be zero or less") unless cents > 0
      end

      validates_presence_of :sku, :currency
      validates_uniqueness_of :sku
  end

Methods

*   +   -   /   <=>   as_ca_dollar   as_ca_euro   as_us_dollar   ca_dollar   cents   empty   eql?   euro   exchange_to   format   new   to_money   to_s   us_dollar   zero?  

Included Modules

Comparable

Attributes

cents  [R] 
currency  [R] 

Public Class methods

Create a new money object using the Canadian dollar currency

[Source]

     # File lib/money/money.rb, line 168
168:   def self.ca_dollar(num)
169:     Money.new(num, "CAD")
170:   end

Create a new money object with value 0

[Source]

     # File lib/money/money.rb, line 163
163:   def self.empty(currency = default_currency)
164:     Money.new(0, currency)
165:   end

Create a new money object using the Euro currency

[Source]

     # File lib/money/money.rb, line 178
178:   def self.euro(num)
179:     Money.new(num, "EUR")
180:   end

Creates a new money object.

 Money.new(100)

Alternativly you can use the convinience methods like Money.ca_dollar and Money.us_dollar

[Source]

    # File lib/money/money.rb, line 51
51:   def initialize(cents, currency = default_currency)
52:     @cents, @currency = cents.round, currency
53:   end

Create a new money object using the American dollar currency

[Source]

     # File lib/money/money.rb, line 173
173:   def self.us_dollar(num)
174:     Money.new(num, "USD")
175:   end

Public Instance methods

multiply money by fixnum

[Source]

    # File lib/money/money.rb, line 96
96:   def *(fixnum)
97:     Money.new(cents * fixnum, currency)    
98:   end

[Source]

    # File lib/money/money.rb, line 68
68:   def +(other_money)
69:     return other_money.dup if cents.zero? 
70:     return dup if other_money.cents.zero?
71: 
72:     if currency == other_money.currency
73:       Money.new(cents + other_money.cents, other_money.currency)
74:     else
75:       Money.new(cents + other_money.exchange_to(currency).cents,currency)
76:     end   
77:   end

[Source]

    # File lib/money/money.rb, line 79
79:   def -(other_money)
80:     return other_money.dup if cents.zero? 
81:     return dup if other_money.cents.zero?
82: 
83:     if self.cents == 0 or currency == other_money.currency
84:       Money.new(cents - other_money.cents, other_money.currency)
85:     else
86:       Money.new(cents - other_money.exchange_to(currency).cents, currency)
87:     end   
88:   end

divide money by fixnum

[Source]

     # File lib/money/money.rb, line 101
101:   def /(fixnum)
102:     Money.new(cents / fixnum, currency)    
103:   end

[Source]

    # File lib/money/money.rb, line 60
60:   def <=>(other_money)
61:     if currency == other_money.currency
62:       cents <=> other_money.cents
63:     else
64:       cents <=> other_money.exchange_to(currency).cents
65:     end
66:   end

Recieve a money object with the same amount as the current Money object in canadian dollar

[Source]

     # File lib/money/money.rb, line 190
190:   def as_ca_dollar
191:     exchange_to("CAD")
192:   end

Recieve a money object with the same amount as the current Money object in euro

[Source]

     # File lib/money/money.rb, line 196
196:   def as_ca_euro
197:     exchange_to("EUR")
198:   end

Recieve a money object with the same amount as the current Money object in american dollar

[Source]

     # File lib/money/money.rb, line 184
184:   def as_us_dollar
185:     exchange_to("USD")
186:   end

get the cents value of the object

[Source]

    # File lib/money/money.rb, line 91
91:   def cents
92:     @cents.to_i
93:   end

Do two money objects equal? Only works if both objects are of the same currency

[Source]

    # File lib/money/money.rb, line 56
56:   def eql?(other_money)
57:     cents == other_money.cents && currency == other_money.currency
58:   end

Recieve the amount of this money object in another currency

[Source]

     # File lib/money/money.rb, line 158
158:   def exchange_to(other_currency)
159:     self.class.bank.reduce(self, other_currency)
160:   end

Format the price according to several rules Currently supported are :with_currency, :no_cents and :html

with_currency:

 Money.ca_dollar(0).format => "free"
 Money.ca_dollar(100).format => "$1.00"
 Money.ca_dollar(100).format(:with_currency) => "$1.00 CAD"
 Money.us_dollar(85).format(:with_currency) => "$0.85 USD"

no_cents:

 Money.ca_dollar(100).format(:no_cents) => "$1"
 Money.ca_dollar(599).format(:no_cents) => "$5"

 Money.ca_dollar(570).format(:no_cents, :with_currency) => "$5 CAD"
 Money.ca_dollar(39000).format(:no_cents) => "$390"

html:

 Money.ca_dollar(570).format(:html, :with_currency) =>  "$5.70 <span class=\"currency\">CAD</span>"

[Source]

     # File lib/money/money.rb, line 132
132:   def format(*rules)
133:     return "free" if cents == 0
134: 
135:     rules = rules.flatten
136: 
137:     if rules.include?(:no_cents)
138:       formatted = sprintf("$%d", cents.to_f / 100  )          
139:     else
140:       formatted = sprintf("$%.2f", cents.to_f / 100  )      
141:     end
142: 
143:     if rules.include?(:with_currency)
144:       formatted << " "
145:       formatted << '<span class="currency">' if rules.include?(:html)
146:       formatted << currency
147:       formatted << '</span>' if rules.include?(:html)
148:     end
149:     formatted
150:   end

Conversation to self

[Source]

     # File lib/money/money.rb, line 201
201:   def to_money
202:     self
203:   end

Money.ca_dollar(100).to_s => "1.00"

[Source]

     # File lib/money/money.rb, line 153
153:   def to_s
154:     sprintf("%.2f", cents.to_f / 100  )
155:   end

Test if the money amount is zero

[Source]

     # File lib/money/money.rb, line 106
106:   def zero?
107:     cents == 0 
108:   end

[Validate]