amounts 0.0.3 → 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.
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Amount
4
- # Shared coercion helpers for opt-in RSpec integrations.
5
- module RSpecSupport
6
- module_function
7
-
8
- def coerce_amount_arguments(arguments)
9
- case arguments.length
10
- when 1
11
- coerce_amount(arguments.first)
12
- when 2
13
- Amount.new(arguments.last, arguments.first)
14
- else
15
- raise ArgumentError, "expected an Amount, a parse string, or a symbol/value pair"
16
- end
17
- end
18
-
19
- def coerce_amount(value)
20
- case value
21
- when Amount then value
22
- when String then Amount.parse(value)
23
- when Hash then Amount.load(value)
24
- else
25
- raise ArgumentError, "cannot coerce #{value.inspect} into an Amount"
26
- end
27
- end
28
-
29
- def coerce_delta(expected_amount, within)
30
- case within
31
- when Amount
32
- within
33
- when Integer, Float, BigDecimal, Rational, String
34
- Amount.new(within, expected_amount.symbol)
35
- else
36
- raise ArgumentError, "cannot coerce #{within.inspect} into an amount delta"
37
- end
38
- end
39
-
40
- def normalize_amount_sums(sum_hash)
41
- sum_hash.to_h do |symbol, atomic|
42
- amount = Amount.new(atomic, symbol, from: :atomic)
43
- [amount.symbol, amount]
44
- end
45
- end
46
- end
47
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Amount
4
- # Converts amounts to and from the versioned hash payload.
5
- class Serializer
6
- VERSION = 1
7
-
8
- def self.dump(amount)
9
- {
10
- v: VERSION,
11
- atomic: amount.atomic.to_s,
12
- symbol: amount.symbol.to_s
13
- }
14
- end
15
-
16
- def self.load(payload)
17
- version = payload[:v] || payload["v"]
18
- validate_version!(version)
19
-
20
- Amount.new(
21
- payload.fetch(:atomic) { payload.fetch("atomic") },
22
- payload.fetch(:symbol) { payload.fetch("symbol") },
23
- from: :atomic
24
- )
25
- rescue KeyError => e
26
- raise InvalidInput, "amount payload missing key: #{e.key}"
27
- end
28
-
29
- def self.validate_version!(version)
30
- return if version.nil? || version == VERSION
31
-
32
- raise InvalidInput, "unsupported amount serialization version: #{version}"
33
- end
34
-
35
- private_class_method :validate_version!
36
- end
37
- end