module Faker::Russian::Okato

Public Instance Methods

okato(options = {}) click to toggle source
# File lib/faker/russian/okato.rb, line 4
def okato(options = {})
  options.assert_valid_keys(:sequence_number, :okato_region_number, :length)

  sequence = find_sequence(options[:sequence_number])
  okato_region_number = find_region_number(options[:okato_region_number], sequence, :okato_region_numbers)
  okato_without_calc = okato_region_number + find_length_digits(options[:length], sequence)
  okato_without_calc + calc_okato(okato_without_calc)
end

Private Instance Methods

calc_okato(okato_without_calc) click to toggle source
# File lib/faker/russian/okato.rb, line 25
def calc_okato(okato_without_calc)
  ((calc_okato_digit(okato_without_calc, 1) < 10) ? calc_okato_digit(okato_without_calc, 1) : calc_okato_digit(okato_without_calc, 3) % 10).to_s
end
calc_okato_digit(okato_without_calc, adding_number) click to toggle source
# File lib/faker/russian/okato.rb, line 29
def calc_okato_digit(okato_without_calc, adding_number)
  okato_without_calc.split(//).map(&:to_i).each_with_index.inject(0){ |s, p| s + p[0] * (p[1] + adding_number) } % 11
end
find_length_digits(length, sequence) click to toggle source
# File lib/faker/russian/okato.rb, line 15
def find_length_digits(length, sequence)
  if length.present?
    raise 'no such length for okato' unless %w{3 6 9}.include?(length.to_s)
  else
    length = [3, 6, 9][sequence.rand(3)]
  end

  sprintf("%06d", sequence.rand(1_000_000)).split(//).first(length.to_i - 3).join
end