Module: Sequel::CurrencyRateExchange

Defined in:
lib/sequel/extensions/currency_rates.rb

Instance Method Summary collapse

Instance Method Details

#exchange_to(currency, rates_table = :currency_rates) ⇒ Sequel::SQL::NumericExpression

Exchange column value to a specific currency

Examples:

Sequel[:amount].exchange_to("EUR", :order_rates)

Parameters:

  • currency (String)

    currency

  • rates_table (Symbol) (defaults to: :currency_rates)

    rates table name

Returns:

  • (Sequel::SQL::NumericExpression)


57
58
59
60
# File 'lib/sequel/extensions/currency_rates.rb', line 57

def exchange_to(currency, rates_table = :currency_rates)
  rate = Sequel[rates_table][:rates].pg_jsonb.get_text(currency).cast_numeric(Float)
  self * rate
end

#in_usd(*opts) ⇒ Sequel::SQL::NumericExpression

Exchange column value to usd

Examples:

Sequel[:amount].in_usd

Returns:

  • (Sequel::SQL::NumericExpression)


69
70
71
# File 'lib/sequel/extensions/currency_rates.rb', line 69

def in_usd(*opts)
  exchange_to("USD", *opts)
end