shopify-money 0.14.5 → 0.14.6
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/lib/money/money.rb +14 -3
- data/lib/money/version.rb +1 -1
- data/spec/money_spec.rb +27 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1059640a2227392fc00f7292905bd4d4de2a71d075e8ffc1fb4d02944c5c8c2
|
4
|
+
data.tar.gz: 80db4d470ef80c27c09937b7fa146827d4183c519418a8382934e9a1875fd5f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f8cf9b8ac4fafc4a372176ebe0e46d3b29c8fc82caf020f16732eef39d80ee3f35005faea7c9a96253791c9ce2c836776ca91dc31d55713bc577af55407eed2
|
7
|
+
data.tar.gz: d484b008600973688c52d232797e9ebb1efa6e782b33b8fa408853c08d35f96d8fde50c353ea53c9d9350378fd4f0390d579b619bef45b0f28d0fbe2d7e7c8b6
|
data/lib/money/money.rb
CHANGED
@@ -207,11 +207,22 @@ class Money
|
|
207
207
|
end
|
208
208
|
|
209
209
|
def to_s(style = nil)
|
210
|
-
case style
|
210
|
+
units = case style
|
211
211
|
when :legacy_dollars
|
212
|
-
|
212
|
+
2
|
213
213
|
when :amount, nil
|
214
|
-
|
214
|
+
currency.minor_units
|
215
|
+
else
|
216
|
+
raise ArgumentError, "Unexpected style: #{style}"
|
217
|
+
end
|
218
|
+
|
219
|
+
rounded_value = value.round(units)
|
220
|
+
if units == 0
|
221
|
+
sprintf("%d", rounded_value)
|
222
|
+
else
|
223
|
+
sign = rounded_value < 0 ? '-' : ''
|
224
|
+
rounded_value = rounded_value.abs
|
225
|
+
sprintf("%s%d.%0#{units}d", sign, rounded_value.truncate, rounded_value.frac * (10 ** units))
|
215
226
|
end
|
216
227
|
end
|
217
228
|
|
data/lib/money/version.rb
CHANGED
data/spec/money_spec.rb
CHANGED
@@ -80,6 +80,33 @@ RSpec.describe "Money" do
|
|
80
80
|
expect(non_fractional_money.to_s(:amount)).to eq("1")
|
81
81
|
end
|
82
82
|
|
83
|
+
it "to_s correctly displays negative numbers" do
|
84
|
+
expect((-money).to_s).to eq("-1.00")
|
85
|
+
expect((-amount_money).to_s).to eq("-1.23")
|
86
|
+
expect((-non_fractional_money).to_s).to eq("-1")
|
87
|
+
expect((-Money.new("0.05")).to_s).to eq("-0.05")
|
88
|
+
end
|
89
|
+
|
90
|
+
it "to_s rounds when more fractions than currency allows" do
|
91
|
+
expect(Money.new("9.999", "USD").to_s).to eq("10.00")
|
92
|
+
expect(Money.new("9.889", "USD").to_s).to eq("9.89")
|
93
|
+
end
|
94
|
+
|
95
|
+
it "to_s does not round when fractions same as currency allows" do
|
96
|
+
expect(Money.new("1.25", "USD").to_s).to eq("1.25")
|
97
|
+
expect(Money.new("9.99", "USD").to_s).to eq("9.99")
|
98
|
+
expect(Money.new("9.999", "BHD").to_s).to eq("9.999")
|
99
|
+
end
|
100
|
+
|
101
|
+
it "to_s does not round if amount is larger than float allows" do
|
102
|
+
expect(Money.new("99999999999999.99", "USD").to_s).to eq("99999999999999.99")
|
103
|
+
expect(Money.new("999999999999999999.99", "USD").to_s).to eq("999999999999999999.99")
|
104
|
+
end
|
105
|
+
|
106
|
+
it "to_s raises ArgumentError on unsupported style" do
|
107
|
+
expect{ money.to_s(:some_weird_style) }.to raise_error(ArgumentError)
|
108
|
+
end
|
109
|
+
|
83
110
|
it "as_json as a float with 2 decimal places" do
|
84
111
|
expect(money.as_json).to eq("1.00")
|
85
112
|
end
|
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.14.
|
4
|
+
version: 0.14.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shopify Inc
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|