Sha256: ec4f22def801263200ac583a5e424a3933209e9e40e954853373a204c05909b9
Contents?: true
Size: 1.23 KB
Versions: 2
Compression:
Stored size: 1.23 KB
Contents
module Faker module Russian module Okato 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 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 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 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 end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
faker-russian-0.0.6 | lib/faker/russian/okato.rb |
faker-russian-0.0.5 | lib/faker/russian/okato.rb |