money 6.0.0 → 6.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2d0dc5b8be2189ee718cbe2cf9a981997283de43
4
+ data.tar.gz: fc919e76acc75c0ff838207f939442aea963f793
5
+ SHA512:
6
+ metadata.gz: e394b1d687d093cbb2651ad073a09ee2de5896b5e16681f601141c8b353d2efca668a14af20a025734f9694cfddb4c2e2254278e56c64ebcb4af44cfabbd1f82
7
+ data.tar.gz: 7856ca8d8c8a15828c237d26c7c9c9a55be1d2dd0127cf9817f163116ff1a5a00fe17f8a34978113793588a0450c57d7ad2a8a2fa5a4a65b32f46ea79408c294
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ repo_token: lUJ1iIe2hjtyIFZm413PZ38saqD5UOLXo
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ # Bundler
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ bin/
6
+
7
+ # Rubinius
8
+ *.rbc
9
+
10
+ # YARD
11
+ .yardoc
12
+ yardoc/
13
+ doc/
14
+
15
+ # Project
16
+ .rbenv-version
17
+ .rbx
18
+ .rvmrc
19
+ .ruby-version
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ script: bundle exec rspec spec
7
+ notifications:
8
+ recipients:
9
+ - semmons99@gmail.com
10
+ - andreas@aloop.org
11
+ - weppos@weppos.net
data/AUTHORS ADDED
@@ -0,0 +1,101 @@
1
+ Abhay Kumar
2
+ Alex Speller
3
+ Alexander Ross
4
+ Andreas Loupasakis
5
+ Andrei
6
+ Andrew White
7
+ Andrius Chamentauskas
8
+ Anthony Hristov
9
+ Anthony Sellitti
10
+ banjerluke
11
+ Bartosz Dz
12
+ bbozo
13
+ Benjamin Grössing
14
+ Bill DeRusha
15
+ Bodaniel Jeanes
16
+ Brian Jones
17
+ bUg.
18
+ Casper Thomsen
19
+ Choongmin Lee
20
+ Chris Kampmeier
21
+ Christian Billen
22
+ Dave Kroondyk
23
+ Doug Droper
24
+ Ed Saunders
25
+ Eloy
26
+ Evan Alter
27
+ Exoth
28
+ François Beausoleil
29
+ François Klingler
30
+ Francisco Trindade
31
+ Gee-Hsien Chuang
32
+ Hakan Ensari
33
+ Hongli Lai
34
+ Ilia Lobsanov
35
+ Ingo Wichmann
36
+ Jacob Atzen
37
+ James Cotterill
38
+ James Hunt
39
+ Jean-Louis Giordano
40
+ Jell
41
+ Jeremy McNevin
42
+ Jérémy Lecour
43
+ Jesse Cooke
44
+ John Duff
45
+ John Gakos
46
+ Josh Delsman
47
+ Joshua Clayton
48
+ Josh Hepworth
49
+ Julien Boyer
50
+ Kaleem Ullah
51
+ Kenichi Kamiya
52
+ Kenn Ejima
53
+ kirillian
54
+ Laurynas Butkus
55
+ Marcel Scherf
56
+ Marco Otte-Witte
57
+ Mateusz Wolsza
58
+ Matias Korhonen
59
+ Matt Jankowski
60
+ Matthew McEachen
61
+ Michael Irwin
62
+ Michael Reinsch
63
+ Mikael Wikman
64
+ Mike Połétyn
65
+ Musannif Zahir
66
+ Neil Middleton
67
+ Nihad Abbasov
68
+ Olek Janiszewski
69
+ Orien Madgwick
70
+ Paul McMahon
71
+ Pavel Gabriel
72
+ Pavan Sudarshan
73
+ pconnor
74
+ Pelle Braendgaard
75
+ Phil Cohen
76
+ pivotal-cloudplanner
77
+ Prathan Thananart
78
+ Robert Starsi
79
+ Romain Gérard
80
+ Ryan Bigg
81
+ sankaranarayanan
82
+ Semyon Perepelitsa
83
+ Scott Pierce
84
+ Shane Emmons
85
+ Simone Carletti
86
+ Spencer Rinehart
87
+ Steve Morris
88
+ Thomas E Enebo
89
+ Ticean Bennett
90
+ Tobias Luetke
91
+ Tobias Schmidt
92
+ Tom Lianza
93
+ tommeier
94
+ Бродяной Александр
95
+ Adrian Longley
96
+ Daniel Sherson
97
+ Clarke Brunsdon
98
+ Yuusuke Takizawa
99
+ Tien Nguyen
100
+ Wei Zhu
101
+ Zubin Henner
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 6.0.1.beta1
4
+ - Deprecated methods lists caller on print out for easier updating.
5
+ - Added support for Money::Currency#to_str and Money::Currency#to_sym
6
+ - Added ability to temporally change the rounding methond inside a given block
7
+ - Replaced parsing and core extensions with the monetize gem
8
+
3
9
  ## 6.0.0
4
10
  - Fix BTC subunit
5
11
  - New option :sign_positive to add a + sign to positive numbers
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,17 @@
1
+ # Contribution Guidelines
2
+
3
+ ## Steps
4
+
5
+ 1. Fork [the repo](https://github.com/RubyMoney/money)
6
+ 2. Grab dependencies: `bundle install`
7
+ 3. Make sure everything is working: `bundle exec rake spec`
8
+ 4. Make your changes
9
+ 5. Test your changes
10
+ 5. Create a Pull Request
11
+ 6. Celebrate!!!!!
12
+
13
+ ## Notes
14
+
15
+ When contributing, please make sure to update the CHANGELOG and AUTHORS files
16
+ when you submit your pull request. Upon merging of your first pull request,
17
+ you will be given commit access to the repository.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "coveralls", :require => false
4
+ gem "json", :platform => [:jruby, :mri_18]
5
+
6
+ gemspec
data/README.md CHANGED
@@ -1,16 +1,23 @@
1
1
  # RubyMoney - Money
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/money.png)](http://badge.fury.io/rb/money) [![Build Status](https://travis-ci.org/RubyMoney/money.png?branch=master)](https://travis-ci.org/RubyMoney/money) [![Code Climate](https://codeclimate.com/github/RubyMoney/money.png)](https://codeclimate.com/github/RubyMoney/money)
3
+ [![Gem Version](https://badge.fury.io/rb/money.png)](http://badge.fury.io/rb/money)
4
+ [![Build Status](https://travis-ci.org/RubyMoney/money.png?branch=master)](https://travis-ci.org/RubyMoney/money)
5
+ [![Code Climate](https://codeclimate.com/github/RubyMoney/money.png)](https://codeclimate.com/github/RubyMoney/money)
6
+ [![Coverage Status](https://coveralls.io/repos/RubyMoney/money/badge.png?branch=master)](https://coveralls.io/r/RubyMoney/money?branch=master)
7
+ [![Dependency Status](https://gemnasium.com/RubyMoney/money.png)](https://gemnasium.com/RubyMoney/money)
8
+ [![License](http://img.shields.io/license/MIT.png?color=green)](http://opensource.org/licenses/MIT)
4
9
 
5
10
  :warning: Please read the [migration notes](#migration-notes) before upgrading to a new major version.
6
11
 
12
+ If you miss String parsing, check out the new [monetize gem](https://github.com/RubyMoney/monetize).
13
+
7
14
  ## Contributing
8
15
 
9
16
  See the [Contribution Guidelines](https://github.com/RubyMoney/money/blob/master/CONTRIBUTING.md)
10
17
 
11
18
  ## Introduction
12
19
 
13
- This library aids one in handling money and different currencies.
20
+ A Ruby Library for dealing with money and currency conversion.
14
21
 
15
22
  ### Features
16
23
 
@@ -23,8 +30,6 @@ This library aids one in handling money and different currencies.
23
30
  - Represents currency as `Money::Currency` instances providing an high level of
24
31
  flexibility.
25
32
  - Provides APIs for exchanging money from one currency to another.
26
- - Has the ability to parse a money and currency strings
27
- into the corresponding Money/Currency object.
28
33
 
29
34
  ### Resources
30
35
 
@@ -73,12 +78,6 @@ Money.new(1000, "USD") - Money.new(200, "USD") == Money.new(800, "USD")
73
78
  Money.new(1000, "USD") / 5 == Money.new(200, "USD")
74
79
  Money.new(1000, "USD") * 5 == Money.new(5000, "USD")
75
80
 
76
- # Assumptive Currencies
77
- Money.assume_from_symbol = true
78
- Money.parse("$100") == Money.new(10000, "USD")
79
- Money.parse("€100") == Money.new(10000, "EUR")
80
- Money.parse("£100") == Money.new(10000, "GBP")
81
-
82
81
  # Currency conversions
83
82
  some_code_to_setup_exchange_rates
84
83
  Money.new(1000, "USD").exchange_to("EUR") == Money.new(some_value, "EUR")
data/Rakefile CHANGED
@@ -1,52 +1,11 @@
1
- require 'rubygems'
2
- require 'rake/clean'
1
+ require "rubygems"
2
+ require "bundler/gem_tasks"
3
+ require "rake/clean"
3
4
 
4
5
  CLOBBER.include('doc', '.yardoc')
5
6
 
6
- def gemspec
7
- @gemspec ||= begin
8
- file = File.expand_path("../money.gemspec", __FILE__)
9
- eval(File.read(file), binding, file)
10
- end
11
- end
12
-
13
-
14
- task :default => :spec
15
- task :test => :spec
16
-
17
-
18
- require 'rspec/core/rake_task'
19
-
20
- RSpec::Core::RakeTask.new
21
-
22
-
23
- require 'yard'
7
+ require "yard"
24
8
 
25
9
  YARD::Rake::YardocTask.new do |t|
26
10
  t.options << "--files" << "CHANGELOG.md,LICENSE"
27
11
  end
28
-
29
-
30
- require 'rubygems/package_task'
31
-
32
- Gem::PackageTask.new(gemspec) do |pkg|
33
- pkg.gem_spec = gemspec
34
- end
35
-
36
- task :gem => :gemspec
37
-
38
- desc "Install the gem locally"
39
- task :install => :gem do
40
- sh "gem install pkg/#{gemspec.full_name}.gem"
41
- end
42
-
43
- desc "Validate the gemspec"
44
- task :gemspec do
45
- gemspec.validate
46
- end
47
-
48
-
49
- desc "Open an irb session preloaded with this library"
50
- task :console do
51
- sh "irb -rubygems -I lib -r money.rb"
52
- end
@@ -427,7 +427,7 @@
427
427
  "subunit": "Fen",
428
428
  "subunit_to_unit": 100,
429
429
  "symbol_first": true,
430
- "html_entity": "&#x5713;",
430
+ "html_entity": "&#20803;",
431
431
  "decimal_mark": ".",
432
432
  "thousands_separator": ",",
433
433
  "iso_numeric": "156"
@@ -30,7 +30,7 @@ class Money
30
30
  # bank.exchange_with(c2, "USD") #=> #<Money @fractional=803115>
31
31
  class VariableExchange < Base
32
32
 
33
- attr_reader :rates
33
+ attr_reader :rates, :mutex
34
34
 
35
35
  # Available formats for importing/exporting rates.
36
36
  RATE_FORMATS = [:json, :ruby, :yaml]
@@ -84,27 +84,38 @@ class Money
84
84
  #
85
85
  # # Exchange 100 CAD to USD:
86
86
  # bank.exchange_with(c2, "USD") #=> #<Money @fractional=803115>
87
- def exchange_with(from, to_currency)
88
- return from if same_currency?(from.currency, to_currency)
87
+ def exchange_with(from, to_currency, &block)
88
+ to_currency = Currency.wrap(to_currency)
89
+ if from.currency == to_currency
90
+ from
91
+ else
92
+ if rate = get_rate(from.currency, to_currency)
93
+ fractional = calculate_fractional(from, to_currency)
94
+ Money.new(
95
+ exchange(fractional, rate, &block), to_currency
96
+ )
97
+ else
98
+ raise UnknownRate, "No conversion rate known for '#{from.currency.iso_code}' -> '#{to_currency}'"
99
+ end
100
+ end
101
+ end
102
+
103
+ def calculate_fractional(from, to_currency)
104
+ BigDecimal.new(from.fractional.to_s) / (
105
+ BigDecimal.new(from.currency.subunit_to_unit.to_s) /
106
+ BigDecimal.new(to_currency.subunit_to_unit.to_s)
107
+ )
108
+ end
89
109
 
90
- rate = get_rate(from.currency, to_currency)
91
- unless rate
92
- raise UnknownRate, "No conversion rate known for '#{from.currency.iso_code}' -> '#{to_currency}'"
110
+ def exchange(fractional, rate, &block)
111
+ ex = (fractional * BigDecimal.new(rate.to_s)).to_f
112
+ if block_given?
113
+ yield ex
114
+ elsif @rounding_method
115
+ @rounding_method.call(ex)
116
+ else
117
+ ex.to_s.to_i
93
118
  end
94
- _to_currency_ = Currency.wrap(to_currency)
95
-
96
- fractional = BigDecimal.new(from.fractional.to_s) / (BigDecimal.new(from.currency.subunit_to_unit.to_s) / BigDecimal.new(_to_currency_.subunit_to_unit.to_s))
97
-
98
- ex = fractional * BigDecimal.new(rate.to_s)
99
- ex = ex.to_f
100
- ex = if block_given?
101
- yield ex
102
- elsif @rounding_method
103
- @rounding_method.call(ex)
104
- else
105
- ex.to_s.to_i
106
- end
107
- Money.new(ex, _to_currency_)
108
119
  end
109
120
 
110
121
  # Registers a conversion rate and returns it (uses +#set_rate+).
@@ -129,6 +140,8 @@ class Money
129
140
  # @param [Currency, String, Symbol] from Currency to exchange from.
130
141
  # @param [Currency, String, Symbol] to Currency to exchange to.
131
142
  # @param [Numeric] rate Rate to use when exchanging currencies.
143
+ # @param [Hash] opts Options hash to set special parameters
144
+ # @option opts [Boolean] :without_mutex disables the usage of a mutex
132
145
  #
133
146
  # @return [Numeric]
134
147
  #
@@ -136,8 +149,13 @@ class Money
136
149
  # bank = Money::Bank::VariableExchange.new
137
150
  # bank.set_rate("USD", "CAD", 1.24515)
138
151
  # bank.set_rate("CAD", "USD", 0.803115)
139
- def set_rate(from, to, rate)
140
- @mutex.synchronize { @rates[rate_key_for(from, to)] = rate }
152
+ def set_rate(from, to, rate, opts = {})
153
+ fn = -> { @rates[rate_key_for(from, to)] = rate }
154
+ if opts[:without_mutex]
155
+ fn.call
156
+ else
157
+ @mutex.synchronize { fn.call }
158
+ end
141
159
  end
142
160
 
143
161
  # Retrieve the rate for the given currencies. Uses +Mutex+ to synchronize
@@ -145,6 +163,8 @@ class Money
145
163
  #
146
164
  # @param [Currency, String, Symbol] from Currency to exchange from.
147
165
  # @param [Currency, String, Symbol] to Currency to exchange to.
166
+ # @param [Hash] opts Options hash to set special parameters
167
+ # @option opts [Boolean] :without_mutex disables the usage of a mutex
148
168
  #
149
169
  # @return [Numeric]
150
170
  #
@@ -155,8 +175,13 @@ class Money
155
175
  #
156
176
  # bank.get_rate("USD", "CAD") #=> 1.24515
157
177
  # bank.get_rate("CAD", "USD") #=> 0.803115
158
- def get_rate(from, to)
159
- @mutex.synchronize { @rates[rate_key_for(from, to)] }
178
+ def get_rate(from, to, opts = {})
179
+ fn = -> { @rates[rate_key_for(from, to)] }
180
+ if opts[:without_mutex]
181
+ fn.call
182
+ else
183
+ @mutex.synchronize { fn.call }
184
+ end
160
185
  end
161
186
 
162
187
  # Return the known rates as a string in the format specified. If +file+
@@ -165,6 +190,8 @@ class Money
165
190
  #
166
191
  # @param [Symbol] format Request format for the resulting string.
167
192
  # @param [String] file Optional file location to write the rates to.
193
+ # @param [Hash] opts Options hash to set special parameters
194
+ # @option opts [Boolean] :without_mutex disables the usage of a mutex
168
195
  #
169
196
  # @return [String]
170
197
  #
@@ -177,12 +204,12 @@ class Money
177
204
  #
178
205
  # s = bank.export_rates(:json)
179
206
  # s #=> "{\"USD_TO_CAD\":1.24515,\"CAD_TO_USD\":0.803115}"
180
- def export_rates(format, file=nil)
207
+ def export_rates(format, file = nil, opts = {})
181
208
  raise Money::Bank::UnknownRateFormat unless
182
209
  RATE_FORMATS.include? format
183
210
 
184
211
  s = ""
185
- @mutex.synchronize {
212
+ fn = -> {
186
213
  s = case format
187
214
  when :json
188
215
  JSON.dump(@rates)
@@ -196,6 +223,11 @@ class Money
196
223
  File.open(file, "w") {|f| f.write(s) }
197
224
  end
198
225
  }
226
+ if opts[:without_mutex]
227
+ fn.call
228
+ else
229
+ @mutex.synchronize { fn.call }
230
+ end
199
231
  s
200
232
  end
201
233
 
@@ -204,6 +236,8 @@ class Money
204
236
  #
205
237
  # @param [Symbol] format The format of +s+.
206
238
  # @param [String] s The rates string.
239
+ # @param [Hash] opts Options hash to set special parameters
240
+ # @option opts [Boolean] :without_mutex disables the usage of a mutex
207
241
  #
208
242
  # @return [self]
209
243
  #
@@ -216,11 +250,11 @@ class Money
216
250
  #
217
251
  # bank.get_rate("USD", "CAD") #=> 1.24515
218
252
  # bank.get_rate("CAD", "USD") #=> 0.803115
219
- def import_rates(format, s)
253
+ def import_rates(format, s, opts = {})
220
254
  raise Money::Bank::UnknownRateFormat unless
221
255
  RATE_FORMATS.include? format
222
256
 
223
- @mutex.synchronize {
257
+ fn = -> {
224
258
  @rates = case format
225
259
  when :json
226
260
  JSON.load(s)
@@ -230,6 +264,11 @@ class Money
230
264
  YAML.load(s)
231
265
  end
232
266
  }
267
+ if opts[:without_mutex]
268
+ fn.call
269
+ else
270
+ @mutex.synchronize { fn.call }
271
+ end
233
272
  self
234
273
  end
235
274
 
@@ -0,0 +1,7 @@
1
+ class Numeric
2
+ alias_method :_to_money, :to_money
3
+ def to_money(*args)
4
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use Numeric#to_money. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so"
5
+ _to_money(*args)
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ class String
2
+ alias_method :_to_money, :to_money
3
+ def to_money(*args)
4
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use String#to_money. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so"
5
+ _to_money(*args)
6
+ end
7
+
8
+ alias_method :_to_currency, :to_currency
9
+ def to_currency(*args)
10
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use String#to_currency. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so"
11
+ _to_currency(*args)
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ class Symbol
2
+ alias_method :_to_currency, :to_currency
3
+ def to_currency(*args)
4
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use Symbol#to_currency. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so"
5
+ _to_currency(*args)
6
+ end
7
+ end
@@ -1,80 +1,7 @@
1
- # Open +Numeric+ to add new method.
2
- class Numeric
3
- # Converts this numeric into a +Money+ object in the given +currency+.
4
- #
5
- # @param [Currency, String, Symbol] currency
6
- # The currency to set the resulting +Money+ object to.
7
- #
8
- # @return [Money]
9
- #
10
- # @example
11
- # 100.to_money #=> #<Money @fractional=10000>
12
- # 100.37.to_money #=> #<Money @fractional=10037>
13
- # BigDecimal.new('100').to_money #=> #<Money @fractional=10000>
14
- #
15
- # @see Money.from_numeric
16
- #
17
- def to_money(currency = nil)
18
- Money.deprecate "as of Money 6.1.0 you must `require 'money/core_extension'` to use Numeric#to_money."
19
- Money.from_numeric(self, currency || Money.default_currency)
20
- end
21
- end
1
+ require "money/core_extensions/numeric"
2
+ require "money/core_extensions/string"
3
+ require "money/core_extensions/symbol"
22
4
 
23
- # Open +String+ to add new methods.
24
- class String
25
- # Parses the current string and converts it to a +Money+ object.
26
- # Excess characters will be discarded.
27
- #
28
- # @param [Currency, String, Symbol] currency
29
- # The currency to set the resulting +Money+ object to.
30
- #
31
- # @return [Money]
32
- #
33
- # @example
34
- # '100'.to_money #=> #<Money @fractional=10000>
35
- # '100.37'.to_money #=> #<Money @fractional=10037>
36
- # '100 USD'.to_money #=> #<Money @fractional=10000, @currency=#<Money::Currency id: usd>>
37
- # 'USD 100'.to_money #=> #<Money @fractional=10000, @currency=#<Money::Currency id: usd>>
38
- # '$100 USD'.to_money #=> #<Money @fractional=10000, @currency=#<Money::Currency id: usd>>
39
- # 'hello 2000 world'.to_money #=> #<Money @fractional=200000 @currency=#<Money::Currency id: usd>>
40
- #
41
- # @see Money.from_string
42
- #
43
- def to_money(currency = nil)
44
- Money.deprecate "String#to_money is depreciated and will be remove in 6.1.0. Please write your own parsing methods."
45
- Money.parse(self, currency)
46
- end
5
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use core extensions. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so."
47
6
 
48
- # Converts the current string into a +Currency+ object.
49
- #
50
- # @return [Money::Currency]
51
- #
52
- # @raise [Money::Currency::UnknownCurrency]
53
- # If this String reference an unknown currency.
54
- #
55
- # @example
56
- # "USD".to_currency #=> #<Money::Currency id: usd>
57
- #
58
- def to_currency
59
- Money.deprecate "as of Money 6.1.0 you must `require 'money/core_extension'` to use String#to_currency."
60
- Money::Currency.new(self)
61
- end
62
- end
63
-
64
- # Open +Symbol+ to add new methods.
65
- class Symbol
66
- # Converts the current symbol into a +Currency+ object.
67
- #
68
- # @return [Money::Currency]
69
- #
70
- # @raise [Money::Currency::UnknownCurrency]
71
- # If this String reference an unknown currency.
72
- #
73
- # @example
74
- # :ars.to_currency #=> #<Money::Currency id: ars>
75
- #
76
- def to_currency
77
- Money.deprecate "as of Money 6.1.0 you must `require 'money/core_extension'` to use Symbol#to_currency."
78
- Money::Currency.new(self)
79
- end
80
- end
7
+ Money.silence_core_extensions_deprecations = true