Sha256: ad45fb5d483d595038bc748cc4a1058d37e7a7482094bc44bc0123eaf8ad1311

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

module Tally
  class Archiver

    def initialize(key: nil, day: nil, record: nil, type: nil)
      @key = key
      @day = day
      @record = record
      @type = type
    end

    def archive!
      remove_existing_records

      finder.entries.each do |entry|
        next if entry.type.present? && !entry.record

        record = if entry.record
          Record.find_or_initialize_by(day: entry.date, key: entry.key, recordable: entry.record)
        else
          Record.find_or_initialize_by(day: entry.date, key: entry.key, recordable: nil)
        end

        record.value = entry.value
        record.save
      end

      enqueue_registered_calculators

      true
    end

    def day
      @day ||= Time.current.utc.to_date
    end

    def self.archive!(*args)
      new(*args).archive!
    end

    private

      def enqueue_registered_calculators
        day_str = day.strftime("%Y-%m-%d")
        calculate_method = Tally.config.perform_calculators == :now ? :perform_now : :perform_later

        Tally.calculators.each do |class_name|
          CalculatorRunnerJob.public_send(calculate_method, class_name, day_str)
        end
      end

      def finder
        @finder ||= Daily.new(key: @key, day: @day, record: @record, type: @type)
      end

      def remove_existing_records
        return if @key.present?
        return if @record.present?
        return if @type.present?

        Record.where(day: day).delete_all
      end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tally-1.0.0 lib/tally/archiver.rb
tally-1.0.0.beta1 lib/tally/archiver.rb