Sha256: 26887afcbc03f3f45cb8de7866bf7f5fe6b802220097da83f208f9cd7e844f2a

Contents?: true

Size: 557 Bytes

Versions: 11

Compression:

Stored size: 557 Bytes

Contents

class IsbnValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    unless valid?(value.to_s)
      record.errors[attribute] << (options[:message] || I18n.t('flash_validators.errors.messages.isbn'))
    end
  end

  private

  def valid_format?(value)
    value = '' if value.nil?
    value.gsub!(/-| /, '')
    value.downcase!

    [10, 13].include?(value.size) &&
    value.chars.all? { |char| ['0','1','2','3','4','5','6','7','8','9','0','x'].include?(char) }
  end

  def valid?(value)
    valid_format?(value)
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
flash_validators-3.0.12 lib/flash_validators/validators/isbn_validator.rb
flash_validators-3.0.11 lib/flash_validators/validators/isbn_validator.rb
flash_validators-3.0.10 lib/flash_validators/validators/isbn_validator.rb
flash_validators-3.0.9 lib/flash_validators/validators/isbn_validator.rb
flash_validators-3.0.8 lib/flash_validators/validators/isbn_validator.rb
flash_validators-3.0.7 lib/flash_validators/validators/isbn_validator.rb
flash_validators-3.0.6 lib/flash_validators/validators/isbn_validator.rb
flash_validators-3.0.5 lib/flash_validators/validators/isbn_validator.rb
flash_validators-3.0.4 lib/flash_validators/validators/isbn_validator.rb
flash_validators-3.0.3 lib/flash_validators/validators/isbn_validator.rb
flash_validators-3.0.2 lib/flash_validators/validators/isbn_validator.rb