Sha256: 120a20db0caf8ea6dda439b0a54fd5adaeb41b8ae1f755e285b66975bce1a54d

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

module Tally
  class CalculatorRunner

    attr_reader :date

    def initialize(class_name, date)
      @date       = date
      @class_name = class_name
    end

    def klass
      @klass ||= @class_name.to_s.safe_constantize
    end

    # loop through each value and save in db
    def save
      return false unless valid?

      values.each do |attributes|
        create_record(attributes)
      end

      true
    end

    def values
      return [] unless valid?

      @values ||= [ klass.new(date).call ].flatten
    end

    def valid?
      klass.present? && date.present?
    end

    private

      def create_record(attributes)
        finder = { day: date, key: attributes[:key] }

        id = attributes.delete(:id)
        type = attributes.delete(:type)

        if id && type
          finder[:recordable_id] = id
          finder[:recordable_type] = type.to_s.classify
        end

        record = Record.find_or_initialize_by(finder)

        record.attributes = attributes
        record.save
      end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
tally-2.1.0 lib/tally/calculator_runner.rb
tally-2.0.0 lib/tally/calculator_runner.rb
tally-1.0.2 lib/tally/calculator_runner.rb
tally-1.0.1 lib/tally/calculator_runner.rb
tally-1.0.0 lib/tally/calculator_runner.rb
tally-1.0.0.beta1 lib/tally/calculator_runner.rb