class Tally::CalculatorRunner

Attributes

date[R]

Public Class Methods

new(class_name, date) click to toggle source
# File lib/tally/calculator_runner.rb, line 6
def initialize(class_name, date)
  @date       = date
  @class_name = class_name
end

Public Instance Methods

klass() click to toggle source
# File lib/tally/calculator_runner.rb, line 11
def klass
  @klass ||= @class_name.to_s.safe_constantize
end
save() click to toggle source

loop through each value and save in db

# File lib/tally/calculator_runner.rb, line 16
def save
  return false unless valid?

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

  true
end
valid?() click to toggle source
# File lib/tally/calculator_runner.rb, line 32
def valid?
  klass.present? && date.present?
end
values() click to toggle source
# File lib/tally/calculator_runner.rb, line 26
def values
  return [] unless valid?

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

Private Instance Methods

create_record(attributes) click to toggle source
# File lib/tally/calculator_runner.rb, line 38
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