Class: UOB::Payroll::HashCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/uob/payroll/hash_calculator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header:, rows:) ⇒ HashCalculator

Returns a new instance of HashCalculator.



16
17
18
19
# File 'lib/uob/payroll/hash_calculator.rb', line 16

def initialize(header:, rows:)
  @header = header
  @rows = rows
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



26
27
28
# File 'lib/uob/payroll/hash_calculator.rb', line 26

def header
  @header
end

#rowsObject (readonly)

Returns the value of attribute rows.



26
27
28
# File 'lib/uob/payroll/hash_calculator.rb', line 26

def rows
  @rows
end

Class Method Details

.calculate(header:, rows:) ⇒ Object



11
12
13
# File 'lib/uob/payroll/hash_calculator.rb', line 11

def calculate(header:, rows:)
  new(header: header, rows: rows).calculate
end

Instance Method Details

#calculateObject



21
22
23
24
# File 'lib/uob/payroll/hash_calculator.rb', line 21

def calculate
  sum_header +
    sum_rows
end

#sum_headerObject



28
29
30
31
32
# File 'lib/uob/payroll/hash_calculator.rb', line 28

def sum_header
  calculate_string(header.originating_bic_code) +
    calculate_padded_string(string: header., size: 34) +
    calculate_padded_string(string: sanitize(header.), size: 140)
end

#sum_rowsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/uob/payroll/hash_calculator.rb', line 34

def sum_rows
  hash_code = 0
  sum = 0

  rows.each.map do |row|
    hash_code = 0 if hash_code == 9
    hash_code += 1

    sum += calculate_string(row.receiving_bic_code) +
      hash_code * calculate_padded_string(string: row., size: 34) +
      hash_code * calculate_padded_string(string: sanitize(row.), size: 140) +
      calculate_payment_type(hash_code) +
      calculate_string('SGD') +
      calculate_padded_string(string: row.formatted_amount, size: 18, pad: '0', just: :right) +
      calculate_string('SALA')
  end

  sum
end