class RfcFacil::NaturalTenDigitsCodeCalculator

Constants

FORBIDDEN_WORDS
SPECIAL_PARTICLES
VOWEL_PATTERN

Attributes

person[RW]

Public Class Methods

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

Public Instance Methods

birthday_code() click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 39
def birthday_code
  "#{@person.year.to_s[-2, 2]}#{format('%02d', @person.month)}#{format('%02d', @person.day)}"
end
calculate() click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 16
def calculate
  "#{obfuscate_forbidden_words(name_code)}#{birthday_code}"
end
filter_name(name) click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 78
def filter_name(name)
  normalize(name).strip.sub(/^(MA|MA\.|MARIA|JOSE)\s+/, "")
end
first_last_name_empty?() click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 52
def first_last_name_empty?
  normalize(@person.first_last_name).nil? || normalize(@person.first_last_name).empty?
end
first_last_name_empty_form() click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 47
def first_last_name_empty_form
  first_two_letters_of(@person.second_last_name) <<
    first_two_letters_of(filter_name(@person.name))
end
first_last_name_is_too_short?() click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 67
def first_last_name_is_too_short?
  normalize(@person.first_last_name).length <= 2
end
first_last_name_too_short_form() click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 56
def first_last_name_too_short_form
  first_letter_of(@person.first_last_name) <<
    first_letter_of(@person.second_last_name) <<
    first_two_letters_of(filter_name(@person.name))
end
first_letter_of(word) click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 90
def first_letter_of(word)
  normalized_word = normalize(word)
  normalized_word[0]
end
first_two_letters_of(word) click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 62
def first_two_letters_of(word)
  normalized_word = normalize(word)
  normalized_word[0..1]
end
first_vowel_excluding_first_character_of(word) click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 113
def first_vowel_excluding_first_character_of(word)
  normalized_word = normalize(word)[1..-1]
  m = VOWEL_PATTERN.match(normalized_word)
  raise ArgumentError, "Word doesn't contain a vowel: #{normalized_word}" if m.nil?
  normalized_word[m.to_s[0]]
end
formatted_in_two_digits(number) click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 82
def formatted_in_two_digits(number)
  format('%02d', number)
end
last_two_digits_of(number) click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 86
def last_two_digits_of(number)
  formatted_in_two_digits(number % 100)
end
name_code() click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 27
def name_code
  return first_last_name_empty_form if first_last_name_empty?
  return second_last_name_empty_form if second_last_name_empty?
  return first_last_name_too_short_form if first_last_name_is_too_short?
  normal_form
end
normal_form() click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 71
def normal_form
  first_letter_of(@person.first_last_name) <<
    first_vowel_excluding_first_character_of(@person.first_last_name) <<
    first_letter_of(@person.second_last_name) <<
    first_letter_of(filter_name(@person.name))
end
normalize(word) click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 95
def normalize(word)
  return word if word.nil? || word.empty?
  normalized_word = UnicodeUtils.upcase(I18n.transliterate(word))
  remove_special_particles(normalized_word, SPECIAL_PARTICLES)
end
obfuscate_forbidden_words(name_code) click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 20
def obfuscate_forbidden_words(name_code)
  FORBIDDEN_WORDS.each do |forbidden|
    return "#{name_code[0..2]}X" if forbidden == name_code
  end
  name_code
end
remove_special_particles(word, special_particles) click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 101
def remove_special_particles(word, special_particles)
  new_word = word
  special_particles.each do |particle|
    pp = "#{particle} "
    while new_word.include?(pp)
      i = new_word.to_s.index(pp).to_i
      new_word.slice!(i..i + pp.length - 1)
    end
  end
  new_word.to_s
end
second_last_name_empty?() click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 43
def second_last_name_empty?
  normalize(@person.second_last_name).nil? || normalize(@person.second_last_name).empty?
end
second_last_name_empty_form() click to toggle source
# File lib/rfc_facil/natural_ten_digits_code_calculator.rb, line 34
def second_last_name_empty_form
  first_two_letters_of(@person.first_last_name) <<
    first_two_letters_of(filter_name(@person.name))
end