class RfcFacil::HomoclaveCalculator

Constants

FULL_NAME_MAPPING
HOMOCLAVE_DIGITS

Attributes

full_name[RW]
homoclave[RW]
mapped_full_name[RW]
pairs_of_digits_sum[RW]
person[RW]

Public Class Methods

new(person) click to toggle source
# File lib/rfc_facil/homoclave_calculator.rb, line 15
def initialize(person)
  @person = person
end

Public Instance Methods

calculate() click to toggle source
# File lib/rfc_facil/homoclave_calculator.rb, line 19
def calculate
  normalize_full_name
  map_full_name_to_digits_code
  sum_pairs_of_digits
  build_homoclave

  @homoclave
end

Private Instance Methods

add_missing_char_to_full_name(raw_full_name, missing_char) click to toggle source
# File lib/rfc_facil/homoclave_calculator.rb, line 66
def add_missing_char_to_full_name(raw_full_name, missing_char)
  index = raw_full_name.index(missing_char)
  until index.nil?
    @full_name[index] = missing_char
    index = raw_full_name.index(missing_char, index + 1)
  end
end
build_homoclave() click to toggle source
# File lib/rfc_facil/homoclave_calculator.rb, line 30
def build_homoclave
  last_three_digits = (@pairs_of_digits_sum % 1000)
  quo = (last_three_digits / 34)
  reminder = (last_three_digits % 34)
  @homoclave = "#{HOMOCLAVE_DIGITS[quo]}#{HOMOCLAVE_DIGITS[reminder]}"
end
map_character_to_two_digit_code(c) click to toggle source
# File lib/rfc_facil/homoclave_calculator.rb, line 54
def map_character_to_two_digit_code(c)
  return FULL_NAME_MAPPING[c] if FULL_NAME_MAPPING.key?(c)
  raise ArgumentError, "No two-digit-code mapping for char: #{c}"
end
map_full_name_to_digits_code() click to toggle source
# File lib/rfc_facil/homoclave_calculator.rb, line 47
def map_full_name_to_digits_code
  @mapped_full_name = '0'
  @full_name.each_char do |c|
    @mapped_full_name << map_character_to_two_digit_code(c)
  end
end
normalize_full_name() click to toggle source
# File lib/rfc_facil/homoclave_calculator.rb, line 59
def normalize_full_name
  raw_full_name = UnicodeUtils.upcase(@person.to_s)
  @full_name = I18n.transliterate(raw_full_name)
  @full_name.gsub!(/[-.']/, '') # remove .'-
  add_missing_char_to_full_name(raw_full_name, 'Ñ')
end
sum_pairs_of_digits() click to toggle source
# File lib/rfc_facil/homoclave_calculator.rb, line 37
def sum_pairs_of_digits
  @pairs_of_digits_sum = 0
  (0..@mapped_full_name.length - 2).each do |i|
    num1 = @mapped_full_name[i..i + 1].to_i
    num2 = @mapped_full_name[i + 1..i + 1].to_i

    @pairs_of_digits_sum += num1 * num2
  end
end