lib/faker/russian/inn.rb in faker-russian-0.0.3 vs lib/faker/russian/inn.rb in faker-russian-0.0.4

- old
+ new

@@ -1,12 +1,10 @@ module Faker module Russian module Inn def inn(options = {}) - if (options.keys - [:sequence_number, :region_number, :kind]).any? - raise 'wrong options. just :kind, :region_number or :sequence_number' - end + options.assert_valid_keys(:sequence_number, :region_number, :kind) sequence = find_sequence(options[:sequence_number]) region_number = find_region_number(options[:region_number], sequence) kind = find_kind(options[:kind], sequence) sequence_digits = find_digits(kind, sequence) @@ -29,16 +27,16 @@ (kind == :legal) ? sequence.rand(1_000_000..9_999_999).to_s : sequence.rand(10_000_000..99_999_999).to_s end def check_digit(digits) if digits.length == 9 - calc(P10, digits) + calc_inn(P10, digits) else - calc(P11, digits) + calc(P12, digits + calc(P11, digits)) + calc_inn(P11, digits) + calc_inn(P12, digits + calc_inn(P11, digits)) end end - def calc(p, inn) + def calc_inn(p, inn) (p.each_with_index.inject(0){ |s, p| s + p[0] * inn[p[1]].to_i } % 11 % 10).to_s end P10 = [2, 4, 10, 3, 5, 9, 4, 6, 8] P11 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8]