money 3.1.0.pre3 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +196 -0
- data/README.md +217 -0
- data/lib/money/bank/base.rb +73 -32
- data/lib/money/bank/variable_exchange.rb +128 -27
- data/lib/money/core_extensions.rb +39 -18
- data/lib/money/currency.rb +146 -53
- data/lib/money/defaults.rb +8 -2
- data/lib/money/deprecations.rb +8 -5
- data/lib/money/money.rb +363 -170
- metadata +45 -54
- data/.document +0 -5
- data/.gitignore +0 -21
- data/CHANGELOG.rdoc +0 -68
- data/README.rdoc +0 -212
- data/Rakefile +0 -49
- data/VERSION +0 -1
- data/money.gemspec +0 -77
- data/spec/bank/base_spec.rb +0 -78
- data/spec/bank/variable_exchange_spec.rb +0 -311
- data/spec/core_extensions_spec.rb +0 -111
- data/spec/currency_spec.rb +0 -126
- data/spec/deprecations_spec.rb +0 -16
- data/spec/money_spec.rb +0 -799
- data/spec/spec_helper.rb +0 -8
data/CHANGELOG.md
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
Money 3.1.0
|
2
|
+
===========
|
3
|
+
|
4
|
+
Features
|
5
|
+
--------
|
6
|
+
- Implemented `Money::Bank::Base`.
|
7
|
+
([#issue/14](http://github.com/RubyMoney/money/issues/14))
|
8
|
+
- Added `Money::Bank::Base#exchange_with`.
|
9
|
+
- Deprecated `Money::Bank::Base#exchange`. Deprecation target set to Money
|
10
|
+
3.2.0.
|
11
|
+
- Implented `Money::Bank::VariableExchange`
|
12
|
+
- Deprecated `Money::VariableExchangeBank`. Deprecation target set to Money
|
13
|
+
3.2.0.
|
14
|
+
- Deprecate `Money::SYMBOLS`, `Money::SEPARATORS` and `Money::DELIMITERS`.
|
15
|
+
Deprecation target set to Money 3.2.0.
|
16
|
+
([#issue/16](http://github.com/RubyMoney/money/issues/16))
|
17
|
+
- Implemented `#has` for `Money` and `Money::Currency`.
|
18
|
+
- Refactored test suite to conform to RSpec conventions.
|
19
|
+
- Moved project from [FooBarWidget](http://github.com/FooBarWidget) to
|
20
|
+
[RubyMoney](http://github.com/RubyMoney)
|
21
|
+
- Added Simone Carletti to list of authors.
|
22
|
+
- Moved `@rounding_method` from `Money::Bank::VariableExchange` to
|
23
|
+
`Money::Bank::Base`.
|
24
|
+
([#issue/18](http://github.com/RubyMoney/money/issues/18))
|
25
|
+
- Added `#setup` to `Money::Bank::Base`. Called from `#initialize`.
|
26
|
+
([#issue/19](http://github.com/RubyMoney/money/issues/19))
|
27
|
+
- Added [google_currency](http://github.com/RubyMoney/google_currency) to list
|
28
|
+
of Currency Exchange Implementations.
|
29
|
+
- Added `#export_rates` to `Money::Bank::VariableExchange`.
|
30
|
+
([#issue/21](http://github.com/RubyMoney/money/issues/21))
|
31
|
+
- Added `#import_rates` to `Money::Bank::VariableExchange`.
|
32
|
+
([#issue/21](http://github.com/RubyMoney/money/issues/21))
|
33
|
+
- Removed dependency on Jeweler.
|
34
|
+
- Replaced usage of hanna with yardoc.
|
35
|
+
- Rewrote/reformatted all documentation.
|
36
|
+
|
37
|
+
Bugfixes
|
38
|
+
--------
|
39
|
+
- Fixed incorrect URLs in documentation.
|
40
|
+
([#issue/17](http://github.com/RubyMoney/money/issues/17))
|
41
|
+
- Updated `:subunit_to_unit` for HKD from 10 to 100.
|
42
|
+
([#issue/20](http://github.com/RubyMoney/money/issues/20))
|
43
|
+
- Updated Ghanaian Cedi to use correct ISO Code, GHS.
|
44
|
+
([#issue/22](http://github.com/RubyMoney/money/issues/22))
|
45
|
+
- Make `default` rake task call `spec`.
|
46
|
+
([#issue/23](http://github.com/RubyMoney/money/issues/23))
|
47
|
+
|
48
|
+
Money 3.1.0.pre3
|
49
|
+
================
|
50
|
+
|
51
|
+
Features
|
52
|
+
--------
|
53
|
+
- Added [google_currency](http://github.com/RubyMoney/google_currency) to list
|
54
|
+
of Currency Exchange Implementations.
|
55
|
+
- Added `#export_rates` to `Money::Bank::VariableExchange`.
|
56
|
+
([#issue/21](http://github.com/RubyMoney/money/issues/21))
|
57
|
+
- Added `#import_rates` to `Money::Bank::VariableExchange`.
|
58
|
+
([#issue/21](http://github.com/RubyMoney/money/issues/21))
|
59
|
+
|
60
|
+
Bugfixes
|
61
|
+
--------
|
62
|
+
- Updated `:subunit_to_unit` for HKD from 10 to 100.
|
63
|
+
([#issue/20](http://github.com/RubyMoney/money/issues/20))
|
64
|
+
|
65
|
+
Money 3.1.0.pre2
|
66
|
+
================
|
67
|
+
|
68
|
+
Features
|
69
|
+
--------
|
70
|
+
- Moved `@rounding_method` from `Money::Bank::VariableExchange` to
|
71
|
+
`Money::Bank::Base`.
|
72
|
+
([#issue/18](http://github.com/RubyMoney/money/issues/18))
|
73
|
+
- Added `#setup` to `Money::Bank::Base`. Called from `#initialize`.
|
74
|
+
([#issue/19](http://github.com/RubyMoney/money/issues/19))
|
75
|
+
|
76
|
+
Bugfixes
|
77
|
+
--------
|
78
|
+
- Fixed incorrect URLs in documentation.
|
79
|
+
([#issue/17](http://github.com/RubyMoney/money/issues/17))
|
80
|
+
|
81
|
+
Money 3.1.0.pre1
|
82
|
+
================
|
83
|
+
|
84
|
+
Features
|
85
|
+
--------
|
86
|
+
- Implemented `Money::Bank::Base`.
|
87
|
+
([#issue/14](http://github.com/RubyMoney/money/issues/14))
|
88
|
+
- Added `Money::Bank::Base#exchange_with`.
|
89
|
+
- Deprecated `Money::Bank::Base#exchange`. Deprecation target set to Money
|
90
|
+
3.2.0.
|
91
|
+
- Implented `Money::Bank::VariableExchange`
|
92
|
+
- Deprecated `Money::VariableExchangeBank`. Deprecation target set to Money
|
93
|
+
3.2.0.
|
94
|
+
- Deprecate `Money::SYMBOLS`, `Money::SEPARATORS` and `Money::DELIMITERS`.
|
95
|
+
Deprecation target set to Money 3.2.0.
|
96
|
+
([#issue/16](http://github.com/RubyMoney/money/issues/16))
|
97
|
+
- Implemented `#has` for `Money` and `Money::Currency`.
|
98
|
+
- Refactored test suite to conform to RSpec conventions.
|
99
|
+
- Moved project from [FooBarWidget](http://github.com/FooBarWidget) to
|
100
|
+
[RubyMoney](http://github.com/RubyMoney)
|
101
|
+
- Added Simone Carletti to list of authors.
|
102
|
+
|
103
|
+
Bugfixes
|
104
|
+
--------
|
105
|
+
- Fixed rounding error in `Numeric#to_money`.
|
106
|
+
([#issue/15](http://github.com/RubyMoney/money/issues/15))
|
107
|
+
|
108
|
+
Money 3.0.5
|
109
|
+
===========
|
110
|
+
|
111
|
+
Features
|
112
|
+
--------
|
113
|
+
- Added `Money#abs`.
|
114
|
+
- Added ability to pass a block to `VariableExchangeBank#new` or `#exchange`,
|
115
|
+
specifying a custom truncation method
|
116
|
+
- Added optional `currency` argument to` Numeric#to_money`.
|
117
|
+
([#issue/11](http://github.com/RubyMoney/money/issues/11))
|
118
|
+
- Added optional `currency` argument to `String#to_money`.
|
119
|
+
([#issue/11](http://github.com/RubyMoney/money/issues/11))
|
120
|
+
- Use '¤' as the default currency symbol.
|
121
|
+
([#issue/10](http://github.com/RubyMoney/money/issues/10))
|
122
|
+
|
123
|
+
Bugfixes
|
124
|
+
--------
|
125
|
+
- Updated `Currency#subunit_to_unit` documentation (it's an integer not a
|
126
|
+
string).
|
127
|
+
- Fixed issue when exchanging currencies with different `:subunit_to_unit`
|
128
|
+
values.
|
129
|
+
- `Numeric#to_money` now respects `:subunit_to_unit`.
|
130
|
+
([#issue/12](http://github.com/RubyMoney/money/issues/12))
|
131
|
+
|
132
|
+
Money 3.0.4
|
133
|
+
===========
|
134
|
+
|
135
|
+
Features
|
136
|
+
--------
|
137
|
+
- Use `:subunit_to_unit` in `#to_s`, `#to_f` and `#format`.
|
138
|
+
- Deprecated `Money#SEPARATORS` and `Money#DELIMITERS`.
|
139
|
+
|
140
|
+
Bugfixes
|
141
|
+
--------
|
142
|
+
- Updated `#exchange` to avoid floating point rounding errors.
|
143
|
+
- Added `:separator` and `:delimiter` to `Currency`
|
144
|
+
- Updated the attributes of the Chilean Peso.
|
145
|
+
|
146
|
+
Money 3.0.3
|
147
|
+
===========
|
148
|
+
|
149
|
+
Features
|
150
|
+
--------
|
151
|
+
- Added `#currency_as_string` and `#currency_as_string=` for easier
|
152
|
+
integration with ActiveRecord/Rails
|
153
|
+
|
154
|
+
Money 3.0.2
|
155
|
+
===========
|
156
|
+
|
157
|
+
Features
|
158
|
+
--------
|
159
|
+
- Added `#div`, `#divmod`, `#modulo`, `#%` and `#remainder` to `Money`.
|
160
|
+
|
161
|
+
Money 3.0.1
|
162
|
+
===========
|
163
|
+
|
164
|
+
Features
|
165
|
+
--------
|
166
|
+
- Added `#eql?` to `Money`
|
167
|
+
- Updated `Numeric#to_money` to work with all children of `Numeric` (i.e.
|
168
|
+
`BigDecimal`, `Integer`, `Fixnum`, `Float`, etc)
|
169
|
+
|
170
|
+
Money 3.0.0
|
171
|
+
===========
|
172
|
+
|
173
|
+
Features
|
174
|
+
--------
|
175
|
+
- Version Bump due to compatibility changes with ActiveRecord. See
|
176
|
+
conversation
|
177
|
+
[here](http://github.com/RubyMoney/money/issues#issue/4/comment/224880)
|
178
|
+
for more information.
|
179
|
+
|
180
|
+
Money 2.3.0
|
181
|
+
===========
|
182
|
+
|
183
|
+
Features
|
184
|
+
--------
|
185
|
+
- Currency is now represented by a `Currency` Object instead of a `String`.
|
186
|
+
|
187
|
+
Money 2.2.0
|
188
|
+
===========
|
189
|
+
|
190
|
+
Features
|
191
|
+
--------
|
192
|
+
- Can now divide two Money objects by one another using `#/`.
|
193
|
+
- Can now convert a Money object to a float using `#to_f`.
|
194
|
+
- Users can now specify Separators and Delimiters when using `#format`.
|
195
|
+
- Support for Brazilian Real `Money.new(1_00, :BRL)`
|
196
|
+
- Migrated to Jeweler
|
data/README.md
ADDED
@@ -0,0 +1,217 @@
|
|
1
|
+
# RubyMoney - Money
|
2
|
+
|
3
|
+
## Introduction
|
4
|
+
|
5
|
+
This library aids one in handling money and different currencies.
|
6
|
+
|
7
|
+
### Features
|
8
|
+
|
9
|
+
- Provides a `Money` class which encapsulates all information about an certain
|
10
|
+
amount of money, such as its value and its currency.
|
11
|
+
- Provides a `Money::Currency` class which encapsulates all information about
|
12
|
+
a monetary unit.
|
13
|
+
- Represents monetary values as integers, in cents. This avoids floating point
|
14
|
+
rounding errors.
|
15
|
+
- Represents currency as `Money::Currency` instances providing an high level of
|
16
|
+
flexibility.
|
17
|
+
- Provides APIs for exchanging money from one currency to another.
|
18
|
+
- Has the ability to parse a money and currency strings
|
19
|
+
into the corresponding Money/Currency object.
|
20
|
+
|
21
|
+
### Resources
|
22
|
+
|
23
|
+
- [Website](http://money.rubyforge.org)
|
24
|
+
- [API Documentation](http://money.rubyforge.org)
|
25
|
+
- [Git Repository](http://github.com/RubyMoney/money)
|
26
|
+
|
27
|
+
## Downloading
|
28
|
+
|
29
|
+
Install stable releases with the following command:
|
30
|
+
|
31
|
+
gem install money
|
32
|
+
|
33
|
+
The development version (hosted on Github) can be installed with:
|
34
|
+
|
35
|
+
git clone git://github.com/RubyMoney/money.git
|
36
|
+
cd money
|
37
|
+
rake install
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
require 'money'
|
42
|
+
|
43
|
+
# 10.00 USD
|
44
|
+
money = Money.new(1000, "USD")
|
45
|
+
money.cents #=> 1000
|
46
|
+
money.currency #=> Currency.new("USD")
|
47
|
+
|
48
|
+
# Comparisons
|
49
|
+
Money.new(1000, "USD") == Money.new(1000, "USD") #=> true
|
50
|
+
Money.new(1000, "USD") == Money.new(100, "USD") #=> false
|
51
|
+
Money.new(1000, "USD") == Money.new(1000, "EUR") #=> false
|
52
|
+
Money.new(1000, "USD") != Money.new(1000, "EUR") #=> true
|
53
|
+
|
54
|
+
# Arithmetic
|
55
|
+
Money.new(1000, "USD") + Money.new(500, "USD") == Money.new(1500, "USD")
|
56
|
+
Money.new(1000, "USD") - Money.new(200, "USD") == Money.new(800, "USD")
|
57
|
+
Money.new(1000, "USD") / 5 == Money.new(200, "USD")
|
58
|
+
Money.new(1000, "USD") * 5 == Money.new(5000, "USD")
|
59
|
+
|
60
|
+
# Currency conversions
|
61
|
+
some_code_to_setup_exchange_rates
|
62
|
+
Money.new(1000, "USD").exchange_to("EUR") == Money.new(some_value, "EUR")
|
63
|
+
|
64
|
+
## Currency
|
65
|
+
|
66
|
+
Currencies are consistently represented as instances of `Money::Currency`.
|
67
|
+
The most part of `Money` APIs allows you to supply either a `String` or a
|
68
|
+
`Money::Currency`.
|
69
|
+
|
70
|
+
Money.new(1000, "USD") == Money.new(900, Currency.new("USD"))
|
71
|
+
Money.new(1000, "EUR").currency == Currency.new("EUR")
|
72
|
+
|
73
|
+
A `Money::Currency` instance holds all the information about the currency,
|
74
|
+
including the currency symbol, name and much more.
|
75
|
+
|
76
|
+
currency = Money.new(1000, "USD").currency
|
77
|
+
currency.iso_code #=> "USD"
|
78
|
+
currency.name #=> "United States Dollar"
|
79
|
+
|
80
|
+
To define a new `Money::Currency` simply add a new item to the
|
81
|
+
`Money::Currency::TABLE` hash, where the key is the identifier for the currency
|
82
|
+
object and the value is a hash containing all the currency attributes.
|
83
|
+
|
84
|
+
Money::Currency::TABLE[:USD] = {
|
85
|
+
:priority => 1,
|
86
|
+
:iso_code => "USD",
|
87
|
+
:name => "United States Dollar",
|
88
|
+
:symbol => "$",
|
89
|
+
:subunit => "Cent"
|
90
|
+
:subunit_to_unit => 100,
|
91
|
+
:separator => ".",
|
92
|
+
:delimiter => ","
|
93
|
+
}
|
94
|
+
|
95
|
+
The pre-defined set of attributes includes:
|
96
|
+
|
97
|
+
- `:priority` a numerical value you can use to sort/group the currency list
|
98
|
+
- `:iso_code` the international 3-letter code as defined by the ISO 4217 standard
|
99
|
+
- `:name` the currency name
|
100
|
+
- `:symbol` the currency symbol (UTF-8 encoded)
|
101
|
+
- `:subunit` the name of the fractional monetary unit
|
102
|
+
- `:subunit_to_unit` the proportion between the unit and the subunit
|
103
|
+
- `:separator` character between the whole and fraction amounts
|
104
|
+
- `:delimiter` character between each thousands place
|
105
|
+
|
106
|
+
All attributes are optional. Some attributes, such as `:symbol`, are used by
|
107
|
+
the Money class to print out a representation of the object. Other attributes,
|
108
|
+
such as `:name` or `:priority`, exist to provide a basic API you can take
|
109
|
+
advantage of to build your application.
|
110
|
+
|
111
|
+
### :priority
|
112
|
+
|
113
|
+
The priority attribute is an arbitrary numerical value you can assign to the
|
114
|
+
`Money::Currency` and use in sorting/grouping operation.
|
115
|
+
|
116
|
+
For instance, let's assume your Rails application needs to render a currency
|
117
|
+
selector like the one available
|
118
|
+
[here](http://finance.yahoo.com/currency-converter/). You can create a couple of
|
119
|
+
custom methods to return the list of major currencies and all currencies as
|
120
|
+
follows:
|
121
|
+
|
122
|
+
# Returns an array of currency id where
|
123
|
+
# priority < 10
|
124
|
+
def major_currencies(hash)
|
125
|
+
hash.inject([]) do |array, (id, attributes)|
|
126
|
+
priority = attributes[:priority]
|
127
|
+
if priority && priority < 10
|
128
|
+
array[priority] ||= []
|
129
|
+
array[priority] << id
|
130
|
+
end
|
131
|
+
array
|
132
|
+
end.compact.flatten
|
133
|
+
end
|
134
|
+
|
135
|
+
# Returns an array of all currency id
|
136
|
+
def all_currencies(hash)
|
137
|
+
hash.keys
|
138
|
+
end
|
139
|
+
|
140
|
+
major_currencies(Money::Currency::TABLE)
|
141
|
+
# => [ :usd, :eur, :bgp, :cad ]
|
142
|
+
|
143
|
+
all_currencies(Money::Currency::TABLE)
|
144
|
+
# => [ :aed, :afn, all, ... ]
|
145
|
+
|
146
|
+
### Default Currency
|
147
|
+
|
148
|
+
By default `Money` defaults to USD as its currency. This can be overwritten
|
149
|
+
using:
|
150
|
+
|
151
|
+
Money.default_currency = Money::Currency.new("CAD")
|
152
|
+
|
153
|
+
If you use Rails, then `environment.rb` is a very good place to put this.
|
154
|
+
|
155
|
+
## Currency Exchange
|
156
|
+
|
157
|
+
Exchanging money is performed through an exchange bank object. The default
|
158
|
+
exchange bank object requires one to manually specify the exchange rate. Here's
|
159
|
+
an example of how it works:
|
160
|
+
|
161
|
+
Money.add_rate("USD", "CAD", 1.24515)
|
162
|
+
Money.add_rate("CAD", "USD", 0.803115)
|
163
|
+
|
164
|
+
Money.us_dollar(100).exchange_to("CAD") # => Money.new(124, "CAD")
|
165
|
+
Money.ca_dollar(100).exchange_to("USD") # => Money.new(80, "USD")
|
166
|
+
|
167
|
+
Comparison and arithmetic operations work as expected:
|
168
|
+
|
169
|
+
Money.new(1000, "USD") <=> Money.new(900, "USD") # => 1; 9.00 USD is smaller
|
170
|
+
Money.new(1000, "EUR") + Money.new(10, "EUR") == Money.new(1010, "EUR")
|
171
|
+
|
172
|
+
Money.add_rate("USD", "EUR", 0.5)
|
173
|
+
Money.new(1000, "EUR") + Money.new(1000, "USD") == Money.new(1500, "EUR")
|
174
|
+
|
175
|
+
There is nothing stopping you from creating bank objects which scrapes
|
176
|
+
[XE](http://www.xe.com) for the current rates or just returns `rand(2)`:
|
177
|
+
|
178
|
+
Money.default_bank = ExchangeBankWhichScrapesXeDotCom.new
|
179
|
+
|
180
|
+
### Upcoming Changes
|
181
|
+
|
182
|
+
Starting in v3.1.0 we will be making two changes to the exchange process. First
|
183
|
+
a new `Bank::Base` class will be available. This will allow users to more
|
184
|
+
easily create custom `Bank` classes. Secondly the default `#exchange` method
|
185
|
+
will be depreciated and a new `#exchange_with` method will be used. Currently
|
186
|
+
`#exchange` passes the `cents` attribute. `#exchange_with` will pass the
|
187
|
+
actual `Money` object instead.
|
188
|
+
|
189
|
+
### Implementations
|
190
|
+
|
191
|
+
The following is a list of Money.gem compatible currency exchange rate
|
192
|
+
implementations.
|
193
|
+
|
194
|
+
- [eu_central_bank](http://github.com/RubyMoney/eu_central_bank)
|
195
|
+
- [google_currency](http://github.com/RubyMoney/google_currency)
|
196
|
+
|
197
|
+
## Ruby on Rails
|
198
|
+
|
199
|
+
Use the `compose_of` helper to let Active Record deal with embedding the money
|
200
|
+
object in your models. The following example requires a `cents` and a
|
201
|
+
`currency` field.
|
202
|
+
|
203
|
+
composed_of :price,
|
204
|
+
:class_name => "Money",
|
205
|
+
:mapping => [%w(cents cents), %w(currency currency_as_string)],
|
206
|
+
:constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) }
|
207
|
+
|
208
|
+
For Money 2.2.x and previous versions, simply use the following `composed_of`
|
209
|
+
definition:
|
210
|
+
|
211
|
+
composed_of :price,
|
212
|
+
:class_name => "Money",
|
213
|
+
:mapping => [%w(cents cents), %w(currency currency)],
|
214
|
+
:constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) }
|
215
|
+
|
216
|
+
For further details read the full discussion
|
217
|
+
[here](http://github.com/RubyMoney/money/issues/4#comment_224880).
|
data/lib/money/bank/base.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'thread'
|
2
2
|
|
3
3
|
class Money
|
4
|
+
# Provides classes that aid in the ability of exchange one currency with
|
5
|
+
# another.
|
4
6
|
module Bank
|
5
7
|
|
6
8
|
# The lowest Money::Bank error class.
|
@@ -14,37 +16,59 @@ class Money
|
|
14
16
|
end
|
15
17
|
|
16
18
|
|
19
|
+
# Money::Bank::Base is the basic interface for creating a money exchange
|
20
|
+
# object, also called Bank.
|
17
21
|
#
|
18
|
-
#
|
19
|
-
#
|
22
|
+
# A Bank is responsible for storing exchange rates, take a Money object as
|
23
|
+
# input and returns the corresponding Money object converted into an other
|
24
|
+
# currency.
|
20
25
|
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
26
|
+
# This class exists for aiding in the creating of other classes to exchange
|
27
|
+
# money between different currencies. When creating a subclass you will
|
28
|
+
# need to implement the following methods to exchange money between
|
29
|
+
# currencies:
|
24
30
|
#
|
25
|
-
#
|
26
|
-
# different currencies. When creating a subclass you will need to implement
|
27
|
-
# the following methods to exchange money between currencies:
|
28
|
-
#
|
29
|
-
# * #exchange_with(Money) # => Money
|
31
|
+
# - #exchange_with(Money) #=> Money
|
30
32
|
#
|
31
33
|
# See Money::Bank::VariableExchange for a real example.
|
32
34
|
#
|
33
|
-
# Also, you can extend Money::Bank::VariableExchange
|
34
|
-
#
|
35
|
-
#
|
35
|
+
# Also, you can extend +Money::Bank::VariableExchange+ instead of
|
36
|
+
# +Money::Bank::Base+ if your bank implementation needs to store rates
|
37
|
+
# internally.
|
36
38
|
#
|
39
|
+
# @abstract Subclass and override +#exchange_with+ to implement a custom
|
40
|
+
# +Money::Bank+ class. You can also override +#setup+ instead of
|
41
|
+
# +#initialize+ to setup initial variables, etc.
|
37
42
|
class Base
|
38
43
|
|
39
44
|
# Returns the singleton instance of the Base bank.
|
45
|
+
#
|
46
|
+
# @return [Money::Bank::Base]
|
40
47
|
def self.instance
|
41
48
|
@@singleton ||= self.new
|
42
49
|
end
|
43
50
|
|
51
|
+
# The rounding method to use when exchanging rates.
|
52
|
+
#
|
53
|
+
# @return [Proc]
|
44
54
|
attr_reader :rounding_method
|
45
55
|
|
46
|
-
# Initializes a new Money::Bank::Base object. An optional block can be
|
47
|
-
# passed to dictate the rounding method that
|
56
|
+
# Initializes a new +Money::Bank::Base+ object. An optional block can be
|
57
|
+
# passed to dictate the rounding method that +#exchange_with+ can use.
|
58
|
+
#
|
59
|
+
# @yield [n] Optional block to use when rounding after exchanging one
|
60
|
+
# currency for another.
|
61
|
+
# @yieldparam [Float] n The resulting float after exchanging one currency
|
62
|
+
# for another.
|
63
|
+
# @yieldreturn [Integer]
|
64
|
+
#
|
65
|
+
# @return [Money::Bank::Base]
|
66
|
+
#
|
67
|
+
# @example
|
68
|
+
# Money::Bank::Base.new #=> #<Money::Bank::Base @rounding_method=nil>
|
69
|
+
# Money::Bank::Base.new {|n|
|
70
|
+
# n.floor
|
71
|
+
# } #=> #<Money::Bank::Base @round_method=#<Proc>>
|
48
72
|
def initialize(&block)
|
49
73
|
@rounding_method = block
|
50
74
|
setup
|
@@ -52,14 +76,16 @@ class Money
|
|
52
76
|
|
53
77
|
# Called after initialize. Subclasses can use this method to setup
|
54
78
|
# variables, etc that they normally would in +#initialize+.
|
79
|
+
#
|
80
|
+
# @abstract Subclass and override +#setup+ to implement a custom
|
81
|
+
# +Money::Bank+ class.
|
82
|
+
#
|
83
|
+
# @return [self]
|
55
84
|
def setup
|
56
85
|
end
|
57
86
|
|
58
87
|
# @deprecated +#exchange+ will be removed in v3.2.0, use +#exchange_with+
|
59
|
-
#
|
60
|
-
# Exchanges the given amount of cents in +from_currency+ to +to_currency+.
|
61
|
-
#
|
62
|
-
# Returns the amount of cents in +to_currency+ as an integer, rounded down.
|
88
|
+
# @see Money::Bank::Base#exchange_with
|
63
89
|
def exchange(cents, from_currency, to_currency, &block)
|
64
90
|
Money.deprecate "`Money::Bank::Base#exchange' will be removed in v3.2.0, use #exchange_with instead"
|
65
91
|
exchange_with(Money.new(cents, from_currency), to_currency, &block).cents
|
@@ -68,30 +94,45 @@ class Money
|
|
68
94
|
# Exchanges the given +Money+ object to a new +Money+ object in
|
69
95
|
# +to_currency+.
|
70
96
|
#
|
71
|
-
#
|
72
|
-
#
|
97
|
+
# @abstract Subclass and override +#exchange_with+ to implement a custom
|
98
|
+
# +Money::Bank+ class.
|
99
|
+
#
|
100
|
+
# @raise NotImplementedError
|
101
|
+
#
|
102
|
+
# @param [Money] from The +Money+ object to exchange from.
|
103
|
+
# @param [Money::Currency, String, Symbol] to_currency The currency
|
104
|
+
# string or object to exchange to.
|
105
|
+
# @yield [n] Optional block to use to round the result after making
|
106
|
+
# the exchange.
|
107
|
+
# @yieldparam [Float] n The result after exchanging from one currency to
|
108
|
+
# the other.
|
109
|
+
# @yieldreturn [Integer]
|
73
110
|
#
|
74
|
-
#
|
75
|
-
# Raises <tt>NotImplementedError</tt>.
|
111
|
+
# @return [Money]
|
76
112
|
def exchange_with(from, to_currency, &block)
|
77
113
|
raise NotImplementedError, "#exchange_with must be implemented"
|
78
114
|
end
|
79
115
|
|
80
116
|
|
81
|
-
# Given two currency strings or object,
|
82
|
-
#
|
117
|
+
# Given two currency strings or object, checks whether they're both the
|
118
|
+
# same currency. Return +true+ if the currencies are the same, +false+
|
119
|
+
# otherwise.
|
83
120
|
#
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
121
|
+
# @param [Money::Currency, String, Symbol] currency1 The first currency
|
122
|
+
# to compare.
|
123
|
+
# @param [Money::Currency, String, Symbol] currency2 The second currency
|
124
|
+
# to compare.
|
88
125
|
#
|
89
|
-
#
|
126
|
+
# @return [Boolean]
|
127
|
+
#
|
128
|
+
# @example
|
129
|
+
# same_currency?("usd", "USD") #=> true
|
130
|
+
# same_currency?("usd", "EUR") #=> false
|
131
|
+
# same_currency?("usd", Currency.new("USD") #=> true
|
132
|
+
# same_currency?("usd", "USD") #=> true
|
90
133
|
def same_currency?(currency1, currency2)
|
91
134
|
Currency.wrap(currency1) == Currency.wrap(currency2)
|
92
135
|
end
|
93
|
-
|
94
136
|
end
|
95
|
-
|
96
137
|
end
|
97
138
|
end
|