shopify-money 0.11.3 → 0.11.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53109f360b32e2f8fe8c6ade1992ba9a50c4a27d
4
- data.tar.gz: 63b5789d8f13897ddba4ce87e857d8d60092d026
3
+ metadata.gz: 83c2db01e42a2e8495d522c5e2d1e8f1f0503503
4
+ data.tar.gz: 57aee0d1d639f9564792794add56654d4adfd8ef
5
5
  SHA512:
6
- metadata.gz: b602fb6b125c484290a44d6269b14f209c7699d58830bbcf50589b8588b02d4c15d2030dd9ef91890e770fb4e3ba205d2b974a529c120285ff62b673ac0c19e7
7
- data.tar.gz: 3e6c25119ade6fa8a8e4236f5779e64baaaa6e771c1f7be10204334066de7b94a516eeafe98a144cf8467069fed0d3490823a78093fe0318381e09e6c137ba6e
6
+ metadata.gz: e994c9974a9c75e5a3307fb93babbfbff2adaa44606b5eff744586a94ffc5ee4e596894d01fdf03488fc3023090db19fdd5b6e032a33adbe4d9ca3276e795341
7
+ data.tar.gz: 0cb26b9374ff0c6dbf583d5415f2c2e2e1390d490b3ce02ac03f86c27ca31cc23aa69e835e66f1149a026b0475aa64164ab7f0787da7e8235f97e1e7a8681494
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Build Status](https://travis-ci.org/Shopify/money.svg?branch=master)](https://travis-ci.org/Shopify/money) [![codecov](https://codecov.io/gh/Shopify/money/branch/master/graph/badge.svg)](https://codecov.io/gh/Shopify/money)
4
4
 
5
5
 
6
- money_column expects a decimal 8,3 database field.
6
+ money_column expects a DECIMAL(21,3) database field.
7
7
 
8
8
  ### Features
9
9
 
@@ -3,6 +3,7 @@ require_relative 'money/helpers'
3
3
  require_relative 'money/currency'
4
4
  require_relative 'money/null_currency'
5
5
  require_relative 'money/money'
6
+ require_relative 'money/errors'
6
7
  require_relative 'money/deprecations'
7
8
  require_relative 'money/accounting_money_parser'
8
9
  require_relative 'money/core_extensions'
@@ -13,6 +13,6 @@ end
13
13
  # '100.37'.to_money => #<Money @cents=10037>
14
14
  class String
15
15
  def to_money(currency = nil)
16
- empty? ? Money.empty : Money.parse(self, currency)
16
+ Money.parse(self, currency)
17
17
  end
18
18
  end
@@ -0,0 +1,7 @@
1
+ class Money
2
+ class Error < StandardError
3
+ end
4
+
5
+ class IncompatibleCurrencyError < Error
6
+ end
7
+ end
@@ -5,7 +5,7 @@ class Money
5
5
  module Helpers
6
6
  module_function
7
7
 
8
- NUMERIC_REGEX = /\A\s*[\+\-]?\d*(\.\d+)?\s*\z/
8
+ NUMERIC_REGEX = /\A\s*[\+\-]?(\d+|\d*\.\d+)\s*\z/
9
9
  DECIMAL_ZERO = BigDecimal(0).freeze
10
10
  MAX_DECIMAL = 21
11
11
 
@@ -190,7 +190,8 @@ class Money
190
190
 
191
191
  curr = Helpers.value_to_currency(curr)
192
192
  unless currency.compatible?(curr)
193
- Money.deprecate("mathematical operation not permitted for Money objects with different currencies #{curr} and #{currency}.")
193
+ Money.deprecate("mathematical operation not permitted for Money objects with different currencies #{curr} and #{currency}. " \
194
+ "A Money::IncompatibleCurrencyError will raise in the next major release")
194
195
  end
195
196
 
196
197
  self
@@ -63,14 +63,18 @@ class MoneyParser
63
63
 
64
64
  def parse(input, currency = nil, strict: false)
65
65
  currency = Money::Helpers.value_to_currency(currency)
66
- amount = extract_money(input.to_s, currency, strict)
66
+ amount = extract_amount_from_string(input, currency, strict)
67
67
  Money.new(amount, currency)
68
68
  end
69
69
 
70
70
  private
71
71
 
72
- def extract_money(input, currency, strict)
73
- if input.empty?
72
+ def extract_amount_from_string(input, currency, strict)
73
+ unless input.is_a?(String)
74
+ return input
75
+ end
76
+
77
+ if input.strip.empty?
74
78
  return '0'
75
79
  end
76
80
 
@@ -1,3 +1,3 @@
1
1
  class Money
2
- VERSION = "0.11.3"
2
+ VERSION = "0.11.4"
3
3
  end
@@ -14,6 +14,10 @@ RSpec.describe Integer do
14
14
  end
15
15
 
16
16
  it_should_behave_like "an object supporting to_money"
17
+
18
+ it "parses 0 to Money.zero" do
19
+ expect(0.to_money).to eq(Money.zero)
20
+ end
17
21
  end
18
22
 
19
23
  RSpec.describe Float do
@@ -23,6 +27,10 @@ RSpec.describe Float do
23
27
  end
24
28
 
25
29
  it_should_behave_like "an object supporting to_money"
30
+
31
+ it "parses 0.0 to Money.zero" do
32
+ expect(0.0.to_money).to eq(Money.zero)
33
+ end
26
34
  end
27
35
 
28
36
  RSpec.describe String do
@@ -32,6 +40,11 @@ RSpec.describe String do
32
40
  end
33
41
 
34
42
  it_should_behave_like "an object supporting to_money"
43
+
44
+ it "parses an empty string to Money.zero" do
45
+ expect(''.to_money).to eq(Money.zero)
46
+ expect(' '.to_money).to eq(Money.zero)
47
+ end
35
48
  end
36
49
 
37
50
  RSpec.describe BigDecimal do
@@ -41,4 +54,8 @@ RSpec.describe BigDecimal do
41
54
  end
42
55
 
43
56
  it_should_behave_like "an object supporting to_money"
57
+
58
+ it "parses a zero BigDecimal to Money.zero" do
59
+ expect(BigDecimal.new("-0.000").to_money).to eq(Money.zero)
60
+ end
44
61
  end
@@ -291,7 +291,7 @@ RSpec.describe 'MoneyColumn' do
291
291
  expect(record.instance_variable_get(:@money_column_cache)["price"]).to eq(price)
292
292
  end
293
293
 
294
- it 'reload will clear memoizes money values' do
294
+ it 'reload will clear memoized money values' do
295
295
  price = Money.new(1, 'USD')
296
296
  record = MoneyRecord.create(price: price)
297
297
  expect(record.price).to eq(price)
@@ -301,6 +301,23 @@ RSpec.describe 'MoneyColumn' do
301
301
  record.price
302
302
  expect(record.instance_variable_get(:@money_column_cache)["price"]).to eq(price)
303
303
  end
304
+
305
+ it 'reload will clear record cache' do
306
+ price = Money.new(1, 'USD')
307
+ price2 = Money.new(2, 'USD')
308
+
309
+ record = MoneyRecord.create(price: price)
310
+ expect(record.price).to eq(price)
311
+ expect(record[:price]).to eq(price)
312
+
313
+ ActiveRecord::Base.connection.execute("UPDATE money_records SET price=#{price2.value} WHERE id=#{record.id}")
314
+ expect(record[:price]).to_not eq(price2)
315
+ expect(record.price).to_not eq(price2)
316
+
317
+ record.reload
318
+ expect(record[:price]).to eq(price2)
319
+ expect(record.price).to eq(price2)
320
+ end
304
321
  end
305
322
 
306
323
  describe 'ActiveRecord querying' do
@@ -333,6 +333,10 @@ RSpec.describe MoneyParser do
333
333
  it "parses 1.32" do
334
334
  expect(@parser.parse(1.32)).to eq(Money.new(1.32))
335
335
  end
336
+
337
+ it "parses 1.234" do
338
+ expect(@parser.parse(1.234)).to eq(Money.new(1.234))
339
+ end
336
340
  end
337
341
 
338
342
  describe "parsing with thousands separators" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify-money
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.3
4
+ version: 0.11.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-23 00:00:00.000000000 Z
11
+ date: 2018-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -134,6 +134,7 @@ files:
134
134
  - lib/money/currency.rb
135
135
  - lib/money/currency/loader.rb
136
136
  - lib/money/deprecations.rb
137
+ - lib/money/errors.rb
137
138
  - lib/money/helpers.rb
138
139
  - lib/money/money.rb
139
140
  - lib/money/money_parser.rb