monetize 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a3936d5c9346eab97303ce52913b65bf5d68978b
4
- data.tar.gz: 35fa705a15d608df8909ee74acf1eb3db1b7581e
3
+ metadata.gz: 5993b4139001864566f5938e83eb0dbd3e6346c1
4
+ data.tar.gz: 70a6f8947b3c53c688f3fd99c9c6dee3b469a13a
5
5
  SHA512:
6
- metadata.gz: cbed507b666182b576e1e377d2364e94d8af046171977584f53e0097f668856c2d83fb2fda486eb1baac09c201d3430be0f99ad2aadf372b48465ec6c71ca5b5
7
- data.tar.gz: f55af07eb6857557de29d62d41941b7331b233c82979f2614d4386a309f1d00b265c78f2d3a8a8a26c3950001fcd183147ecdfed69431d45445babad467d2fbe
6
+ metadata.gz: aeaf837897719780e6034acd08afb139d33a96946905fd85f67a1fcf6a9577c35c236c89162da48f7dcc52b7c05a15b7775b11054022f9f93c7385fc8b95653f
7
+ data.tar.gz: 93040c8ac2c3e29625dacd7ab46494738e02c255bf193f52a0e5d159c2dd25f971596977d685e991eebb3928355709e8e12d63f3802c44b7c9374346d7cd3c76
data/.travis.yml CHANGED
@@ -2,7 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
- - 2.1.0
5
+ - 2.1.2
6
6
  script: bundle exec rspec spec
7
7
  notifications:
8
8
  recipients:
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## master (next release)
4
+ - Added correct parsing of Brazilian Real $R symbol
5
+ - Add testing task for Brazilian Real parsing
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/lib/monetize.rb CHANGED
@@ -6,6 +6,14 @@ require "monetize/version"
6
6
 
7
7
  module Monetize
8
8
 
9
+ CURRENCY_SYMBOLS = {
10
+ "$" => "USD",
11
+ "€" => "EUR",
12
+ "£" => "GBP",
13
+ "R$" => "BRL",
14
+ "R" => "ZAR"
15
+ }
16
+
9
17
  # Class methods
10
18
  class << self
11
19
  # @attr_accessor [true, false] assume_from_symbol Use this to enable the
@@ -16,15 +24,10 @@ module Monetize
16
24
  def self.parse(input, currency = Money.default_currency)
17
25
  input = input.to_s.strip
18
26
 
19
- computed_currency = if assume_from_symbol && input =~ /^(\$|€|£)/
20
- case input
21
- when /^\$/ then "USD"
22
- when /^€/ then "EUR"
23
- when /^£/ then "GBP"
24
- end
25
- else
26
- input[/[A-Z]{2,3}/]
27
- end
27
+ computed_currency = compute_currency(input)
28
+ if not assume_from_symbol
29
+ computed_currency = input[/[A-Z]{2,3}/] || currency
30
+ end
28
31
 
29
32
  currency = computed_currency || currency || Money.default_currency
30
33
  currency = Money::Currency.wrap(currency)
@@ -143,4 +146,29 @@ module Monetize
143
146
 
144
147
  negative ? cents * -1 : cents
145
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
146
174
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Monetize
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/monetize.gemspec CHANGED
@@ -18,7 +18,7 @@ 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.add_dependency "money", "~> 6.1.0.beta1"
21
+ spec.add_dependency "money", "~> 6.2.0"
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
@@ -19,41 +19,64 @@ 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 assumption' do
22
+ describe 'currency detection' do
23
+
23
24
  context 'opted in' do
24
- before do
25
+ before :all do
25
26
  Monetize.assume_from_symbol = true
26
27
  end
27
28
 
28
- after do
29
+ after :all do
29
30
  Monetize.assume_from_symbol = false
30
31
  end
31
32
 
32
- it "parses formatted inputs with the currency passed as a symbol" do
33
- original_currency = Money.default_currency
34
- Money.default_currency = "EUR"
35
- Money.default_currency = original_currency
33
+ it "parses formatted inputs with Euros passed as a symbol" do
36
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
37
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
38
42
  expect(Monetize.parse("£9.99")).to eq Money.new(999, 'GBP')
39
43
  end
40
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
+
41
53
  it 'should assume default currency if not a recognised symbol' do
42
54
  expect(Monetize.parse("L9.99")).to eq Money.new(999, 'USD')
43
55
  end
44
56
  end
57
+
45
58
  context 'opted out' do
46
59
  before do
47
60
  Monetize.assume_from_symbol = false
48
61
  end
49
- it "parses formatted inputs with the currency passed as a symbol but ignores the symbol" do
50
- expect(Monetize.parse("$5.95")).to eq Money.new(595, 'USD')
62
+
63
+ it "ignores the Euro symbol" do
51
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
52
72
  expect(Monetize.parse(" €5.95 ")).to eq Money.new(595, 'USD')
53
- expect(Monetize.parse("£9.99")).to eq Money.new(999, 'USD')
73
+ end
54
74
 
75
+ it "ignores the British Pounds Sterling symbol" do
76
+ expect(Monetize.parse("£9.99")).to eq Money.new(999, 'USD')
55
77
  end
56
78
  end
79
+
57
80
  it 'should opt out by default' do
58
81
  expect(Monetize.assume_from_symbol).to be_falsy
59
82
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monetize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.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: 2014-04-12 00:00:00.000000000 Z
11
+ date: 2014-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: money
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 6.1.0.beta1
19
+ version: 6.2.0
20
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: 6.1.0.beta1
26
+ version: 6.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -76,6 +76,8 @@ files:
76
76
  - ".coveralls.yml"
77
77
  - ".gitignore"
78
78
  - ".travis.yml"
79
+ - CHANGELOG.md
80
+ - CONTRIBUTING.md
79
81
  - Gemfile
80
82
  - LICENSE.txt
81
83
  - README.md