nofxx-money 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +0 -0
- data/MIT-LICENSE +21 -0
- data/Manifest.txt +24 -0
- data/README.rdoc +121 -0
- data/Rakefile +27 -0
- data/lib/money/acts_as_money.rb +40 -0
- data/lib/money/core_extensions.rb +39 -0
- data/lib/money/errors.rb +4 -0
- data/lib/money/money.rb +268 -0
- data/lib/money/variable_exchange_bank.rb +72 -0
- data/lib/money.rb +29 -0
- data/money.gemspec +37 -0
- data/rails/init.rb +3 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/db/database.yml +4 -0
- data/spec/db/schema.rb +16 -0
- data/spec/money/acts_as_money_spec.rb +78 -0
- data/spec/money/core_extensions_spec.rb +39 -0
- data/spec/money/exchange_bank_spec.rb +44 -0
- data/spec/money/money_spec.rb +180 -0
- data/spec/spec_helper.rb +22 -0
- metadata +97 -0
data/History.txt
ADDED
File without changes
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2005 Tobias Lutke
|
2
|
+
Copyright (c) 2008 Phusion
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
History.txt
|
2
|
+
MIT-LICENSE
|
3
|
+
Manifest.txt
|
4
|
+
README.rdoc
|
5
|
+
Rakefile
|
6
|
+
lib/money.rb
|
7
|
+
lib/money/acts_as_money.rb
|
8
|
+
lib/money/core_extensions.rb
|
9
|
+
lib/money/errors.rb
|
10
|
+
lib/money/money.rb
|
11
|
+
lib/money/variable_exchange_bank.rb
|
12
|
+
money.gemspec
|
13
|
+
rails/init.rb
|
14
|
+
script/console
|
15
|
+
script/destroy
|
16
|
+
script/generate
|
17
|
+
spec/db/acts_as_money.sqlite3
|
18
|
+
spec/db/database.yml
|
19
|
+
spec/db/schema.rb
|
20
|
+
spec/money/acts_as_money_spec.rb
|
21
|
+
spec/money/core_extensions_spec.rb
|
22
|
+
spec/money/exchange_bank_spec.rb
|
23
|
+
spec/money/money_spec.rb
|
24
|
+
spec/spec_helper.rb
|
data/README.rdoc
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
= Money $
|
2
|
+
|
3
|
+
This library aids one in handling money and different currencies. Features:
|
4
|
+
|
5
|
+
- Provides a Money class which encapsulates all information about an certain
|
6
|
+
amount of money, such as its value and its currency.
|
7
|
+
- Represents monetary values as integers, in cents. This avoids floating point
|
8
|
+
rounding errors.
|
9
|
+
- Provides APIs for exchanging money from one currency to another.
|
10
|
+
- Has the ability to parse a money string into a Money object.
|
11
|
+
- Provides ActiveRecord "has_money" method.
|
12
|
+
|
13
|
+
Resources:
|
14
|
+
|
15
|
+
- This fork: http://github.com/nofxx/money
|
16
|
+
- Website: http://money.rubyforge.org
|
17
|
+
- RDoc API: http://money.rubyforge.org
|
18
|
+
- Git repository: http://github.com/FooBarWidget/money/tree/master
|
19
|
+
|
20
|
+
== Download
|
21
|
+
|
22
|
+
Install stable releases with the following command:
|
23
|
+
|
24
|
+
gem install nofxx-money
|
25
|
+
|
26
|
+
The development version (hosted on Github) can be installed with:
|
27
|
+
|
28
|
+
gem sources -a http://gems.github.com
|
29
|
+
gem install nofxx-money
|
30
|
+
|
31
|
+
== Usage
|
32
|
+
|
33
|
+
=== Synopsis
|
34
|
+
|
35
|
+
require 'money'
|
36
|
+
|
37
|
+
# 10.00 USD
|
38
|
+
money = Money.new(1000, "USD")
|
39
|
+
money.cents # => 1000
|
40
|
+
money.currency # => "USD"
|
41
|
+
|
42
|
+
Money.new(1000, "USD") == Money.new(1000, "USD") # => true
|
43
|
+
Money.new(1000, "USD") == Money.new(100, "USD") # => false
|
44
|
+
Money.new(1000, "USD") == Money.new(1000, "EUR") # => false
|
45
|
+
|
46
|
+
|
47
|
+
=== Currency Exchange
|
48
|
+
|
49
|
+
Exchanging money is performed through an exchange bank object. The default
|
50
|
+
exchange bank object requires one to manually specify the exchange rate. Here's
|
51
|
+
an example of how it works:
|
52
|
+
|
53
|
+
Money.add_rate("USD", "CAD", 1.24515)
|
54
|
+
Money.add_rate("CAD", "USD", 0.803115)
|
55
|
+
|
56
|
+
Money.us_dollar(100).exchange_to("CAD") # => Money.new(124, "CAD")
|
57
|
+
Money.ca_dollar(100).exchange_to("USD") # => Money.new(80, "USD")
|
58
|
+
|
59
|
+
Comparison and arithmetic operations work as expected:
|
60
|
+
|
61
|
+
Money.new(1000, "USD") <=> Money.new(900, "USD") # => 1; 9.00 USD is smaller
|
62
|
+
Money.new(1000, "EUR") + Money.new(10, "EUR") == Money.new(1010, "EUR")
|
63
|
+
|
64
|
+
Money.add_rate("USD", "EUR", 0.5)
|
65
|
+
Money.new(1000, "EUR") + Money.new(1000, "USD") == Money.new(1500, "EUR")
|
66
|
+
|
67
|
+
There is nothing stopping you from creating bank objects which scrapes
|
68
|
+
www.xe.com for the current rates or just returns <tt>rand(2)</tt>:
|
69
|
+
|
70
|
+
Money.default_bank = ExchangeBankWhichScrapesXeDotCom.new
|
71
|
+
|
72
|
+
|
73
|
+
=== Ruby on Rails
|
74
|
+
|
75
|
+
Use the +has_many+ helper to embed the money object in your models.
|
76
|
+
The following example requires a +price_in_cents+ and a +price_currency+
|
77
|
+
fields.
|
78
|
+
|
79
|
+
config/enviroment.rb
|
80
|
+
|
81
|
+
require.gem 'nofxx-money', :lib => 'money'
|
82
|
+
|
83
|
+
app/models/product.rb
|
84
|
+
|
85
|
+
class Product < ActiveRecord::Base
|
86
|
+
belongs_to :product
|
87
|
+
has_money :price
|
88
|
+
|
89
|
+
private
|
90
|
+
validate :cents_not_zero
|
91
|
+
|
92
|
+
def cents_not_zero
|
93
|
+
errors.add("price_in_cents", "cannot be zero or less") unless cents > 0
|
94
|
+
end
|
95
|
+
|
96
|
+
validates_presence_of :sku, :currency
|
97
|
+
validates_uniqueness_of :sku
|
98
|
+
end
|
99
|
+
|
100
|
+
migration:
|
101
|
+
|
102
|
+
create_table :products do |t|
|
103
|
+
t.integer :price_in_cents
|
104
|
+
t.string :price_currency
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
=== Default Currency
|
109
|
+
|
110
|
+
By default Money defaults to USD as its currency. This can be overwritten using
|
111
|
+
|
112
|
+
Money.default_currency = "CAD"
|
113
|
+
|
114
|
+
If you use Rails, then environment.rb is a very good place to put this.
|
115
|
+
|
116
|
+
|
117
|
+
== TODO
|
118
|
+
|
119
|
+
* Better validation
|
120
|
+
* Interest (almost there..)
|
121
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
2
|
+
require File.dirname(__FILE__) + '/lib/money'
|
3
|
+
|
4
|
+
# Generate all the Rake tasks
|
5
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
6
|
+
$hoe = Hoe.new('money', Money::VERSION) do |p|
|
7
|
+
p.developer('FIXME full name', 'FIXME email')
|
8
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
9
|
+
p.rubyforge_name = p.name
|
10
|
+
p.summary = "This library aids one in handling money and different currencies."
|
11
|
+
p.description = "This library aids one in handling money and different currencies."
|
12
|
+
p.url = "http://github.com/nofxx/money"
|
13
|
+
|
14
|
+
|
15
|
+
p.extra_dev_deps = [
|
16
|
+
['newgem', ">= #{::Newgem::VERSION}"]
|
17
|
+
]
|
18
|
+
|
19
|
+
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
20
|
+
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
21
|
+
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
22
|
+
p.rsync_args = '-av --delete --ignore-errors'
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
26
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
27
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# Money require 'money'
|
2
|
+
# based on github.com/collectiveidea/acts_as_money
|
3
|
+
module ActsAsMoney #:nodoc:
|
4
|
+
def self.included(base) #:nodoc:
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
#
|
10
|
+
# class Product
|
11
|
+
# has_money :value, :tax, :opts => opts
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# @product.value.class #=> "Money"
|
15
|
+
# @product.value_in_cents #=> "1000"
|
16
|
+
# @product.tax_currency #=> "USD"
|
17
|
+
#
|
18
|
+
# Opts:
|
19
|
+
# :cents => "pennys" #=> @product.pennys
|
20
|
+
# :currency => "currency" #=> @product.currency
|
21
|
+
# :allow_nil => true
|
22
|
+
# :with_currency => true
|
23
|
+
#
|
24
|
+
def has_money(*attributes)
|
25
|
+
config = {:with_currency => true, :converter => lambda { |m| m.to_money },
|
26
|
+
:allow_nil => false }.update(attributes.extract_options!)
|
27
|
+
|
28
|
+
attributes.each do |attr|
|
29
|
+
mapping = [[config[:cents] || "#{attr}_in_cents", 'cents']]
|
30
|
+
mapping << [config[:currency] || "#{attr}_currency", 'currency'] if config[:with_currency]
|
31
|
+
|
32
|
+
composed_of attr, :class_name => 'Money',:allow_nil => config[:allow_nil],
|
33
|
+
:mapping => mapping, :converter => config[:converter]
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class Numeric
|
2
|
+
# Converts this numeric to a Money object in the default currency. It
|
3
|
+
# multiplies the numeric value by 100 and treats that as cents.
|
4
|
+
#
|
5
|
+
# 100.to_money => #<Money @cents=10000>
|
6
|
+
# 100.37.to_money => #<Money @cents=10037>
|
7
|
+
def to_money
|
8
|
+
Money.new(self * 100)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class String
|
13
|
+
# Parses the current string and converts it to a Money object.
|
14
|
+
# Excess characters will be discarded.
|
15
|
+
#
|
16
|
+
# '100'.to_money # => #<Money @cents=10000>
|
17
|
+
# '100.37'.to_money # => #<Money @cents=10037>
|
18
|
+
# '100 USD'.to_money # => #<Money @cents=10000, @currency="USD">
|
19
|
+
# 'USD 100'.to_money # => #<Money @cents=10000, @currency="USD">
|
20
|
+
# '$100 USD'.to_money # => #<Money @cents=10000, @currency="USD">
|
21
|
+
def to_money
|
22
|
+
# Get the currency.
|
23
|
+
matches = scan /([A-Z]{2,3})/
|
24
|
+
currency = matches[0] ? matches[0][0] : Money.default_currency
|
25
|
+
|
26
|
+
# Get the cents amount
|
27
|
+
sans_spaces = gsub(/\s+/, '')
|
28
|
+
matches = sans_spaces.scan /(\-?\d+(?:[\.,]\d+)?)/
|
29
|
+
cents = if matches[0]
|
30
|
+
value = matches[0][0].gsub(/,/, '.')
|
31
|
+
value.to_f * 100
|
32
|
+
else
|
33
|
+
0
|
34
|
+
end
|
35
|
+
|
36
|
+
Money.new(cents, currency)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
data/lib/money/errors.rb
ADDED
data/lib/money/money.rb
ADDED
@@ -0,0 +1,268 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'money/variable_exchange_bank'
|
3
|
+
|
4
|
+
# Represents an amount of money in a certain currency.
|
5
|
+
class Money
|
6
|
+
include Comparable
|
7
|
+
|
8
|
+
attr_reader :cents, :currency, :bank
|
9
|
+
|
10
|
+
class << self
|
11
|
+
# Each Money object is associated to a bank object, which is responsible
|
12
|
+
# for currency exchange. This property allows one to specify the default
|
13
|
+
# bank object.
|
14
|
+
#
|
15
|
+
# bank1 = MyBank.new
|
16
|
+
# bank2 = MyOtherBank.new
|
17
|
+
#e if VariableExchangeBank.
|
18
|
+
# It allows one to specify custom exchange rates:
|
19
|
+
#
|
20
|
+
# Money.default_bank.add_rate("USD", "CAD", 1.24515)
|
21
|
+
# Money.default_bank.add_rate("CAD", "USD", 0.803115)
|
22
|
+
# Money.us_dollar(100).exchange_to("CAD") # => MONEY.ca_dollar(124)
|
23
|
+
# Money.ca_dollar(100).exchange_to("USD") # => Money.us_dollar(80)
|
24
|
+
attr_accessor :default_bank
|
25
|
+
|
26
|
+
# the default currency, which is used when <tt>Money.new</tt> is called
|
27
|
+
# without an explicit currency argument. The default value is "USD".
|
28
|
+
attr_accessor :default_currency
|
29
|
+
end
|
30
|
+
|
31
|
+
self.default_bank = VariableExchangeBank.instance
|
32
|
+
self.default_currency = "USD"
|
33
|
+
|
34
|
+
|
35
|
+
# Create a new money object with value 0.
|
36
|
+
def self.empty(currency = default_currency)
|
37
|
+
Money.new(0, currency)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Creates a new Money object of the given value, using the Canadian dollar currency.
|
41
|
+
def self.ca_dollar(cents)
|
42
|
+
Money.new(cents, "CAD")
|
43
|
+
end
|
44
|
+
|
45
|
+
# Creates a new Money object of the given value, using the American dollar currency.
|
46
|
+
def self.us_dollar(cents)
|
47
|
+
Money.new(cents, "USD")
|
48
|
+
end
|
49
|
+
|
50
|
+
# Creates a new Money object of the given value, using the Euro currency.
|
51
|
+
def self.euro(cents)
|
52
|
+
Money.new(cents, "EUR")
|
53
|
+
end
|
54
|
+
|
55
|
+
# Creates a new Money object of the given value, using the Euro currency.
|
56
|
+
def self.real(cents)
|
57
|
+
Money.new(cents, "BRL")
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.add_rate(from_currency, to_currency, rate)
|
61
|
+
Money.default_bank.add_rate(from_currency, to_currency, rate)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Creates a new money object.
|
65
|
+
# Money.new(100)
|
66
|
+
#
|
67
|
+
# Alternativly you can use the convinience methods like
|
68
|
+
# Money.ca_dollar and Money.us_dollar
|
69
|
+
def initialize(cents, currency = Money.default_currency, bank = Money.default_bank)
|
70
|
+
@cents = cents.round # ? cents.round : 0
|
71
|
+
@currency = currency
|
72
|
+
@bank = bank
|
73
|
+
end
|
74
|
+
|
75
|
+
# Do two money objects equal? Only works if both objects are of the same currency
|
76
|
+
def ==(other_money)
|
77
|
+
cents == other_money.cents && bank.same_currency?(currency, other_money.currency)
|
78
|
+
end
|
79
|
+
|
80
|
+
def <=>(other_money)
|
81
|
+
if bank.same_currency?(currency, other_money.currency)
|
82
|
+
cents <=> other_money.cents
|
83
|
+
else
|
84
|
+
cents <=> other_money.exchange_to(currency).cents
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def +(other_money)
|
89
|
+
if currency == other_money.currency
|
90
|
+
Money.new(cents + other_money.cents, other_money.currency)
|
91
|
+
else
|
92
|
+
Money.new(cents + other_money.exchange_to(currency).cents,currency)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def -(other_money)
|
97
|
+
if currency == other_money.currency
|
98
|
+
Money.new(cents - other_money.cents, other_money.currency)
|
99
|
+
else
|
100
|
+
Money.new(cents - other_money.exchange_to(currency).cents, currency)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# get the cents value of the object
|
105
|
+
def cents
|
106
|
+
@cents
|
107
|
+
end
|
108
|
+
|
109
|
+
# multiply money by fixnum
|
110
|
+
def *(fixnum)
|
111
|
+
Money.new(cents * fixnum, currency)
|
112
|
+
end
|
113
|
+
|
114
|
+
# divide money by fixnum
|
115
|
+
# check out split_in_installments method too
|
116
|
+
def /(fixnum)
|
117
|
+
Money.new(cents / fixnum, currency)
|
118
|
+
end
|
119
|
+
|
120
|
+
def %(fixnum)
|
121
|
+
Money.new(cents % fixnum, currency)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Test if the money amount is zero
|
125
|
+
def zero?
|
126
|
+
cents == 0
|
127
|
+
end
|
128
|
+
|
129
|
+
# Calculates compound interest
|
130
|
+
# Returns a money object with the sum of self + it
|
131
|
+
def compound_interest(rate,count=1)
|
132
|
+
|
133
|
+
Money.new(cents * ((1 + rate / 100.0 / 12) ** count - 1))
|
134
|
+
end
|
135
|
+
|
136
|
+
# Calculate self + simple interest
|
137
|
+
def simple_interest(rate,count=1)
|
138
|
+
Money.new(rate/100/12*cents*count)
|
139
|
+
end
|
140
|
+
|
141
|
+
def with_simple_interest(rate,count=1)
|
142
|
+
end
|
143
|
+
|
144
|
+
# Split money in installments
|
145
|
+
# So US$ 10.00 == [ 3.34, 3.33, 3.33 ]
|
146
|
+
def split_in_installments(fixnum,extra=nil,*opts)
|
147
|
+
wallet = Wallet.new(fixnum, Money.new(cents/fixnum,currency))
|
148
|
+
to_add = cents % fixnum
|
149
|
+
to_add.times { |m| wallet[m] += Money.new(1) }
|
150
|
+
wallet
|
151
|
+
end
|
152
|
+
|
153
|
+
# Split money in installments based on payment value
|
154
|
+
def in_installments_of(other_money,first=false)
|
155
|
+
split_in_installments(cents/other_money.cents,first)
|
156
|
+
end
|
157
|
+
|
158
|
+
# Just a helper if you got tax inputs in percentage.
|
159
|
+
# Ie. with_tax(20) => cents * 1.20
|
160
|
+
def with_tax(tax)
|
161
|
+
Money.new(cents + cents / 100 * tax)
|
162
|
+
end
|
163
|
+
|
164
|
+
# Format the price according to several rules
|
165
|
+
# Currently supported are :with_currency, :no_cents, :symbol and :html
|
166
|
+
#
|
167
|
+
# with_currency:
|
168
|
+
#
|
169
|
+
# Money.ca_dollar(0).format => "free"
|
170
|
+
# Money.ca_dollar(100).format => "$1.00"
|
171
|
+
# Money.ca_dollar(100).format(:with_currency => true) => "$1.00 CAD"
|
172
|
+
# Money.us_dollar(85).format(:with_currency => true) => "$0.85 USD"
|
173
|
+
#
|
174
|
+
# no_cents:
|
175
|
+
#
|
176
|
+
# Money.ca_dollar(100).format(:no_cents) => "$1"
|
177
|
+
# Money.ca_dollar(599).format(:no_cents) => "$5"
|
178
|
+
#
|
179
|
+
# Money.ca_dollar(570).format(:no_cents, :with_currency) => "$5 CAD"
|
180
|
+
# Money.ca_dollar(39000).format(:no_cents) => "$390"
|
181
|
+
#
|
182
|
+
# symbol:
|
183
|
+
#
|
184
|
+
# Money.new(100, :currency => "GBP").format(:symbol => "£") => "£1.00"
|
185
|
+
#
|
186
|
+
# html:
|
187
|
+
#
|
188
|
+
# Money.ca_dollar(570).format(:html => true, :with_currency => true) => "$5.70 <span class=\"currency\">CAD</span>"
|
189
|
+
def format(rules = {})
|
190
|
+
return "free" if cents == 0
|
191
|
+
|
192
|
+
rules = rules.flatten
|
193
|
+
|
194
|
+
if rules.include?(:no_cents)
|
195
|
+
formatted = sprintf("$%d", cents.to_f / 100 )
|
196
|
+
else
|
197
|
+
formatted = sprintf("$%.2f", cents.to_f / 100 )
|
198
|
+
end
|
199
|
+
|
200
|
+
if rules[:with_currency]
|
201
|
+
formatted << " "
|
202
|
+
formatted << '<span class="currency">' if rules[:html]
|
203
|
+
formatted << currency
|
204
|
+
formatted << '</span>' if rules[:html]
|
205
|
+
end
|
206
|
+
formatted
|
207
|
+
end
|
208
|
+
|
209
|
+
# Money.ca_dollar(100).to_s => "1.00"
|
210
|
+
def to_s
|
211
|
+
sprintf("%.2f", cents / 100.0)
|
212
|
+
end
|
213
|
+
|
214
|
+
# Money.ca_dollar(100).to_f => "1.0"
|
215
|
+
def to_f
|
216
|
+
cents / 100.0
|
217
|
+
end
|
218
|
+
|
219
|
+
# Recieve the amount of this money object in another currency.
|
220
|
+
def exchange_to(other_currency)
|
221
|
+
Money.new(@bank.exchange(self.cents, currency, other_currency), other_currency)
|
222
|
+
end
|
223
|
+
|
224
|
+
# Recieve a money object with the same amount as the current Money object
|
225
|
+
# in american dollar
|
226
|
+
def as_us_dollar
|
227
|
+
exchange_to("USD")
|
228
|
+
end
|
229
|
+
|
230
|
+
# Recieve a money object with the same amount as the current Money object
|
231
|
+
# in canadian dollar
|
232
|
+
def as_ca_dollar
|
233
|
+
exchange_to("CAD")
|
234
|
+
end
|
235
|
+
|
236
|
+
# Recieve a money object with the same amount as the current Money object
|
237
|
+
# in euro
|
238
|
+
def as_euro
|
239
|
+
exchange_to("EUR")
|
240
|
+
end
|
241
|
+
|
242
|
+
# Recieve a money object with the same amount as the current Money object
|
243
|
+
# in real
|
244
|
+
def as_real
|
245
|
+
exchange_to("BRL")
|
246
|
+
end
|
247
|
+
|
248
|
+
# Conversation to self
|
249
|
+
def to_money
|
250
|
+
self
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
#
|
255
|
+
# Represent a financial array.
|
256
|
+
# Investment/Time/Installments...
|
257
|
+
#
|
258
|
+
class Wallet < Array
|
259
|
+
|
260
|
+
def to_s
|
261
|
+
map &:to_s
|
262
|
+
end
|
263
|
+
|
264
|
+
def sum
|
265
|
+
Money.new(inject(0){ |sum,m| sum + m.cents })
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'money/errors'
|
3
|
+
|
4
|
+
# Class for aiding in exchanging money between different currencies.
|
5
|
+
# By default, the Money class uses an object of this class (accessible through
|
6
|
+
# Money#bank) for performing currency exchanges.
|
7
|
+
#
|
8
|
+
# By default, VariableExchangeBank has no knowledge about conversion rates.
|
9
|
+
# One must manually specify them with +add_rate+, after which one can perform
|
10
|
+
# exchanges with +exchange+. For example:
|
11
|
+
#
|
12
|
+
# bank = Money::VariableExchangeBank.new
|
13
|
+
# bank.add_rate("USD", "CAD", 1.24515)
|
14
|
+
# bank.add_rate("CAD", "USD", 0.803115)
|
15
|
+
#
|
16
|
+
# # Exchange 100 CAD to USD:
|
17
|
+
# bank.exchange(100_00, "CAD", "USD") # => 124
|
18
|
+
# # Exchange 100 USD to CAD:
|
19
|
+
# bank.exchange(100_00, "USD", "CAD") # => 80
|
20
|
+
class Money
|
21
|
+
class VariableExchangeBank
|
22
|
+
# Returns the singleton instance of VariableExchangeBank.
|
23
|
+
#
|
24
|
+
# By default, <tt>Money.default_bank</tt> returns the same object.
|
25
|
+
def self.instance
|
26
|
+
@@singleton
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
@rates = {}
|
31
|
+
@mutex = Mutex.new
|
32
|
+
end
|
33
|
+
|
34
|
+
# Registers a conversion rate. +from+ and +to+ are both currency names.
|
35
|
+
def add_rate(from, to, rate)
|
36
|
+
@mutex.synchronize do
|
37
|
+
@rates["#{from}_TO_#{to}".upcase] = rate
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Gets the rate for exchanging the currency named +from+ to the currency
|
42
|
+
# named +to+. Returns nil if the rate is unknown.
|
43
|
+
def get_rate(from, to)
|
44
|
+
@mutex.synchronize do
|
45
|
+
@rates["#{from}_TO_#{to}".upcase]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Given two currency names, checks whether they're both the same currency.
|
50
|
+
#
|
51
|
+
# bank = VariableExchangeBank.new
|
52
|
+
# bank.same_currency?("usd", "USD") # => true
|
53
|
+
# bank.same_currency?("usd", "EUR") # => false
|
54
|
+
def same_currency?(currency1, currency2)
|
55
|
+
currency1.upcase == currency2.upcase
|
56
|
+
end
|
57
|
+
|
58
|
+
# Exchange the given amount of cents in +from_currency+ to +to_currency+.
|
59
|
+
# Returns the amount of cents in +to_currency+ as an integer, rounded down.
|
60
|
+
#
|
61
|
+
# If the conversion rate is unknown, then Money::UnknownRate will be raised.
|
62
|
+
def exchange(cents, from_currency, to_currency)
|
63
|
+
rate = get_rate(from_currency, to_currency)
|
64
|
+
if !rate
|
65
|
+
raise Money::UnknownRate, "No conversion rate known for '#{from_currency}' -> '#{to_currency}'"
|
66
|
+
end
|
67
|
+
(cents * rate).floor
|
68
|
+
end
|
69
|
+
|
70
|
+
@@singleton = VariableExchangeBank.new
|
71
|
+
end
|
72
|
+
end
|
data/lib/money.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Copyright (c) 2005 Tobias Luetke
|
2
|
+
# Copyright (c) 2008 Phusion
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
|
23
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__))
|
24
|
+
require 'money/money'
|
25
|
+
require 'money/core_extensions'
|
26
|
+
|
27
|
+
class Money
|
28
|
+
VERSION = "2.2.1"
|
29
|
+
end
|
data/money.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{money}
|
5
|
+
s.version = "2.2.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["FIXME full name"]
|
9
|
+
s.date = %q{2009-02-12}
|
10
|
+
s.description = %q{This library aids one in handling money and different currencies.}
|
11
|
+
s.email = ["FIXME email"]
|
12
|
+
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
|
13
|
+
s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "README.rdoc", "Rakefile", "lib/money.rb", "lib/money/acts_as_money.rb", "lib/money/core_extensions.rb", "lib/money/errors.rb", "lib/money/money.rb", "lib/money/variable_exchange_bank.rb", "money.gemspec", "rails/init.rb", "script/console", "script/destroy", "script/generate", "spec/db/acts_as_money.sqlite3", "spec/db/database.yml", "spec/db/schema.rb", "spec/money/acts_as_money_spec.rb", "spec/money/core_extensions_spec.rb", "spec/money/exchange_bank_spec.rb", "spec/money/money_spec.rb", "spec/spec_helper.rb"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/nofxx/money}
|
16
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{money}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{This library aids one in handling money and different currencies.}
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 2
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_development_dependency(%q<newgem>, [">= 1.2.3"])
|
28
|
+
s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
|
29
|
+
else
|
30
|
+
s.add_dependency(%q<newgem>, [">= 1.2.3"])
|
31
|
+
s.add_dependency(%q<hoe>, [">= 1.8.0"])
|
32
|
+
end
|
33
|
+
else
|
34
|
+
s.add_dependency(%q<newgem>, [">= 1.2.3"])
|
35
|
+
s.add_dependency(%q<hoe>, [">= 1.8.0"])
|
36
|
+
end
|
37
|
+
end
|
data/rails/init.rb
ADDED
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/postgis_adapter.rb'}"
|
9
|
+
puts "Loading postgis_adapter gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/spec/db/schema.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
ActiveRecord::Schema.define() do
|
4
|
+
|
5
|
+
create_table :accounts, :force => true do |t|
|
6
|
+
t.integer :value_in_cents, :total_in_cents
|
7
|
+
t.string :value_currency, :total_currency
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table :products, :force => true do |t|
|
11
|
+
t.integer :value_in_cents, :tax_pennys
|
12
|
+
t.string :value_currency
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
require File.dirname(__FILE__) + '/../../rails/init.rb'
|
3
|
+
|
4
|
+
# Uncomment this once to create the db
|
5
|
+
load_schema
|
6
|
+
|
7
|
+
class Account < ActiveRecord::Base
|
8
|
+
has_money :value, :total, :allow_nil => true
|
9
|
+
end
|
10
|
+
|
11
|
+
class Product < ActiveRecord::Base
|
12
|
+
has_money :value
|
13
|
+
has_money :tax, :cents => "pennys", :with_currency => false
|
14
|
+
|
15
|
+
validates_presence_of :value
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "Acts as Money" do
|
19
|
+
|
20
|
+
it "should require money" do
|
21
|
+
@account = Account.create(:value => nil)
|
22
|
+
@account.value_in_cents.should eql(0)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should require money" do
|
26
|
+
@product_fake = Product.create(:value => nil)
|
27
|
+
@product_fake.value_in_cents.should eql(0)
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "Account" do
|
31
|
+
|
32
|
+
before(:each) do
|
33
|
+
@account = Account.create(:value => 10, :total => "20 BRL")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return an instance of Money" do
|
37
|
+
@account.value.should be_instance_of(Money)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should write to the db" do
|
41
|
+
Account.first.value.to_s.should eql("10.00")
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should include nicely" do
|
45
|
+
@account.value.to_s.should eql("10.00")
|
46
|
+
@account.total.to_s.should eql("20.00")
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should map cents" do
|
50
|
+
@account.value_in_cents.to_s.should eql("1000")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should map currency" do
|
54
|
+
@account.value_currency.should eql("USD")
|
55
|
+
@account.total_currency.should eql("BRL")
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "Product" do
|
61
|
+
|
62
|
+
before(:each) do
|
63
|
+
@product = Product.create(:value => 10, :tax => 2)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should map attributes" do
|
67
|
+
@product.pennys.should eql(200)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should map currency on tax" do
|
71
|
+
@product.should_not respond_to(:tax_currency)
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe "Money core extensions" do
|
4
|
+
describe "Numberic#to_money works" do
|
5
|
+
it "should convert integer to money" do
|
6
|
+
money = 1234.to_money
|
7
|
+
money.cents.should == 1234_00
|
8
|
+
money.currency.should == Money.default_currency
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should convert float to money" do
|
12
|
+
money = 100.37.to_money
|
13
|
+
money.cents.should == 100_37
|
14
|
+
money.currency.should == Money.default_currency
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "String#to_money works" do
|
19
|
+
it { "100".to_money.should == Money.new(100_00) }
|
20
|
+
it { "100.37".to_money.should == Money.new(100_37) }
|
21
|
+
it { "100,37".to_money.should == Money.new(100_37) }
|
22
|
+
it { "100 000".to_money.should == Money.new(100_000_00) }
|
23
|
+
|
24
|
+
it { "100 USD".to_money.should == Money.new(100_00, "USD") }
|
25
|
+
it { "-100 USD".to_money.should == Money.new(-100_00, "USD") }
|
26
|
+
it { "100 EUR".to_money.should == Money.new(100_00, "EUR") }
|
27
|
+
it { "100.37 EUR".to_money.should == Money.new(100_37, "EUR") }
|
28
|
+
it { "100,37 EUR".to_money.should == Money.new(100_37, "EUR") }
|
29
|
+
|
30
|
+
it { "USD 100".to_money.should == Money.new(100_00, "USD") }
|
31
|
+
it { "EUR 100".to_money.should == Money.new(100_00, "EUR") }
|
32
|
+
it { "EUR 100.37".to_money.should == Money.new(100_37, "EUR") }
|
33
|
+
it { "CAD -100.37".to_money.should == Money.new(-100_37, "CAD") }
|
34
|
+
it { "EUR 100,37".to_money.should == Money.new(100_37, "EUR") }
|
35
|
+
it { "EUR -100,37".to_money.should == Money.new(-100_37, "EUR") }
|
36
|
+
|
37
|
+
it {"$100 USD".to_money.should == Money.new(100_00, "USD") }
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe Money::VariableExchangeBank do
|
4
|
+
before :each do
|
5
|
+
@bank = Money::VariableExchangeBank.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns the previously specified conversion rate" do
|
9
|
+
@bank.add_rate("USD", "EUR", 0.788332676)
|
10
|
+
@bank.add_rate("EUR", "YEN", 122.631477)
|
11
|
+
@bank.get_rate("USD", "EUR").should == 0.788332676
|
12
|
+
@bank.get_rate("EUR", "YEN").should == 122.631477
|
13
|
+
end
|
14
|
+
|
15
|
+
it "treats currency names case-insensitively" do
|
16
|
+
@bank.add_rate("usd", "eur", 1)
|
17
|
+
@bank.get_rate("USD", "EUR").should == 1
|
18
|
+
@bank.same_currency?("USD", "usd").should be_true
|
19
|
+
@bank.same_currency?("EUR", "usd").should be_false
|
20
|
+
end
|
21
|
+
|
22
|
+
it "returns nil if the conversion rate is unknown" do
|
23
|
+
@bank.get_rate("American Pesos", "EUR").should be_nil
|
24
|
+
end
|
25
|
+
|
26
|
+
it "exchanges money from one currency to another according to the specified conversion rates" do
|
27
|
+
@bank.add_rate("USD", "EUR", 0.5)
|
28
|
+
@bank.add_rate("EUR", "YEN", 10)
|
29
|
+
@bank.exchange(10_00, "USD", "EUR").should == 5_00
|
30
|
+
@bank.exchange(500_00, "EUR", "YEN").should == 5000_00
|
31
|
+
end
|
32
|
+
|
33
|
+
it "rounds the exchanged result down" do
|
34
|
+
@bank.add_rate("USD", "EUR", 0.788332676)
|
35
|
+
@bank.add_rate("EUR", "YEN", 122.631477)
|
36
|
+
@bank.exchange(10_00, "USD", "EUR").should == 788
|
37
|
+
@bank.exchange(500_00, "EUR", "YEN").should == 6131573
|
38
|
+
end
|
39
|
+
|
40
|
+
it "raises Money::UnknownRate upon conversion if the conversion rate is unknown" do
|
41
|
+
block = lambda { @bank.exchange(10, "USD", "EUR") }
|
42
|
+
block.should raise_error(Money::UnknownRate)
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe Money do
|
4
|
+
it "is associated to the singleton instance of VariableExchangeBank by default" do
|
5
|
+
Money.new(0).bank.object_id.should == Money::VariableExchangeBank.instance.object_id
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return the amount of cents passed to the constructor" do
|
9
|
+
Money.new(200_00, "USD").cents.should == 200_00
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should rounds the given cents to an integer" do
|
13
|
+
Money.new(1.00, "USD").cents.should == 1
|
14
|
+
Money.new(1.01, "USD").cents.should == 1
|
15
|
+
Money.new(1.50, "USD").cents.should == 2
|
16
|
+
end
|
17
|
+
|
18
|
+
it "#currency returns the currency passed to the constructor" do
|
19
|
+
Money.new(200_00, "USD").currency.should == "USD"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "#zero? returns whether the amount is 0" do
|
23
|
+
Money.new(0, "USD").should be_zero
|
24
|
+
Money.new(0, "EUR").should be_zero
|
25
|
+
Money.new(1, "USD").should_not be_zero
|
26
|
+
Money.new(10, "YEN").should_not be_zero
|
27
|
+
Money.new(-1, "EUR").should_not be_zero
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should exchange_to exchanges the amount via its exchange bank" do
|
31
|
+
money = Money.new(100_00, "USD")
|
32
|
+
money.bank.should_receive(:exchange).with(100_00, "USD", "EUR").and_return(200_00)
|
33
|
+
money.exchange_to("EUR")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "#exchange_to exchanges the amount properly" do
|
37
|
+
money = Money.new(100_00, "USD")
|
38
|
+
money.bank.should_receive(:exchange).with(100_00, "USD", "EUR").and_return(200_00)
|
39
|
+
money.exchange_to("EUR").should == Money.new(200_00, "EUR")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "#== returns true if and only if their amount and currency are equal" do
|
43
|
+
Money.new(1_00, "USD").should == Money.new(1_00, "USD")
|
44
|
+
end
|
45
|
+
|
46
|
+
it "#* multiplies the money's amount by the multiplier while retaining the currency" do
|
47
|
+
(Money.new(1_00, "USD") * 10).should == Money.new(10_00, "USD")
|
48
|
+
end
|
49
|
+
|
50
|
+
it "#* divides the money's amount by the divisor while retaining the currency" do
|
51
|
+
(Money.new(10_00, "USD") / 10).should == Money.new(1_00, "USD")
|
52
|
+
end
|
53
|
+
|
54
|
+
it "# divides the money ammout in installments add last" do
|
55
|
+
@money = Money.new(10_00).split_in_installments(3)
|
56
|
+
@money[0].cents.should eql(334)
|
57
|
+
@money[1].cents.should eql(333)
|
58
|
+
@money[2].cents.should eql(333)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "# divides the money ammout in installments add first" do
|
62
|
+
@money = Money.new(10_00).split_in_installments(3,true)
|
63
|
+
@money.to_s.should eql(["3.34", "3.33", "3.33"])
|
64
|
+
end
|
65
|
+
|
66
|
+
it "# divides the money ammout in installments base on payment" do
|
67
|
+
money = Money.new(3_00)
|
68
|
+
Money.new(10_00).in_installments_of(money)[0].cents.should eql(334)
|
69
|
+
Money.new(10_00).in_installments_of(money)[1].cents.should eql(333)
|
70
|
+
Money.new(10_00).in_installments_of(money)[2].cents.should eql(333)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "shuld sum array" do
|
74
|
+
Money.new(10_00).split_in_installments(3).sum.cents.should eql(1000)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should calculate tax" do
|
78
|
+
Money.new(100).with_tax(20).cents.should eql(120)
|
79
|
+
Money.new(100).with_tax(-20).cents.should eql(80)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should calculate compound tax" do
|
83
|
+
@ma = Money.new(1000_00)
|
84
|
+
@ma.compound_interest(12.99,12).to_s.should eql("137.92")
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should calculate compound tax" do
|
88
|
+
@ma = Money.new(1000_00)
|
89
|
+
@ma.simple_interest(12.99,12).to_s.should eql("129.90")
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should calculate compound tax" do
|
93
|
+
@ma = Money.new(2500_00)
|
94
|
+
@ma.compound_interest(12.99,3).to_s.should eql("82.07")
|
95
|
+
end
|
96
|
+
|
97
|
+
it "shuld sum array" do
|
98
|
+
@money = Money.new(10_00).with_tax(10)
|
99
|
+
@money.split_in_installments(3).sum.cents.should eql(1100)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should output as float" do
|
103
|
+
Money.new(10_00).to_f.should eql(10.0)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should output as string" do
|
107
|
+
Money.new(10_00).to_s.should eql("10.00")
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should create a new Money object of 0 cents if empty" do
|
111
|
+
Money.empty.should == Money.new(0)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "Money.ca_dollar creates a new Money object of the given value in CAD" do
|
115
|
+
Money.ca_dollar(50).should == Money.new(50, "CAD")
|
116
|
+
end
|
117
|
+
|
118
|
+
it "Money.us_dollar creates a new Money object of the given value in USD" do
|
119
|
+
Money.us_dollar(50).should == Money.new(50, "USD")
|
120
|
+
end
|
121
|
+
|
122
|
+
it "Money.euro creates a new Money object of the given value in EUR" do
|
123
|
+
Money.euro(50).should == Money.new(50, "EUR")
|
124
|
+
end
|
125
|
+
|
126
|
+
it "Money.real creates a new Money object of the given value in BRL" do
|
127
|
+
Money.real(50).should == Money.new(50, "BRL")
|
128
|
+
end
|
129
|
+
|
130
|
+
it "Money.add_rate works" do
|
131
|
+
Money.add_rate("EUR", "USD", 10)
|
132
|
+
Money.new(10_00, "EUR").exchange_to("USD").should == Money.new(100_00, "USD")
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "Actions involving two Money objects" do
|
137
|
+
describe "if the other Money object has the same currency" do
|
138
|
+
it "#<=> compares the two objects' amounts" do
|
139
|
+
(Money.new(1_00, "USD") <=> Money.new(1_00, "USD")).should == 0
|
140
|
+
(Money.new(1_00, "USD") <=> Money.new(99, "USD")).should > 0
|
141
|
+
(Money.new(1_00, "USD") <=> Money.new(2_00, "USD")).should < 0
|
142
|
+
end
|
143
|
+
|
144
|
+
it "#+ adds the other object's amount to the current object's amount while retaining the currency" do
|
145
|
+
(Money.new(10_00, "USD") + Money.new(90, "USD")).should == Money.new(10_90, "USD")
|
146
|
+
end
|
147
|
+
|
148
|
+
it "#- substracts the other object's amount from the current object's amount while retaining the currency" do
|
149
|
+
(Money.new(10_00, "USD") - Money.new(90, "USD")).should == Money.new(9_10, "USD")
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
describe "if the other Money object has a different currency" do
|
154
|
+
it "#<=> compares the two objects' amount after converting the other object's amount to its own currency" do
|
155
|
+
target = Money.new(200_00, "EUR")
|
156
|
+
target.should_receive(:exchange_to).with("USD").and_return(Money.new(300_00, "USD"))
|
157
|
+
(Money.new(100_00, "USD") <=> target).should < 0
|
158
|
+
|
159
|
+
target = Money.new(200_00, "EUR")
|
160
|
+
target.should_receive(:exchange_to).with("USD").and_return(Money.new(100_00, "USD"))
|
161
|
+
(Money.new(100_00, "USD") <=> target).should == 0
|
162
|
+
|
163
|
+
target = Money.new(200_00, "EUR")
|
164
|
+
target.should_receive(:exchange_to).with("USD").and_return(Money.new(99_00, "USD"))
|
165
|
+
(Money.new(100_00, "USD") <=> target).should > 0
|
166
|
+
end
|
167
|
+
|
168
|
+
it "#+ adds the other object's amount, converted to this object's currency, to this object's amount while retaining its currency" do
|
169
|
+
other = Money.new(90, "EUR")
|
170
|
+
other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
|
171
|
+
(Money.new(10_00, "USD") + other).should == Money.new(19_00, "USD")
|
172
|
+
end
|
173
|
+
|
174
|
+
it "#- substracts the other object's amount, converted to this object's currency, from this object's amount while retaining its currency" do
|
175
|
+
other = Money.new(90, "EUR")
|
176
|
+
other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
|
177
|
+
(Money.new(10_00, "USD") - other).should == Money.new(1_00, "USD")
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
require 'active_record'
|
4
|
+
rescue LoadError
|
5
|
+
require 'rubygems'
|
6
|
+
gem 'rspec'
|
7
|
+
require 'spec'
|
8
|
+
end
|
9
|
+
require 'active_record'
|
10
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
11
|
+
require 'money'
|
12
|
+
|
13
|
+
config = YAML.load_file(File.dirname(__FILE__) + '/db/database.yml')
|
14
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
15
|
+
ActiveRecord::Base.establish_connection(config)
|
16
|
+
|
17
|
+
|
18
|
+
def load_schema
|
19
|
+
load(File.dirname(__FILE__) + "/db/schema.rb")
|
20
|
+
end
|
21
|
+
|
22
|
+
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nofxx-money
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- FIXME full name
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-12 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: newgem
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.2.3
|
23
|
+
version:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: hoe
|
26
|
+
version_requirement:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 1.8.0
|
32
|
+
version:
|
33
|
+
description: This library aids one in handling money and different currencies.
|
34
|
+
email:
|
35
|
+
- FIXME email
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- History.txt
|
42
|
+
- Manifest.txt
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- History.txt
|
46
|
+
- MIT-LICENSE
|
47
|
+
- Manifest.txt
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- lib/money.rb
|
51
|
+
- lib/money/acts_as_money.rb
|
52
|
+
- lib/money/core_extensions.rb
|
53
|
+
- lib/money/errors.rb
|
54
|
+
- lib/money/money.rb
|
55
|
+
- lib/money/variable_exchange_bank.rb
|
56
|
+
- money.gemspec
|
57
|
+
- rails/init.rb
|
58
|
+
- script/console
|
59
|
+
- script/destroy
|
60
|
+
- script/generate
|
61
|
+
- spec/db/acts_as_money.sqlite3
|
62
|
+
- spec/db/database.yml
|
63
|
+
- spec/db/schema.rb
|
64
|
+
- spec/money/acts_as_money_spec.rb
|
65
|
+
- spec/money/core_extensions_spec.rb
|
66
|
+
- spec/money/exchange_bank_spec.rb
|
67
|
+
- spec/money/money_spec.rb
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
has_rdoc: true
|
70
|
+
homepage: http://github.com/nofxx/money
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options:
|
73
|
+
- --main
|
74
|
+
- README.rdoc
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: "0"
|
82
|
+
version:
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: "0"
|
88
|
+
version:
|
89
|
+
requirements: []
|
90
|
+
|
91
|
+
rubyforge_project: money
|
92
|
+
rubygems_version: 1.2.0
|
93
|
+
signing_key:
|
94
|
+
specification_version: 2
|
95
|
+
summary: This library aids one in handling money and different currencies.
|
96
|
+
test_files: []
|
97
|
+
|