Module: Petrarca::ISBN13

Extended by:
ISBN13
Includes:
Helpers
Included in:
ISBN13
Defined in:
lib/petrarca/isbn13.rb

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
# File 'lib/petrarca/isbn13.rb', line 20

def calc_check_digit(isbn)
  nums = isbn.delete("-").split("")[0..11].map{|x| x.to_i }
  sum = nums.zip([1, 3] * 6).map{|x, y| x * y }.inject(:+)
  check_digit = 10 - (sum % 10)
  check_digit == 10 ? "0" : check_digit.to_s
end

#correct_format?(isbn) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/petrarca/isbn13.rb', line 15

def correct_format?(isbn)
  isbn = isbn.delete("-")
  !!(/\A97[89]\d{9}\d\z/ =~ isbn)
end

#hyphenate(isbn) ⇒ Object



27
28
29
# File 'lib/petrarca/isbn13.rb', line 27

def hyphenate(isbn)
  hyphenate13(isbn)
end

#valid?(isbn) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid?(isbn)
  correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
end