IsbnUtils 0.0.0.3 → 0.0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/Isbn10DigitValidator.rb +2 -3
- data/lib/Isbn13DigitValidator.rb +12 -10
- data/lib/IsbnValidator.rb +5 -3
- metadata +1 -1
data/lib/Isbn10DigitValidator.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'IsbnValidator'
|
2
2
|
class Isbn10DigitValidator < IsbnValidator
|
3
3
|
|
4
|
-
|
5
4
|
def initialize(isbn10)
|
6
5
|
@isbn = isbn10
|
7
6
|
end
|
@@ -9,9 +8,9 @@ class Isbn10DigitValidator < IsbnValidator
|
|
9
8
|
def checkDigit
|
10
9
|
@strippedIsbn = @isbn.gsub(/[^0-9a-z]/i,'')
|
11
10
|
if @strippedIsbn.length != 10
|
12
|
-
|
11
|
+
return false
|
13
12
|
end
|
14
|
-
|
13
|
+
return true
|
15
14
|
end
|
16
15
|
|
17
16
|
|
data/lib/Isbn13DigitValidator.rb
CHANGED
@@ -7,29 +7,31 @@ class Isbn13DigitValidator < IsbnValidator
|
|
7
7
|
@isbn = isbn13
|
8
8
|
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
10
|
+
|
11
|
+
def checkDigit
|
12
|
+
@strippedIsbn = @isbn.gsub(/[^0-9a-z]/i,'')
|
13
|
+
if @strippedIsbn.length != 13
|
14
|
+
return false
|
15
|
+
end
|
16
|
+
|
17
|
+
return true
|
18
|
+
end
|
17
19
|
|
18
20
|
|
19
21
|
def validateIsbn
|
20
22
|
check = 0
|
21
23
|
i = 0
|
22
24
|
while i < 12 do
|
23
|
-
check = check + @
|
25
|
+
check = check + @isbn[i].to_i
|
24
26
|
i = i + 2
|
25
27
|
end
|
26
28
|
|
27
29
|
i = 1
|
28
30
|
while i < 12 do
|
29
|
-
check = check + 3* @
|
31
|
+
check = check + 3* @isbn[i].to_i
|
30
32
|
i = i + 2
|
31
33
|
end
|
32
|
-
check = check + @
|
34
|
+
check = check + @isbn[12].to_i
|
33
35
|
return check % 10 == 0;
|
34
36
|
end
|
35
37
|
|
data/lib/IsbnValidator.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
|
2
2
|
# The IsbnValidator class defines a template method called validate , which represent
|
3
3
|
# a workflow to follow. The validate method calls checkDigit and validateIsbn methods
|
4
4
|
# which have its custom implementation in the subclasses.
|
@@ -11,8 +11,10 @@ class IsbnValidator
|
|
11
11
|
def validateIsbn; raise METHOD_MISSING; end
|
12
12
|
|
13
13
|
def validate
|
14
|
-
|
15
|
-
|
14
|
+
result = checkDigit()
|
15
|
+
if result?
|
16
|
+
result = validateIsbn()
|
17
|
+
end
|
16
18
|
return result
|
17
19
|
end
|
18
20
|
|