Sha256: 991253fa1e39ce6032db96873766476d8a32c6f561b188db66e72bc593a9cff3

Contents?: true

Size: 902 Bytes

Versions: 1

Compression:

Stored size: 902 Bytes

Contents

module Trade
  module Model::ExchangeRate
    extend ActiveSupport::Concern

    included do
      attribute :from, :string
      attribute :to, :string
      attribute :rate, :decimal
    end

    class_methods do
      def get_rate(from_iso_code, to_iso_code)
        rate = find_by(from: from_iso_code, to: to_iso_code)
        rate&.rate
      end

      def add_rate(from_iso_code, to_iso_code, rate)
        exrate = find_or_initialize_by(from: from_iso_code, to: to_iso_code)
        exrate.rate = rate
        exrate.save!
      end

      def each_rate
        return find_each unless block_given?

        find_each do |rate|
          yield rate.from, rate.to, rate.rate
        end
      end

      def currency_options
        r = {}

        Money::Currency.table.each do |_, value|
          r.merge! value[:name] => value[:iso_code]
        end

        r
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_trade-0.0.3 app/models/trade/model/exchange_rate.rb