Class: Isbn10DigitValidator
Constant Summary
IsbnValidator::METHOD_MISSING
Instance Method Summary
collapse
#validate
Constructor Details
Returns a new instance of Isbn10DigitValidator.
4
5
6
|
# File 'lib/Isbn10DigitValidator.rb', line 4
def initialize(isbn10)
@isbn = isbn10
end
|
Instance Method Details
#checkDigit ⇒ Object
8
9
10
11
12
13
14
|
# File 'lib/Isbn10DigitValidator.rb', line 8
def checkDigit
@strippedIsbn = @isbn.gsub(/[^0-9a-z]/i,'')
if @strippedIsbn.length != 10
return false
end
return true
end
|
#validateIsbn ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/Isbn10DigitValidator.rb', line 17
def validateIsbn
a = 0
for i in 0..9
if (@strippedIsbn[i] == "X" || @strippedIsbn[i] == "x")
a += 10 *(10-i)
elsif @strippedIsbn[i].match(/^(\d)+$/)
a += @strippedIsbn[i].to_i * (10-i)
else
return false
end
end
return ( a%11 == 0 )
end
|