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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +55 -0
- data/lib/amount/active_record/rspec/matchers.rb +50 -0
- data/lib/amount/active_record/rspec.rb +3 -2
- data/lib/amount/active_record/type.rb +3 -4
- data/lib/amount/allocation.rb +66 -0
- data/lib/amount/arithmetic.rb +91 -0
- data/lib/amount/comparison.rb +78 -0
- data/lib/amount/conversion.rb +41 -0
- data/lib/amount/display.rb +23 -6
- data/lib/amount/parser.rb +1 -1
- data/lib/amount/registry.rb +16 -7
- data/lib/amount/rspec/matchers.rb +74 -0
- data/lib/amount/rspec/support.rb +49 -0
- data/lib/amount/rspec.rb +9 -9
- data/lib/amount/serialization.rb +54 -0
- data/lib/amount/version.rb +1 -1
- data/lib/amount.rb +33 -272
- data/test/test_amount.rb +44 -0
- metadata +9 -4
- data/lib/amount/rspec_matchers.rb +0 -105
- data/lib/amount/rspec_support.rb +0 -47
- data/lib/amount/serializer.rb +0 -37
data/lib/amount/rspec_support.rb
DELETED
|
@@ -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
|
data/lib/amount/serializer.rb
DELETED
|
@@ -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
|