Module: Petrarca::ISBN10
Instance Method Summary
collapse
Methods included from Helpers
#hyphenate13, load_ranges, #split_to_parts
Instance Method Details
#calc_check_digit(isbn) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/petrarca/isbn10.rb', line 20
def calc_check_digit(isbn)
nums = isbn.delete("-").split("")[0..8].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
|
15
16
17
18
|
# File 'lib/petrarca/isbn10.rb', line 15
def correct_format?(isbn)
isbn = isbn.delete("-")
!!(/\A\d{9}[0-9X]\z/ =~ isbn)
end
|
#hyphenate(isbn) ⇒ Object
34
35
36
37
|
# File 'lib/petrarca/isbn10.rb', line 34
def hyphenate(isbn)
s = hyphenate13("978" + isbn)
s.sub(/^978-/, "")
end
|
#valid?(isbn) ⇒ Boolean
11
12
13
|
# File 'lib/petrarca/isbn10.rb', line 11
def valid?(isbn)
correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
end
|