Sha256: a6ba1d0ec42b3e9dde4939c2aca418da6768a2f275bc0189bd26e504e9a57936

Contents?: true

Size: 551 Bytes

Versions: 2

Compression:

Stored size: 551 Bytes

Contents

class IsbnValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    unless valid?(value.to_s)
      record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.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

2 entries across 2 versions & 1 rubygems

Version Path
yukonisuru-1.0.0 lib/yukonisuru/validators/isbn_validator.rb
yukonisuru-0.0.1 lib/yukonisuru/validators/isbn_validator.rb