monetize 0.1.3 → 1.0.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.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -0
- data/.travis.yml +9 -0
- data/CHANGELOG.md +5 -0
- data/CONTRIBUTING.md +17 -0
- data/Gemfile +2 -0
- data/README.md +20 -1
- data/lib/monetize/version.rb +1 -1
- data/lib/monetize.rb +46 -9
- data/monetize.gemspec +2 -1
- data/spec/core_extensions_spec.rb +2 -2
- data/spec/monetize_spec.rb +44 -17
- data/spec/spec_helper.rb +2 -0
- metadata +21 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6302730a9554cb777109e4b35eaaee0da9963dbf
|
|
4
|
+
data.tar.gz: ce9f0611782915a2aa0f788e2b9ad6a676c4dbaf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 183e6584506fe0ff495c2d0d32ea6c3d208f16a9be9d01aec21d4d07f7d5c22218e29062aa09e82fa48d08e485ee8030f9b72d8f690ed3e2f3e83c98501362c3
|
|
7
|
+
data.tar.gz: 57b26b992ced6c627aa6c346a6c495e91cbef9d3faef1fd9578790346ec8488cb147c2f85d927a09fbda71575c8a844c9b7179667f89a01e5a5f271b29eb0252
|
data/.coveralls.yml
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
repo_token: pbKErt5rAW1iRYGMIAGGAAHfVRmiBzxfd
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Contribution Guidelines
|
|
2
|
+
|
|
3
|
+
## Steps
|
|
4
|
+
|
|
5
|
+
1. Fork [the repo](https://github.com/RubyMoney/monetize)
|
|
6
|
+
2. Grab dependencies: `bundle install`
|
|
7
|
+
3. Make sure everything is working: `bundle exec rspec 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 when you submit
|
|
16
|
+
your pull request. Upon merging of your first pull request, you will be
|
|
17
|
+
given commit access to the repository.
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Monetize
|
|
2
2
|
|
|
3
|
+
[](http://badge.fury.io/rb/monetize)
|
|
4
|
+
[](https://travis-ci.org/RubyMoney/monetize)
|
|
5
|
+
[](https://codeclimate.com/github/RubyMoney/monetize)
|
|
6
|
+
[](https://coveralls.io/r/RubyMoney/monetize)
|
|
7
|
+
[](https://gemnasium.com/RubyMoney/monetize)
|
|
8
|
+
[](http://opensource.org/licenses/MIT)
|
|
9
|
+
|
|
3
10
|
A library for converting various objects into `Money` objects.
|
|
4
11
|
|
|
5
12
|
## Installation
|
|
@@ -19,7 +26,19 @@ Or install it yourself as:
|
|
|
19
26
|
## Usage
|
|
20
27
|
|
|
21
28
|
```ruby
|
|
22
|
-
|
|
29
|
+
Monetize.parse("$100") == Money.new(100_00, "USD")
|
|
30
|
+
Monetize.parse("€100") == Money.new(100_00, "EUR")
|
|
31
|
+
Monetize.parse("£100") == Money.new(100_00, "GBP")
|
|
32
|
+
|
|
33
|
+
"100".to_money == Money.new(100_00, "USD")
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Optionally, enable the ability to assume the currency from a passed symbol.
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
Monetize.assume_from_symbol = true
|
|
40
|
+
|
|
41
|
+
"$100".to_money == Money.new(100_00, "USD")
|
|
23
42
|
```
|
|
24
43
|
|
|
25
44
|
## Contributing
|
data/lib/monetize/version.rb
CHANGED
data/lib/monetize.rb
CHANGED
|
@@ -1,21 +1,33 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
2
|
|
|
3
3
|
require "money"
|
|
4
|
+
require "monetize/core_extensions"
|
|
4
5
|
require "monetize/version"
|
|
5
6
|
|
|
6
7
|
module Monetize
|
|
8
|
+
|
|
9
|
+
CURRENCY_SYMBOLS = {
|
|
10
|
+
"$" => "USD",
|
|
11
|
+
"€" => "EUR",
|
|
12
|
+
"£" => "GBP",
|
|
13
|
+
"R$" => "BRL",
|
|
14
|
+
"R" => "ZAR"
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
# Class methods
|
|
18
|
+
class << self
|
|
19
|
+
# @attr_accessor [true, false] assume_from_symbol Use this to enable the
|
|
20
|
+
# ability to assume the currency from a passed symbol
|
|
21
|
+
attr_accessor :assume_from_symbol
|
|
22
|
+
end
|
|
23
|
+
|
|
7
24
|
def self.parse(input, currency = Money.default_currency)
|
|
8
25
|
input = input.to_s.strip
|
|
9
26
|
|
|
10
|
-
computed_currency =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
when /^£/ then "GBP"
|
|
15
|
-
end
|
|
16
|
-
else
|
|
17
|
-
input[/[A-Z]{2,3}/]
|
|
18
|
-
end
|
|
27
|
+
computed_currency = compute_currency(input)
|
|
28
|
+
if not assume_from_symbol
|
|
29
|
+
computed_currency = input[/[A-Z]{2,3}/] || currency
|
|
30
|
+
end
|
|
19
31
|
|
|
20
32
|
currency = computed_currency || currency || Money.default_currency
|
|
21
33
|
currency = Money::Currency.wrap(currency)
|
|
@@ -134,4 +146,29 @@ module Monetize
|
|
|
134
146
|
|
|
135
147
|
negative ? cents * -1 : cents
|
|
136
148
|
end
|
|
149
|
+
|
|
150
|
+
private
|
|
151
|
+
|
|
152
|
+
def self.contains_currency_symbol?(amount)
|
|
153
|
+
currency_symbol_regex === amount
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def self.compute_currency(amount)
|
|
157
|
+
if contains_currency_symbol?(amount)
|
|
158
|
+
matches = amount.match(currency_symbol_regex)
|
|
159
|
+
CURRENCY_SYMBOLS[matches[:symbol]]
|
|
160
|
+
else
|
|
161
|
+
amount[/[A-Z]{2,3}/]
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def self.regex_safe_symbols
|
|
166
|
+
CURRENCY_SYMBOLS.keys.map { |key|
|
|
167
|
+
Regexp.escape(key)
|
|
168
|
+
}.join('|')
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def self.currency_symbol_regex
|
|
172
|
+
/\A(?<symbol>#{regex_safe_symbols})/
|
|
173
|
+
end
|
|
137
174
|
end
|
data/monetize.gemspec
CHANGED
|
@@ -18,7 +18,8 @@ Gem::Specification.new do |spec|
|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
19
|
spec.require_paths = ["lib"]
|
|
20
20
|
|
|
21
|
-
spec.
|
|
21
|
+
spec.add_dependency "money", "~> 6.4.0"
|
|
22
|
+
|
|
22
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
|
23
24
|
spec.add_development_dependency "rake"
|
|
24
25
|
spec.add_development_dependency "rspec", "~> 3.0.0.beta1"
|
|
@@ -29,7 +29,7 @@ describe Monetize, "core extensions" do
|
|
|
29
29
|
it "respects :subunit_to_unit currency property" do
|
|
30
30
|
expect(10.to_money('USD')).to eq Money.new(10_00, 'USD')
|
|
31
31
|
expect(10.to_money('TND')).to eq Money.new(10_000, 'TND')
|
|
32
|
-
expect(10.to_money('
|
|
32
|
+
expect(10.to_money('JPY')).to eq Money.new(10, 'JPY')
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
specify "GH-15" do
|
|
@@ -124,7 +124,7 @@ describe Monetize, "core extensions" do
|
|
|
124
124
|
it "respects :subunit_to_unit currency property" do
|
|
125
125
|
expect("1".to_money("USD")).to eq Money.new(1_00, "USD")
|
|
126
126
|
expect("1".to_money("TND")).to eq Money.new(1_000, "TND")
|
|
127
|
-
expect("1".to_money("
|
|
127
|
+
expect("1".to_money("JPY")).to eq Money.new(1, "JPY")
|
|
128
128
|
expect("1.5".to_money("KWD").cents).to eq 1500
|
|
129
129
|
end
|
|
130
130
|
end
|
data/spec/monetize_spec.rb
CHANGED
|
@@ -19,39 +19,66 @@ describe Monetize do
|
|
|
19
19
|
expect(Monetize.parse('EUR 1.111.234.567,89')).to eq Money.new(111123456789, 'EUR')
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
describe 'currency
|
|
22
|
+
describe 'currency detection' do
|
|
23
|
+
|
|
23
24
|
context 'opted in' do
|
|
24
|
-
before do
|
|
25
|
-
|
|
25
|
+
before :all do
|
|
26
|
+
Monetize.assume_from_symbol = true
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
after :all do
|
|
30
|
+
Monetize.assume_from_symbol = false
|
|
26
31
|
end
|
|
27
32
|
|
|
28
|
-
it "parses formatted inputs with
|
|
29
|
-
original_currency = Money.default_currency
|
|
30
|
-
Money.default_currency = "EUR"
|
|
31
|
-
Money.default_currency = original_currency
|
|
33
|
+
it "parses formatted inputs with Euros passed as a symbol" do
|
|
32
34
|
expect(Monetize.parse("€5.95")).to eq Money.new(595, 'EUR')
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "parses formatted inputs with Euros passed as a symbol with surrounding space" do
|
|
33
38
|
expect(Monetize.parse(" €5.95 ")).to eq Money.new(595, 'EUR')
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "parses formatted inputs with British Pounds Sterling passed as a symbol" do
|
|
34
42
|
expect(Monetize.parse("£9.99")).to eq Money.new(999, 'GBP')
|
|
35
43
|
end
|
|
36
44
|
|
|
45
|
+
it "parses formatted inputs with South African Rand passed as a symbol" do
|
|
46
|
+
expect(Monetize.parse("R9.99")).to eq Money.new(999, 'ZAR')
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "parses formatted inputs with Brazilian real passed as a symbol" do
|
|
50
|
+
expect(Monetize.parse("R$R9.99")).to eq Money.new(999, 'BRL')
|
|
51
|
+
end
|
|
52
|
+
|
|
37
53
|
it 'should assume default currency if not a recognised symbol' do
|
|
38
54
|
expect(Monetize.parse("L9.99")).to eq Money.new(999, 'USD')
|
|
39
55
|
end
|
|
40
56
|
end
|
|
57
|
+
|
|
41
58
|
context 'opted out' do
|
|
42
59
|
before do
|
|
43
|
-
|
|
60
|
+
Monetize.assume_from_symbol = false
|
|
44
61
|
end
|
|
45
|
-
|
|
46
|
-
|
|
62
|
+
|
|
63
|
+
it "ignores the Euro symbol" do
|
|
47
64
|
expect(Monetize.parse("€5.95")).to eq Money.new(595, 'USD')
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "ignores the South African Rand symbol" do
|
|
68
|
+
expect(Monetize.parse("R5.95")).to eq Money.new(595, 'USD')
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "ignores the Euro symbol with surrounding spaces" do
|
|
48
72
|
expect(Monetize.parse(" €5.95 ")).to eq Money.new(595, 'USD')
|
|
49
|
-
|
|
73
|
+
end
|
|
50
74
|
|
|
75
|
+
it "ignores the British Pounds Sterling symbol" do
|
|
76
|
+
expect(Monetize.parse("£9.99")).to eq Money.new(999, 'USD')
|
|
51
77
|
end
|
|
52
78
|
end
|
|
79
|
+
|
|
53
80
|
it 'should opt out by default' do
|
|
54
|
-
expect(
|
|
81
|
+
expect(Monetize.assume_from_symbol).to be_falsy
|
|
55
82
|
end
|
|
56
83
|
end
|
|
57
84
|
|
|
@@ -162,7 +189,7 @@ describe Monetize do
|
|
|
162
189
|
it "respects :subunit_to_unit currency property" do
|
|
163
190
|
expect(Monetize.from_string("1", "USD")).to eq Money.new(1_00, "USD")
|
|
164
191
|
expect(Monetize.from_string("1", "TND")).to eq Money.new(1_000, "TND")
|
|
165
|
-
expect(Monetize.from_string("1", "
|
|
192
|
+
expect(Monetize.from_string("1", "JPY")).to eq Money.new(1, "JPY")
|
|
166
193
|
end
|
|
167
194
|
|
|
168
195
|
it "accepts a currency options" do
|
|
@@ -187,7 +214,7 @@ describe Monetize do
|
|
|
187
214
|
it "should respect :subunit_to_unit currency property" do
|
|
188
215
|
expect(Monetize.from_fixnum(1, "USD")).to eq Money.new(1_00, "USD")
|
|
189
216
|
expect(Monetize.from_fixnum(1, "TND")).to eq Money.new(1_000, "TND")
|
|
190
|
-
expect(Monetize.from_fixnum(1, "
|
|
217
|
+
expect(Monetize.from_fixnum(1, "JPY")).to eq Money.new(1, "JPY")
|
|
191
218
|
end
|
|
192
219
|
|
|
193
220
|
it "accepts a currency options" do
|
|
@@ -212,7 +239,7 @@ describe Monetize do
|
|
|
212
239
|
it "respects :subunit_to_unit currency property" do
|
|
213
240
|
expect(Monetize.from_float(1.2, "USD")).to eq Money.new(1_20, "USD")
|
|
214
241
|
expect(Monetize.from_float(1.2, "TND")).to eq Money.new(1_200, "TND")
|
|
215
|
-
expect(Monetize.from_float(1.2, "
|
|
242
|
+
expect(Monetize.from_float(1.2, "JPY")).to eq Money.new(1, "JPY")
|
|
216
243
|
end
|
|
217
244
|
|
|
218
245
|
it "accepts a currency options" do
|
|
@@ -237,7 +264,7 @@ describe Monetize do
|
|
|
237
264
|
it "respects :subunit_to_unit currency property" do
|
|
238
265
|
expect(Monetize.from_bigdecimal(BigDecimal.new("1"), "USD")).to eq Money.new(1_00, "USD")
|
|
239
266
|
expect(Monetize.from_bigdecimal(BigDecimal.new("1"), "TND")).to eq Money.new(1_000, "TND")
|
|
240
|
-
expect(Monetize.from_bigdecimal(BigDecimal.new("1"), "
|
|
267
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new("1"), "JPY")).to eq Money.new(1, "JPY")
|
|
241
268
|
end
|
|
242
269
|
|
|
243
270
|
it "accepts a currency options" do
|
|
@@ -290,7 +317,7 @@ describe Monetize do
|
|
|
290
317
|
it "respects :subunit_to_unit currency property" do
|
|
291
318
|
expect(Monetize.from_numeric(1, "USD")).to eq Money.new(1_00, "USD")
|
|
292
319
|
expect(Monetize.from_numeric(1, "TND")).to eq Money.new(1_000, "TND")
|
|
293
|
-
expect(Monetize.from_numeric(1, "
|
|
320
|
+
expect(Monetize.from_numeric(1, "JPY")).to eq Money.new(1, "JPY")
|
|
294
321
|
end
|
|
295
322
|
|
|
296
323
|
it "accepts a bank option" do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: monetize
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Shane Emmons
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2014-10-30 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: money
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
16
16
|
requirements:
|
|
17
|
-
- - ~>
|
|
17
|
+
- - "~>"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
20
|
-
type: :
|
|
19
|
+
version: 6.4.0
|
|
20
|
+
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
|
-
- - ~>
|
|
24
|
+
- - "~>"
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version:
|
|
26
|
+
version: 6.4.0
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: bundler
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
|
-
- - ~>
|
|
31
|
+
- - "~>"
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
33
|
version: '1.3'
|
|
34
34
|
type: :development
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
|
-
- - ~>
|
|
38
|
+
- - "~>"
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
40
|
version: '1.3'
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: rake
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
44
44
|
requirements:
|
|
45
|
-
- -
|
|
45
|
+
- - ">="
|
|
46
46
|
- !ruby/object:Gem::Version
|
|
47
47
|
version: '0'
|
|
48
48
|
type: :development
|
|
49
49
|
prerelease: false
|
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
51
|
requirements:
|
|
52
|
-
- -
|
|
52
|
+
- - ">="
|
|
53
53
|
- !ruby/object:Gem::Version
|
|
54
54
|
version: '0'
|
|
55
55
|
- !ruby/object:Gem::Dependency
|
|
56
56
|
name: rspec
|
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
|
58
58
|
requirements:
|
|
59
|
-
- - ~>
|
|
59
|
+
- - "~>"
|
|
60
60
|
- !ruby/object:Gem::Version
|
|
61
61
|
version: 3.0.0.beta1
|
|
62
62
|
type: :development
|
|
63
63
|
prerelease: false
|
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
65
|
requirements:
|
|
66
|
-
- - ~>
|
|
66
|
+
- - "~>"
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
68
|
version: 3.0.0.beta1
|
|
69
69
|
description: A library for converting various objects into `Money` objects.
|
|
@@ -73,7 +73,11 @@ executables: []
|
|
|
73
73
|
extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
|
75
75
|
files:
|
|
76
|
-
- .
|
|
76
|
+
- ".coveralls.yml"
|
|
77
|
+
- ".gitignore"
|
|
78
|
+
- ".travis.yml"
|
|
79
|
+
- CHANGELOG.md
|
|
80
|
+
- CONTRIBUTING.md
|
|
77
81
|
- Gemfile
|
|
78
82
|
- LICENSE.txt
|
|
79
83
|
- README.md
|
|
@@ -98,17 +102,17 @@ require_paths:
|
|
|
98
102
|
- lib
|
|
99
103
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
104
|
requirements:
|
|
101
|
-
- -
|
|
105
|
+
- - ">="
|
|
102
106
|
- !ruby/object:Gem::Version
|
|
103
107
|
version: '0'
|
|
104
108
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
109
|
requirements:
|
|
106
|
-
- -
|
|
110
|
+
- - ">="
|
|
107
111
|
- !ruby/object:Gem::Version
|
|
108
112
|
version: '0'
|
|
109
113
|
requirements: []
|
|
110
114
|
rubyforge_project:
|
|
111
|
-
rubygems_version: 2.
|
|
115
|
+
rubygems_version: 2.2.2
|
|
112
116
|
signing_key:
|
|
113
117
|
specification_version: 4
|
|
114
118
|
summary: A library for converting various objects into `Money` objects.
|