Skip to content

Commit

Permalink
Update the README
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Jan 22, 2009
1 parent 6c11afd commit 8ecb768
Showing 1 changed file with 21 additions and 40 deletions.
61 changes: 21 additions & 40 deletions README
Original file line number Diff line number Diff line change
@@ -1,71 +1,52 @@
== Money class
== Money

This money class is based on the example from the ActiveRecord doc:
http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html

Its in production use at http://www.snowdevil.ca and I haven't found any major issues
so far.
The main reason to open source it is because It might be useful to other people and
I hope i'll get some feedback on how to improve the class.

I bundled the exporter with the money class since some tests depend on it and I figured
that most applications which need to deal with Money also need to deal with proper
exporting.
This library makes it easier to deal with Money values, storing them as integers to avoid floating-point math errors.

== Download

Preferred method of installation is gem:

gem install --source http://dist.leetsoft.com money
gem install --source http://gems.github.com collectiveidea-money

Alternatively you can get the library packed
You can find the source at:

http://dist.leetsoft.com/pkg/

== Usage

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) %(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
http://github.com/collectiveidea/money

== Rails

There is a rails extension that makes it easier to store money values in the database.

class Product < ActiveRecord::Base
money :price
validates_numericality_of :price_in_cents, :greater_than => 0
end

This assumes that there is a price_in_cents (integer) column in the database, which can
be changed by passing the :cents option. You can also specify the :currency option to
save the currency to a field in the database.

class Room < ActiveRecord::Base
money :rate, :cents => :rate_in_cents, :currency => :rate_currency
money :discount, :cents => :discount_in_cents
money :rate, :cents => :rate_cents, :currency => :rate_currency
money :discount, :cents => :discount_cents
end

You can pass a String, Fixnum, or Float as a parameter to the setter, and it will call
#to_money to convert it to a Money object. This makes it convenient for using money
fields in forms.
You can set the attribute to a String, Fixnum, or Float and it will call #to_money to
convert it to a Money object. This makes it convenient for using money fields in forms.

r = Room.new :rate => "100.00"
r.rate # returns <Money:0x249ef9c @currency="USD", @cents=10000>

By default, money values will be stored with a precision of 2 (cents). If you need to store different precisions, such as to the nearest tenth of a cent, you can specify the +:precision+ option:

class Room < ActiveRecord::Base
money :rate, :precision => 3
end

r = Room.new :rate => "100"
r.rate.format # returns $100.000
r.rate = "100.995"
r.rate.format # returns $100.995

To use the Rails functionality, install money as a plugin, or require 'money/rails'.
This version is compatible with Rails 2.2. For compatibility with previous versions of
Rails, check out the rails-2.1 branch.
Expand Down

0 comments on commit 8ecb768

Please sign in to comment.