shopify-money 4.1.0 → 4.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cadc7891cca0acc6f6128c570b259a5c1bead3a6a87175ebe3def93d9a618afb
4
- data.tar.gz: 3fcc02abc7af2a86aba449ed135361a1024a18975430c8075f4b36d36d70c72a
3
+ metadata.gz: 7657b4f32cc093849fc1e4b9cc35cbe0792ec41750b08df424e5e37f5cc0ef6c
4
+ data.tar.gz: 688ba3c4195453a8dafd5c099d7daf42c5cddd2cb34b39cdc6b6c11bc8289a52
5
5
  SHA512:
6
- metadata.gz: 982ccb02e8ad20c3ad3ba9d217e0a53531d370cf32e7664947d859997ee2bc493614908f722fd1dc1ee0f234e114cb4c7e3552c85401d978afac4d462e35344c
7
- data.tar.gz: 4a5e188fb292ad0ca2afdc2ba3dbcde89507a352d34ac7b7964870fc1584c0f9fd10d1036bd5b78a1971649d107b89f549f3b38a4f9bad823e829ac0c60d29ea
6
+ metadata.gz: 3c304e8b46c7a8be5bf71a635ec31d17f9bcf952e971d99b231814deaeb4959bcbe423c9d80c6a61dbfd1cbfdca7a8f7701ae747c5bbb23c356f1e529cd6da7d
7
+ data.tar.gz: 1192d7dc8f9315ca4b9bfe7420ac5023f18cfe189751b82123153c2c833a0b8b9f631f77d2971bf1a7c3cf4a29ed1f1ed1cc853e23cd9847cf7eea9656d9fa6a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shopify-money (4.1.0)
4
+ shopify-money (4.1.1)
5
5
  bigdecimal (>= 3.0)
6
6
 
7
7
  GEM
@@ -66,9 +66,7 @@ class Money
66
66
 
67
67
  strategy ||= Money::Config.current.default_allocation_strategy
68
68
 
69
- # Float#to_r preserves float imprecision (0.98.to_r != 98/100).
70
- # Rationalize gives the clean fraction (0.98.rationalize == 49/50).
71
- splits.map!(&:rationalize)
69
+ splits.map! { |split| rationalize(split) }
72
70
  allocations = splits.inject(0, :+)
73
71
 
74
72
  if (allocations - ONE) > Float::EPSILON
@@ -185,5 +183,12 @@ class Money
185
183
  def rank_by_nearest(amounts)
186
184
  amounts.each_with_index.sort_by { |amount, _i| 1 - amount[:fractional_subunits] }.map(&:last)
187
185
  end
186
+
187
+ def rationalize(number)
188
+ return number if number.is_a?(Rational)
189
+
190
+ # Float#to_r preserves float imprecision (0.98.to_r != 49/50).
191
+ number.to_s.to_r
192
+ end
188
193
  end
189
194
  end
data/lib/money/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Money
4
- VERSION = "4.1.0"
4
+ VERSION = "4.1.1"
5
5
  end
@@ -301,6 +301,10 @@ gems:
301
301
  version: '0'
302
302
  source:
303
303
  type: stdlib
304
+ - name: prism
305
+ version: 1.9.0
306
+ source:
307
+ type: rubygems
304
308
  - name: pstore
305
309
  version: '0'
306
310
  source:
@@ -27,5 +27,6 @@ class Money
27
27
  def amounts_from_splits: (Numeric allocations, Array[Numeric] splits, ?Integer subunits_to_split) -> [Array[Hash[Symbol, Numeric]], Integer]
28
28
  def all_rational?: (Array[Numeric] splits) -> bool
29
29
  def rank_by_nearest: (Array[Hash[Symbol, Numeric]] amounts) -> Array[Integer]
30
+ def rationalize: (Numeric number) -> Rational
30
31
  end
31
32
  end
@@ -57,6 +57,22 @@ RSpec.describe "Allocator" do
57
57
  expect { new_allocator(1).allocate([rate, 1 - rate]) }.not_to raise_error
58
58
  end
59
59
 
60
+ specify "#allocate handles BigDecimal splits" do
61
+ splits = [BigDecimal("0.25"), BigDecimal("0.75")]
62
+ expect(new_allocator(10).allocate(splits)).to eq([Money.new(2.50), Money.new(7.50)])
63
+ end
64
+
65
+ specify "#allocate handles Float splits" do
66
+ splits = [0.25, 0.75]
67
+ expect(new_allocator(10).allocate(splits)).to eq([Money.new(2.50), Money.new(7.50)])
68
+ end
69
+
70
+ specify "#allocate handles Rational splits" do
71
+ splits = [Rational(1, 4), Rational(3, 4)]
72
+ expect(new_allocator(10).allocate(splits)).to eq([Money.new(2.50), Money.new(7.50)])
73
+ end
74
+
75
+
60
76
  specify "#allocate raise ArgumentError when invalid strategy is provided" do
61
77
  expect { new_allocator(0.03).allocate([0.5, 0.5], :bad_strategy_name) }.to raise_error(ArgumentError, "Invalid strategy. Valid options: :roundrobin, :roundrobin_reverse, :nearest")
62
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify-money
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify Inc
@@ -252,7 +252,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
252
252
  - !ruby/object:Gem::Version
253
253
  version: '0'
254
254
  requirements: []
255
- rubygems_version: 4.0.10
255
+ rubygems_version: 4.0.11
256
256
  specification_version: 4
257
257
  summary: Shopify's money gem
258
258
  test_files: []