Class: Tally::CalculatorRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/tally/calculator_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, date) ⇒ CalculatorRunner

Returns a new instance of CalculatorRunner.



6
7
8
9
# File 'lib/tally/calculator_runner.rb', line 6

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

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



4
5
6
# File 'lib/tally/calculator_runner.rb', line 4

def date
  @date
end

Instance Method Details

#klassObject



11
12
13
# File 'lib/tally/calculator_runner.rb', line 11

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

#saveObject

loop through each value and save in db



16
17
18
19
20
21
22
23
24
# 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?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/tally/calculator_runner.rb', line 32

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

#valuesObject



26
27
28
29
30
# File 'lib/tally/calculator_runner.rb', line 26

def values
  return [] unless valid?

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