Class: Isbn13To10DigitConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/Isbn13To10DigitConverter.rb

Instance Method Summary collapse

Constructor Details

#initialize(isbn) ⇒ Isbn13To10DigitConverter

Returns a new instance of Isbn13To10DigitConverter.



7
8
9
# File 'lib/Isbn13To10DigitConverter.rb', line 7

def initialize(isbn)
	@isbn = isbn
end

Instance Method Details

#convert(isbn13) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/Isbn13To10DigitConverter.rb', line 26

def convert(isbn13)
  isbn10 = isbn13[3,9]
  checksum = 0
  weight = 10
  isbn10.each_char do |c|
 checksum += (c.to_i * weight)
 weight -= 1
  end
  checksum = 11-(checksum % 11)
  if (checksum == 10)
 isbn10 += "X"
  elsif (checksum == 11)
 isbn10 += "0"
  else
 isbn10 += checksum.to_s
  end
  isbn10
end

#processObject



16
17
18
19
20
21
22
23
# File 'lib/Isbn13To10DigitConverter.rb', line 16

def process
 @strippedIsbn =  @isbn.gsub(/[^0-9a-z]/i,'')
 if( @strippedIsbn.length() == 13 )
		return convert(@strippedIsbn)
	else (@strippedIsbn.length() == 10 )
	  @isbn10To13Converter.process
  end
end

#set10To13DigitConverter(isbn10To13Converter) ⇒ Object



11
12
13
# File 'lib/Isbn13To10DigitConverter.rb', line 11

def set10To13DigitConverter(isbn10To13Converter)
	@isbn10To13Converter = isbn10To13Converter
end