shopify-money 0.13.1 → 0.14.0

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
  SHA256:
3
- metadata.gz: cc70e7001416dec80b01e52ccae94bffd95d19c017bb55f96d7e568a56744bfc
4
- data.tar.gz: 28b867f14908d4def0d7c349416deb83937aa3ea9bcb4781bcc9325b309c0602
3
+ metadata.gz: b02e24a104e1494877174e804e31e40e09b65a274def627d659990efc1aee7de
4
+ data.tar.gz: e7938ab991812e180b648b0d300da322f4b231168242e645ea665481f5975d31
5
5
  SHA512:
6
- metadata.gz: 77d1625cc5c76642ea49a4859e963b3f411f2d9dbe16faed7419ee58038812beb32cf350e05f4bb4dec8ca8240fd2c1a14121b3ac34f3e33ad1e0067cbebf9aa
7
- data.tar.gz: 7f40317635b9a4e415535093d3e14716e83e4c5617c9c8a439750dd23c7de9486a283e89d39285101a343941b3c35c5385f0c2cbe82048f097f843630c487045
6
+ metadata.gz: e327e9a9a15df9319a137763fea1e959c029a8f41d7791509798b3fbdce1d105ce2eeabbfff97aee2e798df55dff55de7d2800e57f83f649efb9580c15db5a8a
7
+ data.tar.gz: 41ad146ac22f2803728c66547caddc3a108d997c1a010c35d748b264b8f9c90d9130a5e921ab9ac3fff489fb85d830c75a05b020b87a55a0fb8ac7587b40a8a1
data/.travis.yml CHANGED
@@ -5,6 +5,7 @@ branches:
5
5
  only:
6
6
  - master
7
7
  rvm:
8
+ - 2.6
8
9
  - 2.5
9
10
  - 2.4
10
11
  - 2.3
@@ -37,11 +37,8 @@ class Money
37
37
  # Money.new(10.01, "USD").allocate([0.5, 0.5], :roundrobin_reverse)
38
38
  # #=> [#<Money value:5.00 currency:USD>, #<Money value:5.01 currency:USD>]
39
39
  def allocate(splits, strategy = :roundrobin)
40
- if all_rational?(splits)
41
- allocations = splits.inject(0) { |sum, n| sum + n }
42
- else
43
- allocations = splits.inject(0) { |sum, n| sum + Helpers.value_to_decimal(n) }
44
- end
40
+ splits.map!(&:to_r)
41
+ allocations = splits.inject(0, :+)
45
42
 
46
43
  if (allocations - BigDecimal("1")) > Float::EPSILON
47
44
  raise ArgumentError, "splits add to more than 100%"
@@ -81,14 +78,11 @@ class Money
81
78
  maximums_total = maximums.reduce(Money.new(0, allocation_currency), :+)
82
79
 
83
80
  splits = maximums.map do |max_amount|
84
- next(0) if maximums_total.zero?
81
+ next(Rational(0)) if maximums_total.zero?
85
82
  Money.rational(max_amount, maximums_total)
86
83
  end
87
84
 
88
- total_allocatable = [
89
- value * allocation_currency.subunit_to_unit,
90
- maximums_total.value * allocation_currency.subunit_to_unit
91
- ].min
85
+ total_allocatable = [maximums_total.subunits, self.subunits].min
92
86
 
93
87
  subunits_amounts, left_over = amounts_from_splits(1, splits, total_allocatable)
94
88
 
@@ -116,10 +110,12 @@ class Money
116
110
  end
117
111
 
118
112
  def amounts_from_splits(allocations, splits, subunits_to_split = subunits)
113
+ raise ArgumentError, "All splits values must be of type Rational." unless all_rational?(splits)
114
+
119
115
  left_over = subunits_to_split
120
116
 
121
117
  amounts = splits.collect do |ratio|
122
- frac = (Helpers.value_to_decimal(subunits_to_split * ratio) / allocations).floor
118
+ frac = (subunits_to_split * ratio / allocations.to_r).floor
123
119
  left_over -= frac
124
120
  frac
125
121
  end
data/lib/money/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Money
2
- VERSION = "0.13.1"
2
+ VERSION = "0.14.0"
3
3
  end
data/money.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.add_development_dependency("rspec", "~> 3.2")
19
19
  s.add_development_dependency("database_cleaner", "~> 1.6")
20
20
  s.add_development_dependency("sqlite3", "~> 1.3.6")
21
- s.add_development_dependency("bigdecimal", "~> 1.3.2")
21
+ s.add_development_dependency("bigdecimal", "~> 1.4.4")
22
22
 
23
23
  s.files = `git ls-files`.split($/)
24
24
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
@@ -67,6 +67,12 @@ RSpec.describe "Allocator" do
67
67
  specify "#allocate raise ArgumentError when invalid strategy is provided" do
68
68
  expect { new_allocator(0.03).allocate([0.5, 0.5], :bad_strategy_name) }.to raise_error(ArgumentError, "Invalid strategy. Valid options: :roundrobin, :roundrobin_reverse")
69
69
  end
70
+
71
+ specify "#allocate does not raise ArgumentError when invalid splits types are provided" do
72
+ moneys = new_allocator(0.03).allocate([0.5, 0.5], :roundrobin)
73
+ expect(moneys[0]).to eq(Money.new(0.02))
74
+ expect(moneys[1]).to eq(Money.new(0.01))
75
+ end
70
76
  end
71
77
 
72
78
  describe 'allocate_max_amounts' do
@@ -127,6 +133,12 @@ RSpec.describe "Allocator" do
127
133
  new_allocator(3).allocate_max_amounts([Money.empty, Money.empty, Money.empty]),
128
134
  ).to eq([Money.empty, Money.empty, Money.empty])
129
135
  end
136
+
137
+ specify "#allocate_max_amounts allocates the right amount without rounding error" do
138
+ expect(
139
+ new_allocator(24.2).allocate_max_amounts([Money.new(46), Money.new(46), Money.new(50), Money.new(50),Money.new(50)]),
140
+ ).to eq([Money.new(4.6), Money.new(4.6), Money.new(5), Money.new(5), Money.new(5)])
141
+ end
130
142
  end
131
143
 
132
144
  def new_allocator(amount, currency = nil)
@@ -56,6 +56,6 @@ RSpec.describe BigDecimal do
56
56
  it_should_behave_like "an object supporting to_money"
57
57
 
58
58
  it "parses a zero BigDecimal to Money.zero" do
59
- expect(BigDecimal.new("-0.000").to_money).to eq(Money.zero)
59
+ expect(BigDecimal("-0.000").to_money).to eq(Money.zero)
60
60
  end
61
61
  end
data/spec/helpers_spec.rb CHANGED
@@ -51,8 +51,8 @@ RSpec.describe Money::Helpers do
51
51
 
52
52
  it 'returns the bigdecimal representation of numbers while they are deprecated' do
53
53
  expect(Money).to receive(:deprecate).exactly(2).times
54
- expect(subject.value_to_decimal('1.23abc')).to eq(amount)
55
- expect(subject.value_to_decimal("1.23\n23")).to eq(amount)
54
+ expect(subject.value_to_decimal('0.00123e3')).to eq(amount)
55
+ expect(subject.value_to_decimal("0.123e1")).to eq(amount)
56
56
  end
57
57
 
58
58
  it 'raises on invalid object' do
data/spec/money_spec.rb CHANGED
@@ -744,8 +744,8 @@ RSpec.describe "Money" do
744
744
  end
745
745
 
746
746
  it "accepts Rational number" do
747
- expect(Money.from_amount(Rational("999999999999999999.999")).value).to eql(BigDecimal.new("1000000000000000000", Money::Helpers::MAX_DECIMAL))
748
- expect(Money.from_amount(Rational("999999999999999999.99")).value).to eql(BigDecimal.new("999999999999999999.99", Money::Helpers::MAX_DECIMAL))
747
+ expect(Money.from_amount(Rational("999999999999999999.999")).value).to eql(BigDecimal("1000000000000000000", Money::Helpers::MAX_DECIMAL))
748
+ expect(Money.from_amount(Rational("999999999999999999.99")).value).to eql(BigDecimal("999999999999999999.99", Money::Helpers::MAX_DECIMAL))
749
749
  end
750
750
 
751
751
  it "raises ArgumentError with unsupported argument" 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.13.1
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-29 00:00:00.000000000 Z
11
+ date: 2019-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: 1.3.2
103
+ version: 1.4.4
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: 1.3.2
110
+ version: 1.4.4
111
111
  description: Manage money in Shopify with a class that wont lose pennies during division!
112
112
  email: gems@shopify.com
113
113
  executables: