Module: Amount::RSpecSupport

Defined in:
lib/amount/rspec_support.rb

Overview

Shared coercion helpers for opt-in RSpec integrations.

Class Method Summary collapse

Class Method Details

.coerce_amount(value) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/amount/rspec_support.rb', line 19

def coerce_amount(value)
  case value
  when Amount then value
  when String then Amount.parse(value)
  when Hash then Amount.load(value)
  else
    raise ArgumentError, "cannot coerce #{value.inspect} into an Amount"
  end
end

.coerce_amount_arguments(arguments) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/amount/rspec_support.rb', line 8

def coerce_amount_arguments(arguments)
  case arguments.length
  when 1
    coerce_amount(arguments.first)
  when 2
    Amount.new(arguments.last, arguments.first)
  else
    raise ArgumentError, "expected an Amount, a parse string, or a symbol/value pair"
  end
end

.coerce_delta(expected_amount, within) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/amount/rspec_support.rb', line 29

def coerce_delta(expected_amount, within)
  case within
  when Amount
    within
  when Integer, Float, BigDecimal, Rational, String
    Amount.new(within, expected_amount.symbol)
  else
    raise ArgumentError, "cannot coerce #{within.inspect} into an amount delta"
  end
end

.normalize_amount_sums(sum_hash) ⇒ Object



40
41
42
43
44
45
# File 'lib/amount/rspec_support.rb', line 40

def normalize_amount_sums(sum_hash)
  sum_hash.to_h do |symbol, atomic|
    amount = Amount.new(atomic, symbol, from: :atomic)
    [amount.symbol, amount]
  end
end