abstract class Money::Currency::RateStore

Included Modules

Direct Known Subclasses

Defined in:

money/currency/rate_store.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(ttl : Time::Span | Nil = nil) #

[View source]

Instance Method Detail

def <<(rate : Rate) : self #

Registers a conversion rate.

store = Money::Currency::RateStore::Memory.new
store << Rate.new(
  Money::Currency.find("USD"),
  Money::Currency.find("CAD"),
  1.24515.to_big_d
)
store << Rate.new(
  Money::Currency.find("CAD"),
  Money::Currency.find("USD"),
  0.803115.to_big_d
)

[View source]
def <<(rates : Enumerable(Rate)) : self #

Registers multiple conversion rates.

store = Money::Currency::RateStore::Memory.new
store << [
  Rate.new(
    Money::Currency.find("USD"),
    Money::Currency.find("CAD"),
    1.24515.to_big_d
  ),
  Rate.new(
    Money::Currency.find("CAD"),
    Money::Currency.find("USD"),
    0.803115.to_big_d
  ),
]

[View source]
def [](from : String | Symbol | Currency, to : String | Symbol | Currency) : BigDecimal #

Retrieves the rate for the given currency pair or raises UnknownRateError if not found.

store = Money::Currency::RateStore::Memory.new
store["USD", "CAD"] = 1.24515

store["USD", "CAD"] # => 1.24515
store["CAD", "USD"] # raises UnknownRateError

[View source]
def []=(from : String | Symbol | Currency, to : String | Symbol | Currency, value : Number) : Nil #

Registers a conversion rate.

store = Money::Currency::RateStore::Memory.new
store["USD", "CAD"] = 1.24515
store["CAD", "USD"] = 0.803115

[View source]
def []?(from : String | Symbol | Currency, to : String | Symbol | Currency) : BigDecimal | Nil #

Retrieves the rate for the given currency pair or nil if not found.

store = Money::Currency::RateStore::Memory.new
store["USD", "CAD"] = 1.24515

store["USD", "CAD"]? # => 1.24515
store["CAD", "USD"]? # => nil

[View source]
def clear(base_currency : String | Symbol | Currency) : Nil #

Removes rates for the given base currency.


[View source]
def clear : Nil #

Empties currency rate index.


[View source]
def each(& : Rate -> _) : Nil #

Iterates over list of Rate objects.

store.each do |rate|
  puts rate
end

[View source]
def rates : Array(Rate) #

Alias of #to_a.


[View source]
def transaction(*, mutable : Bool = false, & : -> _) #

Wraps block execution in a concurrency-safe transaction.


[View source]