Class: CalculationFormService

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

Overview

Used to find a form class for a given field from the calculation input data

CalculationFormService.for(:marital_status) # => MaritalStatusForm

Examples:

Finding a form


Constant Summary collapse

FORM_CLASSES =
{
  nil => NilForm,
  marital_status: MaritalStatusForm,
  fee: FeeForm,
  date_of_birth: DateOfBirthForm,
  disposable_capital: DisposableCapitalForm,
  benefits_received: BenefitsReceivedForm,
  number_of_children: NumberOfChildrenForm,
  total_income: TotalIncomeForm

}.freeze

Class Method Summary collapse

Class Method Details

.for(form) ⇒ NilForm, ...

Returns a form class for the given form symbol

Parameters:

  • form (Symbol, Nil)

    The form symbol or nil

Returns:

Raises:

  • (ArgumentError)

    If the form specified was not found


27
28
29
30
31
# File 'app/services/calculation_form_service.rb', line 27

def self.for(form)
  klass = FORM_CLASSES[form]
  raise ArgumentError, "Unknown form class for '#{form}'" if klass.nil?
  klass
end

.for_field(field) ⇒ NilForm, ...

Returns a form class who is the first in the chain to be responsible for the given field symbol

Parameters:

  • form (Symbol, Nil)

    The form symbol or nil

Returns:

Raises:

  • (ArgumentError)

    If the field specified was not found in any forms


40
41
42
43
44
45
46
# File 'app/services/calculation_form_service.rb', line 40

def self.for_field(field)
  result = FORM_CLASSES.values.find do |form_class|
    form_class.attribute?(field)
  end
  raise ArgumentError, "Field #{field} was not managed by any of the registered forms" if result.nil?
  result
end