Class: MextSchoolCodeValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- MextSchoolCodeValidator
- Defined in:
- lib/mext_school_code_validator.rb,
lib/mext_school_code_validator/engine.rb
Overview
文科省学校コードのバリデーションを行うバリデーター
Defined Under Namespace
Classes: Engine
Constant Summary collapse
- LENGTH =
13
- SCHOOL_TYPE_ALPHABET_MAP =
{ "A" => "01", "B" => "02", "C" => "03", "D" => "04", "E" => "05", "F" => "06", "G" => "07", "H" => "08" }.freeze
- VALID_ORG_TYPE_REGEXP =
/\A[123]\z/.freeze
- VALID_PREFECTURE_NO_REGEXP =
/\A(?:0[1-9]|(?:[1-3][0-9])|(?:4[0-7]))\z/.freeze
- VALID_SCHOOL_NO_REGEXP =
/\A[1-9][0-9]{6}\z/.freeze
- VALID_SCHOOL_TYPE_REGEXP =
/\A(?:A1|A2|B1|C1|C2|D1|D2|E1|F1|F2|G1|H1|H2)/.freeze
Instance Method Summary collapse
-
#validate_each(record, attribute, value) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity.
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mext_school_code_validator.rb', line 18 def validate_each(record, attribute, value) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity value = value.presence.to_s if value.length != LENGTH record.errors.add(attribute, ([:message] || :wrong_length), count: LENGTH) elsif !value.match?(VALID_SCHOOL_TYPE_REGEXP) school_type = value[0, 2] = ([:message] || :mext_school_code_invalid_school_type) record.errors.add(attribute, , school_type: school_type) elsif !value[2, 2].match?(VALID_PREFECTURE_NO_REGEXP) prefecture_no = value[2, 2] = ([:message] || :mext_school_code_invalid_prefecture_no) record.errors.add(attribute, , prefecture_no: prefecture_no) elsif !value[4].match?(VALID_ORG_TYPE_REGEXP) = ([:message] || :mext_school_code_invalid_org_type) record.errors.add(attribute, , org_type: value[4]) elsif !value[5, 7].match?(VALID_SCHOOL_NO_REGEXP) = ([:message] || :mext_school_code_invalid_school_no) record.errors.add(attribute, , school_no: value[5, 7]) elsif calculate_check_digit(value).to_s != value[-1] = ([:message] || :mext_school_code_invalid_check_digit) record.errors.add(attribute, , expected: calculate_check_digit(value), actual: value[-1]) end end |