collectiveidea / money Goto Github PK
View Code? Open in Web Editor NEWThis project forked from aflatter/money
Leetsoft's Money library
License: MIT License
This project forked from aflatter/money
Leetsoft's Money library
License: MIT License
== Money 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://gems.github.com collectiveidea-money You can find the source at: 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_cents, :currency => :rate_currency money :discount, :cents => :discount_cents end 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. == Class configuration Two const class variables are available to tailor Money to your needs. If you don't need currency exchange at all, just ignore those. === Default Currency By default Money defaults to USD as its currency. This can be overwritten using Money.default_currency = "CAD" If you use rails, the environment.rb is a very good place to put this. === Currency Exchange The second parameter is a bit more complex. It lets you provide your own implementation of the currency exchange service. By default Money throws an exception when trying to call .exchange_to. A second minimalist implementation is provided which lets you supply custom exchange rates: Money.bank = VariableExchangeBank.new Money.bank.add_rate("USD", "CAD", 1.24515) Money.bank.add_rate("CAD", "USD", 0.803115) Money.us_dollar(100).exchange_to("CAD") => Money.ca_dollar(124) Money.ca_dollar(100).exchange_to("USD") => Money.us_dollar(80) There is nothing stopping you from creating bank objects which scrape www.xe.com for the current rates or just return rand(2) == Code If you have any improvements please email them to tobi [at] leetsoft.com
It gives the error:
"no such file to load -- money/rails"
Instead, I used:
config.gem "money", lib => "money/rails"
in my environment .rb file
Nice library. Tried to install as gem from github for use with ActiveRecord. Ruby kept reporting that the "money" method in my model was missing:
undefined method `money' for #Class:0x244517c
... turns out the rails.rb file was not being included by money.rb.
Added:
require 'money/rails'
to my money.rb, and all is well. Also, it appears that the comments in money.rb about usage with ActiveRecord reference an earlier implementation.
https://github.com/collectiveidea/money/blob/master/lib/money/core_extensions.rb#L42
precision = scan(/\.(\d+)/).to_s.length
scan(/\.(\d+)/)
returns an array of arrays.
In Ruby 1.8, Array#to_s was something like Array#join
But in Ruby 1.9 Array#to_s is more like Array#inspect.
Because of this, in Ruby 1.9
'1.99'.to_money.precision # => 8
m=Money.new -500
helper.number_to_currency m
fails with exception
NoMethodError: private method
sub' called for #<Money:0x5273698 @precision=2, @currency="USD", @cents=-500> from /home/deploy/staging2/shared/bundle/ruby/1.8/gems/actionpack-3.0.8/lib/action_view/helpers/number_helper.rb:128:in
number_to_currency'
This is the offending line in Rails's number_to_currency function:
https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/number_helper.rb#L123
It seems if you would implement an "abs" method for a Money object, it would work.
need to add this option to the composed_of declaration:
:constructor => lambda { |cents, currency, precision=options[:precision]| ::Money.new(cents, currency, precision) },
in order to get the correct results with large precisions (I'm using 6)
def abs
if cents < 0 then
Money.new(-cents, currency, precision)
else
self
end
end
Thought you might want to include the following :with_thousands_separator rule in the format method:
if rules.include?(:with_currency)
formatted << " "
formatted << '<span class="currency">' if rules.include?(:html)
formatted << currency
formatted << '</span>' if rules.include?(:html)
end
if rules.include?(:with_thousands_separator)
formatted = formatted.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\1,")
end
formatted
... borrowed from:
http://snippets.dzone.com/posts/show/693
Thanks for clarifying the include.
Cheers...
should be:
# override collectiveidea-money-1.7.4/lib/money/core_extensions.rb
# since it causes high precision strings to go through in 1e-06 format
# and mess up the to_money
class Float
def to_money(precision = nil)
sprintf("%f",self).to_money(precision)
end
end
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.