bookland 0.3.1 → 1.0.0
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/Gemfile +2 -5
- data/Gemfile.lock +15 -22
- data/README.md +19 -26
- data/Rakefile +6 -17
- data/bookland.gemspec +16 -49
- data/lib/bookland.rb +1 -105
- data/lib/bookland/isbn.rb +104 -0
- data/lib/bookland/version.rb +3 -0
- data/spec/bookland/isbn_spec.rb +138 -0
- metadata +38 -22
- data/spec/bookland_spec.rb +0 -111
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,31 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
bookland (1.0.0)
|
5
|
+
|
1
6
|
GEM
|
2
7
|
remote: http://rubygems.org/
|
3
8
|
specs:
|
4
9
|
diff-lcs (1.1.2)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
rspec (2.0.0.beta.19)
|
14
|
-
rspec-core (= 2.0.0.beta.19)
|
15
|
-
rspec-expectations (= 2.0.0.beta.19)
|
16
|
-
rspec-mocks (= 2.0.0.beta.19)
|
17
|
-
rspec-core (2.0.0.beta.19)
|
18
|
-
rspec-expectations (2.0.0.beta.19)
|
19
|
-
diff-lcs (>= 1.1.2)
|
20
|
-
rspec-mocks (2.0.0.beta.19)
|
21
|
-
rubyforge (2.0.4)
|
22
|
-
json_pure (>= 1.1.7)
|
10
|
+
rspec (2.5.0)
|
11
|
+
rspec-core (~> 2.5.0)
|
12
|
+
rspec-expectations (~> 2.5.0)
|
13
|
+
rspec-mocks (~> 2.5.0)
|
14
|
+
rspec-core (2.5.1)
|
15
|
+
rspec-expectations (2.5.0)
|
16
|
+
diff-lcs (~> 1.1.2)
|
17
|
+
rspec-mocks (2.5.0)
|
23
18
|
|
24
19
|
PLATFORMS
|
25
20
|
ruby
|
26
21
|
|
27
22
|
DEPENDENCIES
|
28
|
-
|
29
|
-
|
30
|
-
rake
|
31
|
-
rspec (~> 2.0.0.beta.19)
|
23
|
+
bookland!
|
24
|
+
rspec (~> 2.5.0)
|
data/README.md
CHANGED
@@ -1,46 +1,39 @@
|
|
1
1
|
Bookland
|
2
2
|
========
|
3
3
|
|
4
|
-
Bookland provides
|
5
|
-
|
6
|
-

|
4
|
+
[Bookland](http://en.wikipedia.org/wiki/Bookland) provides an ISBN class in Ruby.
|
7
5
|
|
8
6
|
Examples
|
9
7
|
--------
|
10
8
|
|
11
9
|
include Bookland
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
|
11
|
+
isbn10 = ISBN.new('0262011530')
|
12
|
+
isbn10.to_isbn13
|
15
13
|
=> "9780262011532"
|
16
|
-
|
17
|
-
|
14
|
+
|
15
|
+
isbn10.to_s(1, 3, 5)
|
18
16
|
=> "0-262-01153-0"
|
19
|
-
|
20
|
-
|
17
|
+
|
18
|
+
isbn13 == ISBN.new('9780262011532')
|
21
19
|
=> true
|
22
|
-
|
20
|
+
|
23
21
|
# An invalid ISBN
|
24
|
-
|
25
|
-
|
22
|
+
not_an_isbn = ISBN.new('0262011531')
|
23
|
+
not_an_isbn.valid?
|
26
24
|
=> false
|
27
|
-
|
28
|
-
|
29
|
-
=> Bookland::ISBNError: ISBN not valid
|
25
|
+
not_an_isbn.to_isbn13
|
26
|
+
=> Bookland::ISBNError: Invalid ISBN
|
30
27
|
|
31
|
-
|
28
|
+
Some utility methods defined in the class level:
|
32
29
|
|
33
30
|
include Bookland
|
34
|
-
|
31
|
+
|
32
|
+
ISBN.to_13('0262011530')
|
35
33
|
=> "9780262011532"
|
36
|
-
|
37
|
-
ISBN.to_10(
|
34
|
+
|
35
|
+
ISBN.to_10('9780262011532')
|
38
36
|
=> "0262011530"
|
39
37
|
|
40
|
-
ISBN.valid?(
|
38
|
+
ISBN.valid?('9780262011532')
|
41
39
|
=> true
|
42
|
-
|
43
|
-
Compatibility
|
44
|
-
-------------
|
45
|
-
|
46
|
-
Specs pass against all usual suspects, including Ruby 1.8.7, 1.9.1, and 1.9.2.
|
data/Rakefile
CHANGED
@@ -1,22 +1,11 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require "jeweler"
|
4
|
-
require "rspec/core/rake_task"
|
1
|
+
require 'bundler'
|
2
|
+
require 'rspec/core/rake_task'
|
5
3
|
|
6
|
-
|
7
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
8
|
-
spec.pattern = "spec/**/*_spec.rb"
|
9
|
-
end
|
4
|
+
Bundler::GemHelper.install_tasks
|
10
5
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
gemspec.description = "A simple ISBN class in Ruby"
|
15
|
-
gemspec.email = "code@papercavalier.com"
|
16
|
-
gemspec.homepage = "http://github.com/papercavalier/bookland"
|
17
|
-
gemspec.authors = ["Hakan Ensari", "Piotr Laszewski"]
|
6
|
+
desc 'Run all specs in spec directory'
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
8
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
18
9
|
end
|
19
10
|
|
20
|
-
Jeweler::GemcutterTasks.new
|
21
|
-
|
22
11
|
task :default => :spec
|
data/bookland.gemspec
CHANGED
@@ -1,56 +1,23 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'bookland/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |s|
|
7
|
-
s.name
|
8
|
-
s.version
|
6
|
+
s.name = 'bookland'
|
7
|
+
s.version = Bookland::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Paper Cavalier']
|
10
|
+
s.email = 'code@papercavalier.com'
|
11
|
+
s.homepage = 'https://rubygems.org/gems/bookland'
|
12
|
+
s.summary = %q{An ISBN class in Ruby}
|
13
|
+
s.description = %q{Bookland is an ISBN class in Ruby.}
|
9
14
|
|
10
|
-
s.
|
11
|
-
s.authors = ["Hakan Ensari", "Piotr Laszewski"]
|
12
|
-
s.date = %q{2010-09-09}
|
13
|
-
s.description = %q{A simple ISBN class in Ruby}
|
14
|
-
s.email = %q{code@papercavalier.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE",
|
17
|
-
"README.md"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
".gitignore",
|
21
|
-
".rspec",
|
22
|
-
".rvmrc",
|
23
|
-
"Gemfile",
|
24
|
-
"Gemfile.lock",
|
25
|
-
"History.md",
|
26
|
-
"LICENSE",
|
27
|
-
"README.md",
|
28
|
-
"Rakefile",
|
29
|
-
"VERSION",
|
30
|
-
"bookland.gemspec",
|
31
|
-
"lib/bookland.rb",
|
32
|
-
"spec/bookland_spec.rb",
|
33
|
-
"spec/fixtures/isbn",
|
34
|
-
"spec/spec_helper.rb"
|
35
|
-
]
|
36
|
-
s.homepage = %q{http://github.com/papercavalier/bookland}
|
37
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
-
s.require_paths = ["lib"]
|
39
|
-
s.rubygems_version = %q{1.3.7}
|
40
|
-
s.summary = %q{A simple ISBN class in Ruby}
|
41
|
-
s.test_files = [
|
42
|
-
"spec/bookland_spec.rb",
|
43
|
-
"spec/spec_helper.rb"
|
44
|
-
]
|
15
|
+
s.rubyforge_project = 'bookland'
|
45
16
|
|
46
|
-
|
47
|
-
|
48
|
-
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ['lib']
|
49
21
|
|
50
|
-
|
51
|
-
else
|
52
|
-
end
|
53
|
-
else
|
54
|
-
end
|
22
|
+
s.add_development_dependency('rspec', '~> 2.5.0')
|
55
23
|
end
|
56
|
-
|
data/lib/bookland.rb
CHANGED
@@ -1,105 +1 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# A simple ISBN class
|
4
|
-
class ISBN
|
5
|
-
class << self
|
6
|
-
def to_10(isbn)
|
7
|
-
new(isbn).to_isbn10.to_s
|
8
|
-
end
|
9
|
-
|
10
|
-
def to_13(isbn)
|
11
|
-
new(isbn).to_isbn13.to_s
|
12
|
-
end
|
13
|
-
|
14
|
-
def valid?(isbn)
|
15
|
-
new(isbn).valid?
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def initialize(seed=nil)
|
20
|
-
self.seed = seed
|
21
|
-
end
|
22
|
-
|
23
|
-
def ==(other)
|
24
|
-
to_isbn13.to_s == other.to_isbn13.to_s
|
25
|
-
end
|
26
|
-
|
27
|
-
def inspect
|
28
|
-
to_s
|
29
|
-
end
|
30
|
-
|
31
|
-
def seed=(seed)
|
32
|
-
@raw = seed.gsub(/[^Xx0-9]/, '').split(//) rescue @raw = []
|
33
|
-
end
|
34
|
-
|
35
|
-
def to_isbn10
|
36
|
-
raise ISBNError unless valid?
|
37
|
-
|
38
|
-
if isbn13?
|
39
|
-
raw = @raw[3..11]
|
40
|
-
ISBN.new((raw << check_digit_10(raw)).to_s)
|
41
|
-
else
|
42
|
-
dup
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def to_isbn13
|
47
|
-
raise ISBNError unless valid?
|
48
|
-
|
49
|
-
if isbn10?
|
50
|
-
raw = @raw[0..8].unshift('9', '7', '8')
|
51
|
-
ISBN.new((raw << check_digit_13(raw)).to_s)
|
52
|
-
else
|
53
|
-
dup
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def to_s(*blocks)
|
58
|
-
return false unless valid?
|
59
|
-
|
60
|
-
raw = @raw.dup
|
61
|
-
blocks.any? ? (blocks.map { |i| raw.shift(i).join } << raw.join).delete_if(&:empty?).join('-') : raw.join
|
62
|
-
end
|
63
|
-
|
64
|
-
def valid?
|
65
|
-
if isbn10?
|
66
|
-
@raw[9] == check_digit_10(@raw)
|
67
|
-
elsif isbn13?
|
68
|
-
@raw[12] == check_digit_13(@raw)
|
69
|
-
else
|
70
|
-
false
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
private
|
75
|
-
|
76
|
-
def check_digit_10(raw)
|
77
|
-
cd = 11 - 10.downto(2).to_a.zip(raw[0..8].map(&:to_i)).map { |n,m| n * m }.inject(0) { |i,j| i + j } % 11
|
78
|
-
|
79
|
-
case cd
|
80
|
-
when 0..9 then cd.to_s
|
81
|
-
when 10 then 'X'
|
82
|
-
when 11 then '0'
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def check_digit_13(raw)
|
87
|
-
((10 - ([1, 3] * 6).zip(raw[0..11].map(&:to_i)).map { |n,m| n * m }.inject(0) { |i,j| i + j } % 10) % 10).to_s
|
88
|
-
end
|
89
|
-
|
90
|
-
def isbn10?
|
91
|
-
@raw.length == 10
|
92
|
-
end
|
93
|
-
|
94
|
-
def isbn13?
|
95
|
-
@raw.length == 13
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
# A simple error wrapper for a simple ISBN class
|
100
|
-
class ISBNError < StandardError
|
101
|
-
def initialize(msg='ISBN not valid')
|
102
|
-
super
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
1
|
+
require 'bookland/isbn'
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module Bookland
|
2
|
+
|
3
|
+
# A simple ISBN class
|
4
|
+
class ISBN
|
5
|
+
class << self
|
6
|
+
def to_10(isbn)
|
7
|
+
new(isbn).to_isbn10.to_s
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_13(isbn)
|
11
|
+
new(isbn).to_isbn13.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
def valid?(isbn)
|
15
|
+
new(isbn).valid?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(seed=nil)
|
20
|
+
self.seed = seed
|
21
|
+
end
|
22
|
+
|
23
|
+
def ==(other)
|
24
|
+
to_isbn13.to_s == other.to_isbn13.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def inspect
|
28
|
+
to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
def seed=(seed)
|
32
|
+
@raw = seed.gsub(/[^Xx0-9]/, '').split(//) rescue @raw = []
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_isbn10
|
36
|
+
raise ISBNError unless valid?
|
37
|
+
|
38
|
+
if isbn13?
|
39
|
+
raw = @raw[3..11]
|
40
|
+
ISBN.new((raw << check_digit_10(raw)).to_s)
|
41
|
+
else
|
42
|
+
dup
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_isbn13
|
47
|
+
raise ISBNError unless valid?
|
48
|
+
|
49
|
+
if isbn10?
|
50
|
+
raw = @raw[0..8].unshift('9', '7', '8')
|
51
|
+
ISBN.new((raw << check_digit_13(raw)).to_s)
|
52
|
+
else
|
53
|
+
dup
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_s(*blocks)
|
58
|
+
return false unless valid?
|
59
|
+
|
60
|
+
raw = @raw.dup
|
61
|
+
blocks.any? ? (blocks.map { |i| raw.shift(i).join } << raw.join).delete_if(&:empty?).join('-') : raw.join
|
62
|
+
end
|
63
|
+
|
64
|
+
def valid?
|
65
|
+
if isbn10?
|
66
|
+
@raw[9] == check_digit_10(@raw)
|
67
|
+
elsif isbn13?
|
68
|
+
@raw[12] == check_digit_13(@raw)
|
69
|
+
else
|
70
|
+
false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def check_digit_10(raw)
|
77
|
+
cd = 11 - 10.downto(2).to_a.zip(raw[0..8].map(&:to_i)).map { |n,m| n * m }.inject(0) { |i,j| i + j } % 11
|
78
|
+
|
79
|
+
case cd
|
80
|
+
when 0..9 then cd.to_s
|
81
|
+
when 10 then 'X'
|
82
|
+
when 11 then '0'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def check_digit_13(raw)
|
87
|
+
((10 - ([1, 3] * 6).zip(raw[0..11].map(&:to_i)).map { |n,m| n * m }.inject(0) { |i,j| i + j } % 10) % 10).to_s
|
88
|
+
end
|
89
|
+
|
90
|
+
def isbn10?
|
91
|
+
@raw.length == 10
|
92
|
+
end
|
93
|
+
|
94
|
+
def isbn13?
|
95
|
+
@raw.length == 13
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class ISBNError < StandardError
|
100
|
+
def initialize(msg='Invalid ISBN')
|
101
|
+
super
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module Bookland
|
4
|
+
describe Bookland::ISBN do
|
5
|
+
context ".to_10" do
|
6
|
+
it "converts to ISBN-10" do
|
7
|
+
isbns do |isbn10, isbn13|
|
8
|
+
Bookland::ISBN.to_10(isbn10).should eql isbn10
|
9
|
+
Bookland::ISBN.to_10(isbn13).should eql isbn10
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "raises an ISBNError if an invalid ISBN is converted" do
|
14
|
+
['foo', 1, '', nil].each do |isbn|
|
15
|
+
expect do
|
16
|
+
Bookland::ISBN.to_10(isbn)
|
17
|
+
end.to raise_error Bookland::ISBNError
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context ".to_13" do
|
23
|
+
it "converts to ISBN-13" do
|
24
|
+
isbns do |isbn10, isbn13|
|
25
|
+
Bookland::ISBN.to_13(isbn10).should eql isbn13
|
26
|
+
Bookland::ISBN.to_13(isbn13).should eql isbn13
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "raises an ISBNError if an invalid ISBN is converted" do
|
31
|
+
['foo', 1, '', nil].each do |isbn|
|
32
|
+
expect do
|
33
|
+
Bookland::ISBN.to_13(isbn)
|
34
|
+
end.to raise_error Bookland::ISBNError
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context ".valid?" do
|
40
|
+
it "validates an ISBN" do
|
41
|
+
Bookland::ISBN.valid?("9780485113358").should be_true
|
42
|
+
Bookland::ISBN.valid?("9780485113359").should be_false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "#to_isbn13" do
|
47
|
+
it "converts an ISBN-10 to ISBN-13" do
|
48
|
+
isbns do |isbn10, isbn13|
|
49
|
+
Bookland::ISBN.new(isbn10).to_isbn13.should == Bookland::ISBN.new(isbn13)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it "raises an ISBNError if an invalid ISBN-10 is converted to ISBN-13" do
|
54
|
+
isbns do |isbn10, isbn13|
|
55
|
+
invalid = isbn10.gsub(/(.)$/, "#{(isbn10.split(//).last.to_i - 2) % 10}")
|
56
|
+
expect do
|
57
|
+
Bookland::ISBN.new(invalid).to_isbn13
|
58
|
+
end.to raise_error Bookland::ISBNError
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "#to_isbn10" do
|
64
|
+
it "converts an ISBN-13 to ISBN-10" do
|
65
|
+
isbns do |isbn10, isbn13|
|
66
|
+
Bookland::ISBN.new(isbn13).to_isbn10.should == Bookland::ISBN.new(isbn10)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it "raises an ISBNError if an invalid ISBN-13 is converted to ISBN-10" do
|
71
|
+
isbns do |isbn10, isbn13|
|
72
|
+
invalid = isbn13.gsub(/(.)$/, "#{(isbn13.split(//).last.to_i - 2) % 10}")
|
73
|
+
expect do
|
74
|
+
Bookland::ISBN.new(invalid).to_isbn10
|
75
|
+
end.to raise_error Bookland::ISBNError
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "#==" do
|
81
|
+
it "equates ISBN-10 and ISBN-13" do
|
82
|
+
isbns do |isbn10, isbn13|
|
83
|
+
(Bookland::ISBN.new(isbn10) == Bookland::ISBN.new(isbn13)).should be_true
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "#valid?" do
|
89
|
+
it "validates ISBN-10s" do
|
90
|
+
isbns do |isbn10, isbn13|
|
91
|
+
Bookland::ISBN.new(isbn10).valid?.should be_true
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
it "validates ISBN-13s" do
|
96
|
+
isbns do |isbn10, isbn13|
|
97
|
+
Bookland::ISBN.new(isbn13).valid?.should be_true
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
it "does not validate if seed looks like an ISBN-13 but has an invalid check digit" do
|
102
|
+
isbns do |isbn10, isbn13|
|
103
|
+
invalid = isbn13.gsub(/(.)$/, "#{(isbn13.split(//).last.to_i - 2) % 10}")
|
104
|
+
Bookland::ISBN.new(invalid).valid?.should be_false
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
it "does not validate if seed looks like an ISBN-10 but has an invalid check digit" do
|
109
|
+
isbns do |isbn10, isbn13|
|
110
|
+
invalid = isbn10.gsub(/(.)$/, "#{(isbn10.split(//).last.to_i - 2) % 10}")
|
111
|
+
Bookland::ISBN.new(invalid).valid?.should be_false
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it "does not validate if seed is not an ISBN" do
|
116
|
+
['foo', 1, '', nil].each { |seed| Bookland::ISBN.new(seed).valid?.should be_false }
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "#to_s" do
|
121
|
+
it "casts as string" do
|
122
|
+
Bookland::ISBN.new('0262011530').to_s.should eql '0262011530'
|
123
|
+
end
|
124
|
+
|
125
|
+
it "casts as string and hyphenate an ISBN-13" do
|
126
|
+
Bookland::ISBN.new("9780485113358").to_s(3, 10).should eql '978-0485113358'
|
127
|
+
end
|
128
|
+
|
129
|
+
it "casts as string and hyphenate an ISBN-10" do
|
130
|
+
Bookland::ISBN.new("048511335X").to_s(1, 3, 5, 1).should eql '0-485-11335-X'
|
131
|
+
end
|
132
|
+
|
133
|
+
it "returns false if an invalid ISBN is cast as string" do
|
134
|
+
['foo', 1, '', nil].each { |seed| Bookland::ISBN.new(seed).to_s.should be_false }
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
metadata
CHANGED
@@ -1,34 +1,47 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bookland
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
|
-
- 0
|
8
|
-
- 3
|
9
7
|
- 1
|
10
|
-
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
|
-
-
|
14
|
-
- Piotr Laszewski
|
13
|
+
- Paper Cavalier
|
15
14
|
autorequire:
|
16
15
|
bindir: bin
|
17
16
|
cert_chain: []
|
18
17
|
|
19
|
-
date:
|
18
|
+
date: 2011-03-03 00:00:00 +00:00
|
20
19
|
default_executable:
|
21
|
-
dependencies:
|
22
|
-
|
23
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 5
|
33
|
+
- 0
|
34
|
+
version: 2.5.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Bookland is an ISBN class in Ruby.
|
24
38
|
email: code@papercavalier.com
|
25
39
|
executables: []
|
26
40
|
|
27
41
|
extensions: []
|
28
42
|
|
29
|
-
extra_rdoc_files:
|
30
|
-
|
31
|
-
- README.md
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
32
45
|
files:
|
33
46
|
- .gitignore
|
34
47
|
- .rspec
|
@@ -42,16 +55,18 @@ files:
|
|
42
55
|
- VERSION
|
43
56
|
- bookland.gemspec
|
44
57
|
- lib/bookland.rb
|
45
|
-
-
|
58
|
+
- lib/bookland/isbn.rb
|
59
|
+
- lib/bookland/version.rb
|
60
|
+
- spec/bookland/isbn_spec.rb
|
46
61
|
- spec/fixtures/isbn
|
47
62
|
- spec/spec_helper.rb
|
48
63
|
has_rdoc: true
|
49
|
-
homepage:
|
64
|
+
homepage: https://rubygems.org/gems/bookland
|
50
65
|
licenses: []
|
51
66
|
|
52
67
|
post_install_message:
|
53
|
-
rdoc_options:
|
54
|
-
|
68
|
+
rdoc_options: []
|
69
|
+
|
55
70
|
require_paths:
|
56
71
|
- lib
|
57
72
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -74,11 +89,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
89
|
version: "0"
|
75
90
|
requirements: []
|
76
91
|
|
77
|
-
rubyforge_project:
|
78
|
-
rubygems_version: 1.
|
92
|
+
rubyforge_project: bookland
|
93
|
+
rubygems_version: 1.5.2
|
79
94
|
signing_key:
|
80
95
|
specification_version: 3
|
81
|
-
summary:
|
96
|
+
summary: An ISBN class in Ruby
|
82
97
|
test_files:
|
83
|
-
- spec/
|
98
|
+
- spec/bookland/isbn_spec.rb
|
99
|
+
- spec/fixtures/isbn
|
84
100
|
- spec/spec_helper.rb
|
data/spec/bookland_spec.rb
DELETED
@@ -1,111 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
module Bookland
|
4
|
-
describe Bookland::ISBN do
|
5
|
-
context "class methods" do
|
6
|
-
it "converts to ISBN-10" do
|
7
|
-
isbns do |isbn10, isbn13|
|
8
|
-
Bookland::ISBN.to_10(isbn10).should eql isbn10
|
9
|
-
Bookland::ISBN.to_10(isbn13).should eql isbn10
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
it "converts to ISBN-13" do
|
14
|
-
isbns do |isbn10, isbn13|
|
15
|
-
Bookland::ISBN.to_13(isbn10).should eql isbn13
|
16
|
-
Bookland::ISBN.to_13(isbn13).should eql isbn13
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
it "raises an ISBNError if an invalid ISBN is converted" do
|
21
|
-
['foo', 1, '', nil].each do |isbn|
|
22
|
-
lambda { Bookland::ISBN.to_10(isbn)}.should raise_error Bookland::ISBNError
|
23
|
-
lambda { Bookland::ISBN.to_13(isbn)}.should raise_error Bookland::ISBNError
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
it "validates an ISBN" do
|
28
|
-
Bookland::ISBN.valid?("9780485113358").should be_true
|
29
|
-
Bookland::ISBN.valid?("9780485113359").should be_false
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
it "converts an ISBN-10 to ISBN-13" do
|
34
|
-
isbns do |isbn10, isbn13|
|
35
|
-
Bookland::ISBN.new(isbn10).to_isbn13.should == Bookland::ISBN.new(isbn13)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
it "converts an ISBN-13 to ISBN-10" do
|
40
|
-
isbns do |isbn10, isbn13|
|
41
|
-
Bookland::ISBN.new(isbn13).to_isbn10.should == Bookland::ISBN.new(isbn10)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
it "equates ISBN-10 and ISBN-13" do
|
46
|
-
isbns do |isbn10, isbn13|
|
47
|
-
(Bookland::ISBN.new(isbn10) == Bookland::ISBN.new(isbn13)).should be_true
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
it "validates ISBN-10s" do
|
52
|
-
isbns do |isbn10, isbn13|
|
53
|
-
Bookland::ISBN.new(isbn10).valid?.should be_true
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
it "validates ISBN-13s" do
|
58
|
-
isbns do |isbn10, isbn13|
|
59
|
-
Bookland::ISBN.new(isbn13).valid?.should be_true
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
it "does not validate if seed looks like an ISBN-13 but has an invalid check digit" do
|
64
|
-
isbns do |isbn10, isbn13|
|
65
|
-
invalid = isbn13.gsub(/(.)$/, "#{(isbn13.split(//).last.to_i - 2) % 10}")
|
66
|
-
Bookland::ISBN.new(invalid).valid?.should be_false
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
it "does not validate if seed looks like an ISBN-10 but has an invalid check digit" do
|
71
|
-
isbns do |isbn10, isbn13|
|
72
|
-
invalid = isbn10.gsub(/(.)$/, "#{(isbn10.split(//).last.to_i - 2) % 10}")
|
73
|
-
Bookland::ISBN.new(invalid).valid?.should be_false
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
it "does not validate if seed is not an ISBN" do
|
78
|
-
['foo', 1, '', nil].each { |seed| Bookland::ISBN.new(seed).valid?.should be_false }
|
79
|
-
end
|
80
|
-
|
81
|
-
it "casts as string" do
|
82
|
-
Bookland::ISBN.new('0262011530').to_s.should eql '0262011530'
|
83
|
-
end
|
84
|
-
|
85
|
-
it "casts as string and hyphenate an ISBN-13" do
|
86
|
-
Bookland::ISBN.new("9780485113358").to_s(3, 10).should eql '978-0485113358'
|
87
|
-
end
|
88
|
-
|
89
|
-
it "casts as string and hyphenate an ISBN-10" do
|
90
|
-
Bookland::ISBN.new("048511335X").to_s(1, 3, 5, 1).should eql '0-485-11335-X'
|
91
|
-
end
|
92
|
-
|
93
|
-
it "returns false if an invalid ISBN is cast as string" do
|
94
|
-
['foo', 1, '', nil].each { |seed| Bookland::ISBN.new(seed).to_s.should be_false }
|
95
|
-
end
|
96
|
-
|
97
|
-
it "raises an ISBNError if an invalid ISBN-13 is converted to ISBN-10" do
|
98
|
-
isbns do |isbn10, isbn13|
|
99
|
-
invalid = isbn13.gsub(/(.)$/, "#{(isbn13.split(//).last.to_i - 2) % 10}")
|
100
|
-
lambda { Bookland::ISBN.new(invalid).to_isbn10 }.should raise_error Bookland::ISBNError
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
it "raises an ISBNError if an invalid ISBN-10 is converted to ISBN-13" do
|
105
|
-
isbns do |isbn10, isbn13|
|
106
|
-
invalid = isbn10.gsub(/(.)$/, "#{(isbn10.split(//).last.to_i - 2) % 10}")
|
107
|
-
lambda { Bookland::ISBN.new(invalid).to_isbn13 }.should raise_error Bookland::ISBNError
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|