Sha256: b04f895c7112fffa5656f87df6a020b9456dbde1a3dd80395c8d82e47dde8ed0

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

module Danconia
  module Stores
    # Store implementation that persist rates using ActiveRecord.
    class ActiveRecord
      # @param unique_keys [Array] each save_rates will update records with this keys' values
      # @param date_field [Symbol] used when storing daily rates
      def initialize unique_keys: %i[pair], date_field: nil
        @unique_keys = unique_keys
        @date_field = date_field
      end

      # Creates or updates the rates by the `unique_keys` provided in the constructor.
      #
      # @param rates [Array] must be an array of maps.
      def save_rates rates
        ExchangeRate.transaction do
          rates.each do |fields|
            ExchangeRate
              .where(fields.slice(*@unique_keys))
              .first_or_initialize
              .update(fields.slice(*ExchangeRate.column_names.map(&:to_sym)))
          end
        end
      end

      # Returns an array of maps like the one it received.
      def rates **filters
        ExchangeRate.where(process_filters(filters)).map { |er| er.attributes.symbolize_keys }
      end

      private

      def process_filters filters
        if @date_field
          param = filters.delete(@date_field) || Date.today
          last_record = ExchangeRate.where(filters).where("#{@date_field} <= ?", param).order(@date_field => :desc).first
          filters.merge(@date_field => (last_record[@date_field] if last_record))
        else
          filters
        end
      end
    end

    class ExchangeRate < ::ActiveRecord::Base
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
danconia-0.4.0 lib/danconia/stores/active_record.rb
danconia-0.3.1 lib/danconia/stores/active_record.rb
danconia-0.3.0 lib/danconia/stores/active_record.rb