Class: Barby::Bookland::ISBN

Inherits:
Object
  • Object
show all
Defined in:
lib/barby/barcode/bookland.rb

Overview

Encapsulates an ISBN number

isbn = ISBN.new(‘978-0-306-40615’)

Constant Summary collapse

DEFAULT_PREFIX =
'978'
PATTERN =
/\A(?<prefix>\d\d\d)?(?<number>\d{9})(?<checksum>\d)?\Z/
ISBN_10_CHECKSUM_MULTIPLIERS =
[10,9,8,7,6,5,4,3,2]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(isbn) ⇒ ISBN

Takes one argument, which is the ISBN string with or without prefix and/or check digit. Non-digit characters like hyphens and spaces are ignored.

Prefix is 978 if not given.



58
59
60
# File 'lib/barby/barcode/bookland.rb', line 58

def initialize(isbn)
  self.isbn = isbn
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



51
52
53
# File 'lib/barby/barcode/bookland.rb', line 51

def number
  @number
end

Instance Method Details

#checksumObject

Calculates the ISBN 13-digit checksum following the algorithm from: en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-13_check_digit_calculation



112
113
114
115
116
# File 'lib/barby/barcode/bookland.rb', line 112

def checksum
  (10 - (digits.each_with_index.inject(0) do |sum, (digit, index)|
    sum + (digit * (index.even? ? 1 : 3))
  end % 10)) % 10
end

#digitsObject



101
102
103
# File 'lib/barby/barcode/bookland.rb', line 101

def digits
  (prefix+number).split('').map(&:to_i)
end

#formatted_isbnObject



88
89
90
# File 'lib/barby/barcode/bookland.rb', line 88

def formatted_isbn
  "#{prefix}-#{number}-#{checksum}"
end

#formatted_isbn_10Object



92
93
94
# File 'lib/barby/barcode/bookland.rb', line 92

def formatted_isbn_10
  "#{number}-#{checksum}"
end

#inspectObject



134
135
136
# File 'lib/barby/barcode/bookland.rb', line 134

def inspect
  "#<#{self.class}:0x#{'%014x' % object_id} #{formatted_isbn}>"
end

#isbnObject



72
73
74
# File 'lib/barby/barcode/bookland.rb', line 72

def isbn
  "#{prefix}#{number}"
end

#isbn=(isbn) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/barby/barcode/bookland.rb', line 63

def isbn=(isbn)
  if match = PATTERN.match(isbn.gsub(/\D/, ''))
    @number = match[:number]
    @prefix = match[:prefix]
  else
    raise ArgumentError, "Not a valid ISBN: #{isbn}"
  end
end

#isbn_10Object



80
81
82
# File 'lib/barby/barcode/bookland.rb', line 80

def isbn_10
  number
end

#isbn_10_checksumObject

Calculates the ISBN 10-digit checksum following the algorithm from: en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-10_check_digit_calculation



123
124
125
126
127
# File 'lib/barby/barcode/bookland.rb', line 123

def isbn_10_checksum
  isbn_10_digits.zip(ISBN_10_CHECKSUM_MULTIPLIERS).inject(0) do |sum, (digit, multiplier)|
    sum + (digit * multiplier)
  end
end

#isbn_10_digitsObject



105
106
107
# File 'lib/barby/barcode/bookland.rb', line 105

def isbn_10_digits
  number.split('').map(&:to_i)
end

#isbn_10_with_checksumObject



84
85
86
# File 'lib/barby/barcode/bookland.rb', line 84

def isbn_10_with_checksum
  "#{number}#{checksum}"
end

#isbn_with_checksumObject



76
77
78
# File 'lib/barby/barcode/bookland.rb', line 76

def isbn_with_checksum
  "#{isbn}#{checksum}"
end

#prefixObject



97
98
99
# File 'lib/barby/barcode/bookland.rb', line 97

def prefix
  @prefix || DEFAULT_PREFIX
end

#to_sObject



130
131
132
# File 'lib/barby/barcode/bookland.rb', line 130

def to_s
  isbn_with_checksum
end