collectiveidea-money 1.7.3 → 1.7.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README +43 -34
- data/lib/money/rails.rb +10 -10
- data/lib/money.rb +2 -2
- data/lib/support/cattr_accessor.rb +3 -3
- metadata +5 -4
data/README
CHANGED
@@ -1,46 +1,55 @@
|
|
1
|
-
== Money
|
1
|
+
== Money
|
2
2
|
|
3
|
-
This
|
4
|
-
http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html
|
5
|
-
|
6
|
-
Its in production use at http://www.snowdevil.ca and I haven't found any major issues
|
7
|
-
so far.
|
8
|
-
The main reason to open source it is because It might be useful to other people and
|
9
|
-
I hope i'll get some feedback on how to improve the class.
|
10
|
-
|
11
|
-
I bundled the exporter with the money class since some tests depend on it and I figured
|
12
|
-
that most applications which need to deal with Money also need to deal with proper
|
13
|
-
exporting.
|
3
|
+
This library makes it easier to deal with Money values, storing them as integers to avoid floating-point math errors.
|
14
4
|
|
15
5
|
== Download
|
16
6
|
|
17
7
|
Preferred method of installation is gem:
|
18
8
|
|
19
|
-
gem install --source http://
|
9
|
+
gem install --source http://gems.github.com collectiveidea-money
|
20
10
|
|
21
|
-
|
11
|
+
You can find the source at:
|
22
12
|
|
23
|
-
http://
|
24
|
-
|
25
|
-
==
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
validates_presence_of :sku, :currency
|
42
|
-
validates_uniqueness_of :sku
|
13
|
+
http://github.com/collectiveidea/money
|
14
|
+
|
15
|
+
== Rails
|
16
|
+
|
17
|
+
There is a rails extension that makes it easier to store money values in the database.
|
18
|
+
|
19
|
+
class Product < ActiveRecord::Base
|
20
|
+
money :price
|
21
|
+
validates_numericality_of :price_in_cents, :greater_than => 0
|
22
|
+
end
|
23
|
+
|
24
|
+
This assumes that there is a price_in_cents (integer) column in the database, which can
|
25
|
+
be changed by passing the :cents option. You can also specify the :currency option to
|
26
|
+
save the currency to a field in the database.
|
27
|
+
|
28
|
+
class Room < ActiveRecord::Base
|
29
|
+
money :rate, :cents => :rate_cents, :currency => :rate_currency
|
30
|
+
money :discount, :cents => :discount_cents
|
43
31
|
end
|
32
|
+
|
33
|
+
You can set the attribute to a String, Fixnum, or Float and it will call #to_money to
|
34
|
+
convert it to a Money object. This makes it convenient for using money fields in forms.
|
35
|
+
|
36
|
+
r = Room.new :rate => "100.00"
|
37
|
+
r.rate # returns <Money:0x249ef9c @currency="USD", @cents=10000>
|
38
|
+
|
39
|
+
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:
|
40
|
+
|
41
|
+
class Room < ActiveRecord::Base
|
42
|
+
money :rate, :precision => 3
|
43
|
+
end
|
44
|
+
|
45
|
+
r = Room.new :rate => "100"
|
46
|
+
r.rate.format # returns $100.000
|
47
|
+
r.rate = "100.995"
|
48
|
+
r.rate.format # returns $100.995
|
49
|
+
|
50
|
+
To use the Rails functionality, install money as a plugin, or require 'money/rails'.
|
51
|
+
This version is compatible with Rails 2.2. For compatibility with previous versions of
|
52
|
+
Rails, check out the rails-2.1 branch.
|
44
53
|
|
45
54
|
== Class configuration
|
46
55
|
|
data/lib/money/rails.rb
CHANGED
@@ -9,21 +9,21 @@ module ActiveRecord #:nodoc:
|
|
9
9
|
|
10
10
|
module ClassMethods
|
11
11
|
|
12
|
-
def default_currency
|
13
|
-
'USD'
|
14
|
-
end
|
15
|
-
|
16
12
|
def money(name, options = {})
|
17
|
-
options = {:cents => "#{name}_in_cents".to_sym
|
18
|
-
mapping = [[options[:cents]
|
13
|
+
options = {:precision => 2, :cents => "#{name}_in_cents".to_sym}.merge(options)
|
14
|
+
mapping = [[options[:cents], 'cents']]
|
19
15
|
mapping << [options[:currency].to_s, 'currency'] if options[:currency]
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
composed_of name, :class_name => 'Money', :mapping => mapping, :allow_nil => true,
|
17
|
+
:converter => lambda{ |m| m.to_money(options[:precision]) }
|
18
|
+
|
19
|
+
define_method "#{name}_with_cleanup=" do |amount|
|
20
|
+
send "#{name}_without_cleanup=", amount.blank? ? nil : amount.to_money(options[:precision])
|
21
|
+
end
|
22
|
+
alias_method_chain "#{name}=", :cleanup
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
ActiveRecord::Base.send :include, ActiveRecord::Acts::Money
|
29
|
+
ActiveRecord::Base.send :include, ActiveRecord::Acts::Money
|
data/lib/money.rb
CHANGED
@@ -26,7 +26,7 @@ class Class # :nodoc:
|
|
26
26
|
end
|
27
27
|
EOS
|
28
28
|
end
|
29
|
-
end
|
29
|
+
end unless instance_methods.include?('cattr_reader')
|
30
30
|
|
31
31
|
def cattr_writer(*syms)
|
32
32
|
syms.each do |sym|
|
@@ -48,10 +48,10 @@ class Class # :nodoc:
|
|
48
48
|
end
|
49
49
|
EOS
|
50
50
|
end
|
51
|
-
end
|
51
|
+
end unless instance_methods.include?('cattr_writer')
|
52
52
|
|
53
53
|
def cattr_accessor(*syms)
|
54
54
|
cattr_reader(*syms)
|
55
55
|
cattr_writer(*syms)
|
56
|
-
end
|
56
|
+
end unless instance_methods.include?('cattr_accessor')
|
57
57
|
end
|
metadata
CHANGED
@@ -1,20 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collectiveidea-money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.7.
|
4
|
+
version: 1.7.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
+
- Brandon Keepers
|
7
8
|
- Tobias Luetke
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date:
|
13
|
+
date: 2009-01-11 00:00:00 -08:00
|
13
14
|
default_executable:
|
14
15
|
dependencies: []
|
15
16
|
|
16
17
|
description: Class aiding in the handling of Money and Currencies. It supports easy pluggable bank objects for customized exchange strategies. Can be used as composite in ActiveRecord tables.
|
17
|
-
email:
|
18
|
+
email: brandon@opensoul.org
|
18
19
|
executables: []
|
19
20
|
|
20
21
|
extensions: []
|
@@ -31,7 +32,7 @@ files:
|
|
31
32
|
- lib/money/rails.rb
|
32
33
|
- lib/support/cattr_accessor.rb
|
33
34
|
has_rdoc: true
|
34
|
-
homepage: http://
|
35
|
+
homepage: http://github.com/collectiveidea/money
|
35
36
|
post_install_message:
|
36
37
|
rdoc_options: []
|
37
38
|
|