amounts 0.0.6 → 0.0.7

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: 01ce6cdf1c52884bc594317df4afa1debc6cfd90c1a6cbef0177e0ed8e2efe63
4
- data.tar.gz: f796211c1f43214d5f8dc6f72d63715b4c901d96c9a4bed30e05220743740fee
3
+ metadata.gz: e8169dc3622ec4944cf900418dac855398a8f1ab1dcf59c07a7d839ace60e205
4
+ data.tar.gz: 6e2d1b8adcaae026bbe777be07f615ec7f7f2c8902e90c6b59e25a0210417b4b
5
5
  SHA512:
6
- metadata.gz: ebbf7e4ff014146a29243ae8dbd857a1833e09e6c5f3e4f124da382663ef0941346a4467a30815aa99ea0b1aa2fa9ace12c799dbf8456b63dbdf4b2f6ab6e5c1
7
- data.tar.gz: 7e177014e0ad23a42cee97c6ae29f1d5c858426b211c1f63ebb3f23871c2e5aceb06edab03feee2efe26b4cf075c3d214e967d18882d3c93aff41226157ee6b9
6
+ metadata.gz: 8afe516fe8c082748e669faa6db55f780a59b8b51948780fb5c23615ac417c830227a447393f5c33393249bbe4b10b40012ee8e090109cfeaf6ca366b197c87e
7
+ data.tar.gz: 984d7e60a58d97dd6b3fed6ba40845bcaa66f2e6bc72e3a2bf36e80880886743ae38217f26efd84a048c7b31d2682b11d993620bd97b6c9dd8901f92e5c29c0c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.0.7 - 2026-05-03
4
+
5
+ ### Fixed
6
+
7
+ - `Display#to_s` now formats the decimal value using `ui_decimals`, matching
8
+ `ui(decorated: false)`. Previously `to_s` used raw `BigDecimal#to_s("F")`
9
+ which dropped trailing zeros (`USDC|1.5` instead of `USDC|1.50`), defeating
10
+ the purpose of the compact format carrying the display-ready amount.
11
+ - `Amount#as_json` returns the compact string (`to_s`), preventing a
12
+ `SystemStackError` (stack level too deep) when calling `as_json` or `to_json`
13
+ on an Amount in Rails. The circular reference between `Display` and `Amount`
14
+ caused ActiveSupport's default recursive serialization to overflow.
15
+
3
16
  ## 0.0.6 - 2026-04-28
4
17
 
5
18
  ### Added
@@ -42,7 +42,8 @@ class Amount
42
42
 
43
43
  # @return [String]
44
44
  def to_s
45
- "#{@entry.symbol}|#{@amount.decimal.to_s("F")}"
45
+ rounded = round(@amount.decimal, @entry.ui_decimals, :floor)
46
+ "#{@entry.symbol}|#{format_number(rounded, @entry.ui_decimals, false)}"
46
47
  end
47
48
 
48
49
  # @param unit [Symbol]
@@ -50,5 +50,10 @@ class Amount
50
50
  symbol: @symbol.to_s
51
51
  }
52
52
  end
53
+
54
+ # @return [String]
55
+ def as_json(*)
56
+ to_s
57
+ end
53
58
  end
54
59
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Amount
4
- VERSION = "0.0.6"
4
+ VERSION = "0.0.7"
5
5
  end
@@ -245,14 +245,14 @@ class AmountActiveRecordTest < Minitest::Test
245
245
  holding = ValidatedHolding.new(amount: "USDC|0.00", reserve: "USDC|1.25")
246
246
 
247
247
  refute holding.valid?
248
- assert_includes holding.errors[:amount], "must be greater than USDC|0.0"
248
+ assert_includes holding.errors[:amount], "must be greater than USDC|0.00"
249
249
  end
250
250
 
251
251
  def test_amount_validator_rejects_amount_above_upper_bound
252
252
  holding = ValidatedHolding.new(amount: "USDC|1000.01", reserve: "USDC|1.25")
253
253
 
254
254
  refute holding.valid?
255
- assert_includes holding.errors[:amount], "must be less than or equal to USDC|1000.0"
255
+ assert_includes holding.errors[:amount], "must be less than or equal to USDC|1000.00"
256
256
  end
257
257
 
258
258
  def test_amount_validator_allows_nil_when_allow_nil_is_set
@@ -271,14 +271,14 @@ class AmountActiveRecordTest < Minitest::Test
271
271
  holding = ValidatedHolding.new(amount: "USDC|100.00", reserve: "USDC|1.25", fee: 0)
272
272
 
273
273
  refute holding.valid?
274
- assert_includes holding.errors[:fee], "must be greater than SOL|0.0"
274
+ assert_includes holding.errors[:fee], "must be greater than SOL|0.0000"
275
275
  end
276
276
 
277
277
  def test_amount_validator_rejects_fixed_symbol_amount_at_exclusive_upper_bound
278
278
  holding = ValidatedHolding.new(amount: "USDC|100.00", reserve: "USDC|1.25", fee: 10)
279
279
 
280
280
  refute holding.valid?
281
- assert_includes holding.errors[:fee], "must be less than SOL|10.0"
281
+ assert_includes holding.errors[:fee], "must be less than SOL|10.0000"
282
282
  end
283
283
 
284
284
  def test_amount_validator_rejects_cross_type_threshold_without_rate
data/test/test_amount.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
3
4
  require_relative "test_helper"
4
5
 
5
6
  class AmountTest < Minitest::Test
@@ -51,7 +52,7 @@ class AmountTest < Minitest::Test
51
52
  amount = Amount.parse("USDC|1.5")
52
53
 
53
54
  assert_equal 1_500_000, amount.atomic
54
- assert_equal "USDC|1.5", amount.to_s
55
+ assert_equal "USDC|1.50", amount.to_s
55
56
  end
56
57
 
57
58
  def test_parse_accepts_explicit_v1_prefix
@@ -140,7 +141,7 @@ class AmountTest < Minitest::Test
140
141
 
141
142
  assert_equal "$1.50", amount.ui
142
143
  assert_equal "1.500000", amount.formatted
143
- assert_equal "USDC|1.5", amount.to_s
144
+ assert_equal "USDC|1.50", amount.to_s
144
145
  end
145
146
 
146
147
  def test_frozen_amount_arithmetic_returns_unfrozen_result
@@ -538,6 +539,19 @@ class AmountTest < Minitest::Test
538
539
  assert_match(/missing key/, error.message)
539
540
  end
540
541
 
542
+ def test_as_json_returns_compact_string
543
+ assert_equal "USDC|1.50", Amount.new("1.5", :USDC).as_json
544
+ end
545
+
546
+ def test_to_json_returns_quoted_compact_string
547
+ assert_equal '"USDC|1.50"', Amount.new("1.5", :USDC).to_json
548
+ end
549
+
550
+ def test_as_json_works_when_nested_in_hash
551
+ hash = { amount: Amount.new("1.5", :USDC) }
552
+ assert_equal({ "amount" => "USDC|1.50" }, JSON.parse(hash.to_json))
553
+ end
554
+
541
555
  def test_load_rejects_unknown_serialization_version
542
556
  assert_raises(Amount::InvalidInput) do
543
557
  Amount.load(v: 2, atomic: "1500000", symbol: "USDC")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amounts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seb Scholl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-28 00:00:00.000000000 Z
11
+ date: 2026-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bigdecimal
@@ -39,7 +39,6 @@ files:
39
39
  - ".rubocop.yml"
40
40
  - CHANGELOG.md
41
41
  - Gemfile
42
- - LICENSE.txt
43
42
  - README.md
44
43
  - Rakefile
45
44
  - bin/console
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Seb Scholl
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.