Class: BaseCalculatorService Abstract

Inherits:
Object
  • Object
show all
Defined in:
app/services/base_calculator_service.rb

Overview

This class is abstract.

An abstract base class for all calculator services, used to define a common interface.

All calculators can be called using 'call' on the class itself, which will return an instance with the results. The caller can then call @see help_available? and @see help_not_available? to determine if a decision has been made by this calculator. If both are false, then it cannot make a decision and the caller will generally move on to the next calculator in the chain.

The reasons for the decisions are given in the @see messages array, which contain a list of hashes looking like this

The caller can then use I18n translation to convert these into text to present to the user.

So, if we had a sub class like this

Then we could could call it like this

Examples:

A messages hash


{ key: :i18n_key, source: :underscored_name_of_calculator }

Defining a calculator service

class MyCalculatorService < BaseCalculatorService
  def call
    do_something
    self.help_available = true
    messages << { key: :likely, source: :my_calculator }
    self
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs) ⇒ BaseCalculatorService

Not expected to be used directly - use the class .call method instead


70
71
72
73
74
75
# File 'app/services/base_calculator_service.rb', line 70

def initialize(inputs)
  self.inputs = inputs
  self.messages = []
  self.available_help = :undecided
  self.final_decision = false
end

Instance Attribute Details

#available_helpSymbol


46
47
48
# File 'app/services/base_calculator_service.rb', line 46

def available_help
  @available_help
end

#messagesObject

Returns the value of attribute messages


44
45
46
# File 'app/services/base_calculator_service.rb', line 44

def messages
  @messages
end

#remissionFloat


46
47
48
# File 'app/services/base_calculator_service.rb', line 46

def remission
  @remission
end

Class Method Details

.call(inputs) ⇒ BaseCalculatorService

Perform the calculation


54
55
56
# File 'app/services/base_calculator_service.rb', line 54

def self.call(inputs)
  new(inputs).call
end

.fields_required(_inputs) ⇒ Object


64
65
66
# File 'app/services/base_calculator_service.rb', line 64

def self.fields_required(_inputs, *)
  raise 'Not Implemented'
end

.identifierSymbol

An identifier for use by the calculation service to store results against


60
61
62
# File 'app/services/base_calculator_service.rb', line 60

def self.identifier
  name.demodulize.gsub(/CalculatorService$/, '').underscore.to_sym
end

Instance Method Details

#callObject

Not expected to be used directly - use the class.call method instead. Performs the calculation


79
80
81
# File 'app/services/base_calculator_service.rb', line 79

def call
  raise 'Not Implemented'
end

#final_decision?Boolean

Indicates a final decision has been made The calculator engine need not bother with any more sub engines as they are not required


93
94
95
# File 'app/services/base_calculator_service.rb', line 93

def final_decision?
  final_decision
end

#valid?Boolean

Indicates if the inputs were all valid


86
87
88
# File 'app/services/base_calculator_service.rb', line 86

def valid?
  raise 'Not Implemented'
end