Class: ValidationExtensions::IsbnValidation::IsbnFormatValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ValidationExtensions::IsbnValidation::IsbnFormatValidator
- Defined in:
- lib/isbn_validation.rb
Instance Method Summary collapse
-
#initialize(options) ⇒ IsbnFormatValidator
constructor
A new instance of IsbnFormatValidator.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ IsbnFormatValidator
Returns a new instance of IsbnFormatValidator.
32 33 34 35 36 37 |
# File 'lib/isbn_validation.rb', line 32 def initialize() [:message] ||= "is not a valid ISBN code" [:allow_nil] ||= false [:allow_blank] ||= false super() end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/isbn_validation.rb', line 39 def validate_each(record, attribute, value) valid_isbn = case [:with] when :isbn10 validate_with_isbn10(value) when :isbn13 validate_with_isbn13(value) else validate_with_isbn10(value) || validate_with_isbn13(value) end unless valid_isbn || (value.nil? && [:allow_nil]) || (value.blank? && [:allow_blank]) record.errors.add(attribute, [:message]) end end |