amounts 0.0.4 → 0.0.5

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: 8b7620b025be07f1166e655108c2b9cea0232acb60d814cf91b71ecb47ddf1fb
4
- data.tar.gz: 8f8ab43608f4ced54ab5020c7cbcf69b0741c9203e549558b27356766187efb5
3
+ metadata.gz: b83ca37936b92cf689a865d0173dafd36e354f5e058fab07983efa436796b5d6
4
+ data.tar.gz: 3071b5b4bb06f46511a8dc45ec8d0f888b0863d61cfdbda363b313d806fb2f8a
5
5
  SHA512:
6
- metadata.gz: 466350a8fbfff01e84bd044f23ced9d63288eb4147f13d70df2a3db9b37745cafee46e03cb3d1d9b0b805fff8ece3ca13df029c19786204f21e2fe1336b1e564
7
- data.tar.gz: 807ba283121158491de02a78084dae5ce12c6360ac106a7d72a1bde6a7f536ac3009fdf329b9e75972fe3631ade8bdae3dd0f2d1f2d2e74f9a023f28d46f5cb1
6
+ metadata.gz: 398502b610a4e35059307a3f2e895f119ba8456bdfeb22cf95a0976d37b21fcbbedc8f0ff201b90efa6faf5b9cb1f18e9dae2209cc838f8f6cc3929ea73d4d81
7
+ data.tar.gz: d26be5a7c85fd002ddc3435880d25bebae7b7a29007295b064efb2bcfe0eacb0d3959044c499db1621e7e8e1b238b311c546dc24c591e8ccfc02b61e4865e281
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.0.5 - 2026-04-26
4
+
5
+ ### Added
6
+
7
+ - `Amount#ui(decorated: false)` returns the rounded UI value as a plain
8
+ numeric string without the `display_symbol` prefix or suffix. Useful when
9
+ the caller renders the currency label separately (e.g. in a column header
10
+ or chip). Composes with `unit:` and `direction:` — for example,
11
+ `Amount.gold("1").ui(unit: :gram, decorated: false)` returns `"31.10"`.
12
+ Default remains `decorated: true`, so existing callers see no change.
13
+
3
14
  ## 0.0.4 - 2026-04-26
4
15
 
5
16
  ### Changed (breaking)
@@ -18,9 +18,21 @@ class Amount
18
18
 
19
19
  # @param unit [Symbol, nil]
20
20
  # @param direction [Symbol]
21
+ # @param decorated [Boolean] when `false`, omit the display symbol and
22
+ # return just the rounded number. Useful when the caller renders the
23
+ # currency label separately (e.g. in a column header or a chip).
21
24
  # @return [String]
22
- def ui(unit: nil, direction: :floor)
23
- unit ? render_display_unit(unit, direction) : render_default(direction)
25
+ # @example
26
+ # Amount.usdc("1.50").ui # => "$1.50"
27
+ # Amount.usdc("1.50").ui(decorated: false) # => "1.50"
28
+ # Amount.gold("1").ui(unit: :gram) # => "31.10 g"
29
+ # Amount.gold("1").ui(unit: :gram, decorated: false) # => "31.10"
30
+ def ui(unit: nil, direction: :floor, decorated: true)
31
+ if unit
32
+ render_display_unit(unit, direction, decorated:)
33
+ else
34
+ render_default(direction, decorated:)
35
+ end
24
36
  end
25
37
 
26
38
  # @return [String]
@@ -37,19 +49,24 @@ class Amount
37
49
 
38
50
  private
39
51
 
40
- def render_default(direction)
52
+ def render_default(direction, decorated:)
41
53
  rounded = round(@amount.decimal, @entry.ui_decimals, direction)
42
- apply_symbol(format("%.#{@entry.ui_decimals}f", rounded), @entry.display_symbol, @entry.display_position)
54
+ formatted = format("%.#{@entry.ui_decimals}f", rounded)
55
+ return formatted unless decorated
56
+
57
+ apply_symbol(formatted, @entry.display_symbol, @entry.display_position)
43
58
  end
44
59
 
45
- def render_display_unit(unit, direction)
60
+ def render_display_unit(unit, direction, decorated:)
46
61
  spec = fetch_display_unit(unit)
47
62
  scaled = @amount.decimal * Amount.coerce_decimal(spec[:scale])
48
63
  decimals = spec[:ui_decimals] || @entry.ui_decimals
49
64
  rounded = round(scaled, decimals, direction)
65
+ formatted = format("%.#{decimals}f", rounded)
66
+ return formatted unless decorated
50
67
 
51
68
  apply_symbol(
52
- format("%.#{decimals}f", rounded),
69
+ formatted,
53
70
  spec[:symbol] || @entry.display_symbol,
54
71
  spec[:position] || @entry.display_position
55
72
  )
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Amount
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
5
5
  end
data/test/test_amount.rb CHANGED
@@ -169,6 +169,16 @@ class AmountTest < Minitest::Test
169
169
  assert_equal "$1.57", Amount.new("1.561", :USDC).ui(direction: :ceil)
170
170
  end
171
171
 
172
+ def test_ui_decorated_false_omits_the_display_symbol
173
+ assert_equal "1.50", Amount.new("1.5", :USDC).ui(decorated: false)
174
+ assert_equal "1.5000", Amount.new("1.5", :SOL).ui(decorated: false)
175
+ assert_equal "1", Amount.new(1, :LOGS).ui(decorated: false)
176
+ end
177
+
178
+ def test_ui_decorated_false_with_ceil_direction
179
+ assert_equal "1.57", Amount.new("1.561", :USDC).ui(direction: :ceil, decorated: false)
180
+ end
181
+
172
182
  def test_display_units_scale_output
173
183
  gold = Amount.new("1.5", :GOLD)
174
184
 
@@ -177,6 +187,14 @@ class AmountTest < Minitest::Test
177
187
  assert_equal "0.04665 kg", gold.ui(unit: :kg)
178
188
  end
179
189
 
190
+ def test_display_units_with_decorated_false
191
+ gold = Amount.new("1.5", :GOLD)
192
+
193
+ assert_equal "1.5000", gold.ui(decorated: false)
194
+ assert_equal "46.65", gold.ui(unit: :gram, decorated: false)
195
+ assert_equal "0.04665", gold.ui(unit: :kg, decorated: false)
196
+ end
197
+
180
198
  def test_in_unit_returns_raw_bigdecimal
181
199
  gold = Amount.new("1.0", :GOLD)
182
200
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amounts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seb Scholl