Module: Petrarca::ISBN10
Instance Method Summary collapse
- #calc_check_digit(isbn) ⇒ Object
- #correct_format?(isbn) ⇒ Boolean
- #hyphenate(isbn) ⇒ Object
- #valid?(isbn) ⇒ Boolean
Instance Method Details
#calc_check_digit(isbn) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/petrarca/isbn10.rb', line 18 def calc_check_digit(isbn) nums = isbn.delete("-").split("")[0, 9].map{|x| x.to_i } sum = nums.zip((2..10).to_a.reverse).map{|x, y| x * y }.inject(:+) check_digit = 11 - (sum % 11) case check_digit when 10 "X" when 11 "0" else check_digit.to_s end end |
#correct_format?(isbn) ⇒ Boolean
13 14 15 16 |
# File 'lib/petrarca/isbn10.rb', line 13 def correct_format?(isbn) isbn = isbn.delete("-") !!(/\A\d{9}[0-9X]\z/ =~ isbn) end |
#hyphenate(isbn) ⇒ Object
32 33 34 35 |
# File 'lib/petrarca/isbn10.rb', line 32 def hyphenate(isbn) s = ISBN13.hyphenate("978" + isbn) s.sub(/^978-/, "") end |
#valid?(isbn) ⇒ Boolean
9 10 11 |
# File 'lib/petrarca/isbn10.rb', line 9 def valid?(isbn) correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn) end |