Class: Isbn10DigitValidator

Inherits:
IsbnValidator show all
Defined in:
lib/Isbn10DigitValidator.rb

Constant Summary

Constants inherited from IsbnValidator

IsbnValidator::METHOD_MISSING

Instance Method Summary collapse

Methods inherited from IsbnValidator

#validate

Constructor Details

#initialize(isbn10) ⇒ Isbn10DigitValidator

Returns a new instance of Isbn10DigitValidator.



4
5
6
# File 'lib/Isbn10DigitValidator.rb', line 4

def initialize(isbn10)
  @isbn = isbn10
end

Instance Method Details

#checkDigitObject



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

#validateIsbnObject



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