abstract class Money::Currency::RateStore

Included Modules

Extended Modules

Direct Known Subclasses

Defined in:

money/currency/rate_store.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) #

Alias of Converter.from_yaml.


def self.new(pull : JSON::PullParser) #

Alias of Converter.from_json.


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

[View source]

Class Method Detail

def self.find(name : String | Symbol) : Money::Currency::RateStore.class #

Returns the Money::Currency::RateStore.class for the given name if found, raises Money::Registry::NotFoundError otherwise.


def self.find?(name : String | Symbol) : Money::Currency::RateStore.class | Nil #

Returns the Money::Currency::RateStore.class for the given name if found, nil otherwise.


def self.registry #

All registered objects.


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 [](base : String | Symbol | Currency, target : 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 Money::UnknownRateError

[View source]
def []=(base : String | Symbol | Currency, target : 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 []?(base : String | Symbol | Currency, target : 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 : 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]
def ttl : Time::Span | Nil #

[View source]
def ttl=(ttl : Time::Span | Nil) #

[View source]