Module: Amount::Conversion
- Included in:
- Amount
- Defined in:
- lib/amount/conversion.rb
Overview
Cross-type conversion via ‘to(target_symbol, rate:)`. Uses an explicitly passed rate when provided, otherwise looks up the registered directional default rate. The result class is taken from the target symbol’s registry entry, so a ‘class:`-registered subclass becomes the natural identity for conversion outputs.
Instance Method Summary collapse
Instance Method Details
#to(target_symbol, rate: nil) ⇒ Amount
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/amount/conversion.rb', line 20 def to(target_symbol, rate: nil) target_symbol = target_symbol.to_sym return self.class.new(@atomic, symbol, from: :atomic) if target_symbol == symbol rate = resolve_rate(target_symbol, rate) target_entry = self.class.registry.lookup(target_symbol) decimal_result = decimal * Amount.coerce_decimal(rate) atomic_result = (decimal_result * (BigDecimal(10)**target_entry.decimals)).to_i target_entry.amount_class.new(atomic_result, target_symbol, from: :atomic) end |