bookland 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README.md +9 -10
- data/Rakefile +12 -0
- data/bookland.gemspec +1 -0
- data/lib/bookland.rb +4 -0
- data/lib/bookland/isbn.rb +44 -2
- data/lib/bookland/version.rb +1 -1
- data/spec/bookland/isbn_spec.rb +83 -56
- data/spec/bookland_spec.rb +7 -0
- metadata +22 -6
- data/Gemfile.lock +0 -24
- data/VERSION +0 -1
data/README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
Bookland
|
2
2
|
========
|
3
3
|
|
4
|
-
[Bookland](http://en.wikipedia.org/wiki/Bookland) provides an ISBN class in
|
4
|
+
[Bookland](http://en.wikipedia.org/wiki/Bookland) provides an ISBN class in
|
5
|
+
Ruby.
|
5
6
|
|
6
|
-
|
7
|
-
|
7
|
+
Usage
|
8
|
+
-----
|
8
9
|
|
9
|
-
|
10
|
+
require 'bookland'
|
10
11
|
|
11
12
|
isbn10 = ISBN.new('0262011530')
|
12
13
|
isbn10.to_isbn13
|
@@ -18,17 +19,15 @@ Examples
|
|
18
19
|
isbn13 == ISBN.new('9780262011532')
|
19
20
|
=> true
|
20
21
|
|
21
|
-
#
|
22
|
-
|
23
|
-
|
22
|
+
# Does an invalid ISBN quack like an ISBN?
|
23
|
+
bad_isbn = ISBN.new('0262011531')
|
24
|
+
bad_isbn.valid?
|
24
25
|
=> false
|
25
|
-
|
26
|
+
bad_isbn.to_isbn13
|
26
27
|
=> Bookland::ISBNError: Invalid ISBN
|
27
28
|
|
28
29
|
Some utility methods defined in the class level:
|
29
30
|
|
30
|
-
include Bookland
|
31
|
-
|
32
31
|
ISBN.to_13('0262011530')
|
33
32
|
=> "9780262011532"
|
34
33
|
|
data/Rakefile
CHANGED
@@ -9,3 +9,15 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
9
9
|
end
|
10
10
|
|
11
11
|
task :default => :spec
|
12
|
+
|
13
|
+
require 'rocco/tasks'
|
14
|
+
Rocco::make 'docs/'
|
15
|
+
|
16
|
+
desc 'Build rocco docs'
|
17
|
+
task :docs => :rocco
|
18
|
+
directory 'docs/'
|
19
|
+
|
20
|
+
desc 'Build docs and open in browser for the reading'
|
21
|
+
task :read => :docs do
|
22
|
+
sh 'open docs/lib/bookland/isbn.html'
|
23
|
+
end
|
data/bookland.gemspec
CHANGED
data/lib/bookland.rb
CHANGED
data/lib/bookland/isbn.rb
CHANGED
@@ -1,16 +1,37 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# [Bookland][bl] is a simple ISBN class in Ruby.
|
4
|
+
#
|
5
|
+
# [bl]: http://en.wikipedia.org/wiki/Bookland
|
1
6
|
module Bookland
|
2
7
|
|
3
|
-
|
8
|
+
#### Public Interface
|
9
|
+
#
|
10
|
+
# `ISBN.new` takes an optional string. The string should look like an ISBN
|
11
|
+
# and may contain extra characters that are traditionally used to format
|
12
|
+
# ISBNs.
|
13
|
+
#
|
14
|
+
# The following are valid instantiations:
|
15
|
+
#
|
16
|
+
# isbn13 = ISBN.new('9780826476951')
|
17
|
+
# isbn10 = ISBN.new('0-8264-7695-3')
|
18
|
+
#
|
19
|
+
# isbn10 == isbn13
|
20
|
+
# => true
|
4
21
|
class ISBN
|
5
22
|
class << self
|
23
|
+
|
24
|
+
# Casts a specified string to a 10-digit ISBN.
|
6
25
|
def to_10(isbn)
|
7
26
|
new(isbn).to_isbn10.to_s
|
8
27
|
end
|
9
28
|
|
29
|
+
# Casts a specified string to a 13-digit ISBN.
|
10
30
|
def to_13(isbn)
|
11
31
|
new(isbn).to_isbn13.to_s
|
12
32
|
end
|
13
33
|
|
34
|
+
# Returns `true` if the specified string is a valid ISBN.
|
14
35
|
def valid?(isbn)
|
15
36
|
new(isbn).valid?
|
16
37
|
end
|
@@ -20,19 +41,24 @@ module Bookland
|
|
20
41
|
self.seed = seed
|
21
42
|
end
|
22
43
|
|
44
|
+
# Returns `true` if the ISBN is equal to another specified ISBN.
|
23
45
|
def ==(other)
|
24
46
|
to_isbn13.to_s == other.to_isbn13.to_s
|
25
47
|
end
|
26
48
|
|
49
|
+
# Inspecting an ISBN object will return its string representation.
|
27
50
|
def inspect
|
28
51
|
to_s
|
29
52
|
end
|
30
53
|
|
54
|
+
# Sets the seed of the ISBN based on a specified string.
|
31
55
|
def seed=(seed)
|
32
56
|
@raw = seed.gsub(/[^Xx0-9]/, '').split(//) rescue @raw = []
|
33
57
|
end
|
34
58
|
|
59
|
+
# Casts the ISBN to a ten-digit ISBN.
|
35
60
|
def to_isbn10
|
61
|
+
|
36
62
|
raise ISBNError unless valid?
|
37
63
|
|
38
64
|
if isbn13?
|
@@ -54,13 +80,27 @@ module Bookland
|
|
54
80
|
end
|
55
81
|
end
|
56
82
|
|
83
|
+
# Casts ISBN to a string.
|
84
|
+
#
|
85
|
+
# Takes an optional list of integers, which it then uses to dashify the
|
86
|
+
# ISBN like so:
|
87
|
+
#
|
88
|
+
# isbn = ISBN.new('0826476953')
|
89
|
+
# isbn.to_s(1,4,4,1)
|
90
|
+
# => "0-8264-7695-3"
|
91
|
+
#
|
57
92
|
def to_s(*blocks)
|
58
93
|
return false unless valid?
|
59
94
|
|
60
95
|
raw = @raw.dup
|
61
|
-
blocks.any?
|
96
|
+
if blocks.any?
|
97
|
+
(blocks.map { |i| raw.shift(i).join } << raw.join).delete_if(&:empty?).join('-')
|
98
|
+
else
|
99
|
+
raw.join
|
100
|
+
end
|
62
101
|
end
|
63
102
|
|
103
|
+
# Returns `true` if the ISBN is valid.
|
64
104
|
def valid?
|
65
105
|
if isbn10?
|
66
106
|
@raw[9] == check_digit_10(@raw)
|
@@ -102,3 +142,5 @@ module Bookland
|
|
102
142
|
end
|
103
143
|
end
|
104
144
|
end
|
145
|
+
|
146
|
+
# C'est ça.
|
data/lib/bookland/version.rb
CHANGED
data/spec/bookland/isbn_spec.rb
CHANGED
@@ -1,137 +1,164 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
module Bookland
|
4
|
-
describe
|
5
|
-
|
6
|
-
it "
|
4
|
+
describe ISBN do
|
5
|
+
describe ".to_10" do
|
6
|
+
it "casts a specified string to a ten-digit ISBN" do
|
7
7
|
isbns do |isbn10, isbn13|
|
8
|
-
|
9
|
-
|
8
|
+
ISBN.to_10(isbn10).should eql isbn10
|
9
|
+
ISBN.to_10(isbn13).should eql isbn10
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
it "raises an ISBNError if
|
13
|
+
it "raises an ISBNError if the specified string is not a valid ISBN" do
|
14
14
|
['foo', 1, '', nil].each do |isbn|
|
15
15
|
expect do
|
16
|
-
|
17
|
-
end.to raise_error
|
16
|
+
ISBN.to_10(isbn)
|
17
|
+
end.to raise_error ISBNError
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
|
23
|
-
it "
|
22
|
+
describe ".to_13" do
|
23
|
+
it "casts a specified string to a 13-digit ISBN" do
|
24
24
|
isbns do |isbn10, isbn13|
|
25
|
-
|
26
|
-
|
25
|
+
ISBN.to_13(isbn10).should eql isbn13
|
26
|
+
ISBN.to_13(isbn13).should eql isbn13
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
it "raises an ISBNError if
|
30
|
+
it "raises an ISBNError if the specified string is not a valid ISBN" do
|
31
31
|
['foo', 1, '', nil].each do |isbn|
|
32
32
|
expect do
|
33
|
-
|
34
|
-
end.to raise_error
|
33
|
+
ISBN.to_13(isbn)
|
34
|
+
end.to raise_error ISBNError
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
|
40
|
-
it "
|
41
|
-
|
42
|
-
|
39
|
+
describe ".valid?" do
|
40
|
+
it "returns true if a specified string is a valid ISBN" do
|
41
|
+
ISBN.valid?('9780485113358').should be_true
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns false if a specified string is not a valid ISBN" do
|
45
|
+
ISBN.valid?('9780485113359').should be_false
|
43
46
|
end
|
44
47
|
end
|
45
48
|
|
46
|
-
|
47
|
-
it "
|
49
|
+
describe "#to_isbn10" do
|
50
|
+
it "casts a 10-digit ISBN to a 10-digit ISBN" do
|
48
51
|
isbns do |isbn10, isbn13|
|
49
|
-
|
52
|
+
ISBN.new(isbn10).to_isbn10.should == ISBN.new(isbn10)
|
50
53
|
end
|
51
54
|
end
|
52
55
|
|
53
|
-
it "
|
56
|
+
it "casts a 13-digit ISBN to a 10-digit ISBN" do
|
54
57
|
isbns do |isbn10, isbn13|
|
55
|
-
|
58
|
+
ISBN.new(isbn13).to_isbn10.should == ISBN.new(isbn10)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it "raises an ISBNError if self is not a valid ISBN" do
|
63
|
+
isbns do |isbn10, isbn13|
|
64
|
+
invalid = isbn13.gsub(/(.)$/, "#{(isbn13.split(//).last.to_i - 2) % 10}")
|
56
65
|
expect do
|
57
|
-
|
58
|
-
end.to raise_error
|
66
|
+
ISBN.new(invalid).to_isbn10
|
67
|
+
end.to raise_error ISBNError
|
59
68
|
end
|
60
69
|
end
|
61
70
|
end
|
62
71
|
|
63
|
-
|
64
|
-
it "
|
72
|
+
describe "#to_isbn13" do
|
73
|
+
it "casts a 10-digit ISBN to a 13-digit ISBN" do
|
65
74
|
isbns do |isbn10, isbn13|
|
66
|
-
|
75
|
+
ISBN.new(isbn10).to_isbn13.should == ISBN.new(isbn13)
|
67
76
|
end
|
68
77
|
end
|
69
78
|
|
70
|
-
it "
|
79
|
+
it "casts a 13-digit ISBN to a 13-digit ISBN" do
|
71
80
|
isbns do |isbn10, isbn13|
|
72
|
-
|
81
|
+
ISBN.new(isbn13).to_isbn13.should == ISBN.new(isbn13)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
it "raises an ISBNError if self is not a valid ISBN" do
|
86
|
+
isbns do |isbn10, isbn13|
|
87
|
+
invalid = isbn10.gsub(/(.)$/, "#{(isbn10.split(//).last.to_i - 2) % 10}")
|
73
88
|
expect do
|
74
|
-
|
75
|
-
end.to raise_error
|
89
|
+
ISBN.new(invalid).to_isbn13
|
90
|
+
end.to raise_error ISBNError
|
76
91
|
end
|
77
92
|
end
|
78
93
|
end
|
79
94
|
|
80
|
-
|
81
|
-
it "equates
|
95
|
+
describe "#==" do
|
96
|
+
it "equates a ten- and a thirteen-digit ISBN" do
|
97
|
+
isbns do |isbn10, isbn13|
|
98
|
+
(ISBN.new(isbn10) == ISBN.new(isbn13)).should be_true
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
it "equates a ten- and a ten-digit ISBN" do
|
103
|
+
isbns do |isbn10, isbn13|
|
104
|
+
(ISBN.new(isbn10) == ISBN.new(isbn10)).should be_true
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
it "equates a thirteen- and a thirteen-digit ISBN" do
|
82
109
|
isbns do |isbn10, isbn13|
|
83
|
-
(
|
110
|
+
(ISBN.new(isbn13) == ISBN.new(isbn13)).should be_true
|
84
111
|
end
|
85
112
|
end
|
86
113
|
end
|
87
114
|
|
88
|
-
|
89
|
-
it "validates ISBN
|
115
|
+
describe "#valid?" do
|
116
|
+
it "validates a ten-digit ISBN" do
|
90
117
|
isbns do |isbn10, isbn13|
|
91
|
-
|
118
|
+
ISBN.new(isbn10).valid?.should be_true
|
92
119
|
end
|
93
120
|
end
|
94
121
|
|
95
|
-
it "validates ISBN
|
122
|
+
it "validates a thirteen-digit ISBN" do
|
96
123
|
isbns do |isbn10, isbn13|
|
97
|
-
|
124
|
+
ISBN.new(isbn13).valid?.should be_true
|
98
125
|
end
|
99
126
|
end
|
100
127
|
|
101
|
-
it "does not validate if
|
128
|
+
it "does not validate if the 13-digit seed has an invalid check digit" do
|
102
129
|
isbns do |isbn10, isbn13|
|
103
130
|
invalid = isbn13.gsub(/(.)$/, "#{(isbn13.split(//).last.to_i - 2) % 10}")
|
104
|
-
|
131
|
+
ISBN.new(invalid).valid?.should be_false
|
105
132
|
end
|
106
133
|
end
|
107
134
|
|
108
|
-
it "does not validate if
|
135
|
+
it "does not validate if the 10-digit seed has an invalid check digit" do
|
109
136
|
isbns do |isbn10, isbn13|
|
110
137
|
invalid = isbn10.gsub(/(.)$/, "#{(isbn10.split(//).last.to_i - 2) % 10}")
|
111
|
-
|
138
|
+
ISBN.new(invalid).valid?.should be_false
|
112
139
|
end
|
113
140
|
end
|
114
141
|
|
115
142
|
it "does not validate if seed is not an ISBN" do
|
116
|
-
['foo', 1, '', nil].each { |seed|
|
143
|
+
['foo', 1, '', nil].each { |seed| ISBN.new(seed).valid?.should be_false }
|
117
144
|
end
|
118
145
|
end
|
119
146
|
|
120
|
-
|
121
|
-
it "casts
|
122
|
-
|
147
|
+
describe "#to_s" do
|
148
|
+
it "casts to string" do
|
149
|
+
ISBN.new('0262011530').to_s.should eql '0262011530'
|
123
150
|
end
|
124
151
|
|
125
|
-
it "casts
|
126
|
-
|
152
|
+
it "casts to string and hyphenates a thirteen-digit ISBN" do
|
153
|
+
ISBN.new('9780485113358').to_s(3, 10).should eql '978-0485113358'
|
127
154
|
end
|
128
155
|
|
129
|
-
it "casts
|
130
|
-
|
156
|
+
it "casts to string and hyphenate a ten-digit ISBN" do
|
157
|
+
ISBN.new('048511335X').to_s(1, 3, 5, 1).should eql '0-485-11335-X'
|
131
158
|
end
|
132
159
|
|
133
|
-
it "returns false if
|
134
|
-
['foo', 1, '', nil].each { |seed|
|
160
|
+
it "returns false if ISBN is not valid" do
|
161
|
+
['foo', 1, '', nil].each { |seed| ISBN.new(seed).to_s.should be_false }
|
135
162
|
end
|
136
163
|
end
|
137
164
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bookland
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Paper Cavalier
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-17 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -34,6 +34,22 @@ dependencies:
|
|
34
34
|
version: 2.5.0
|
35
35
|
type: :development
|
36
36
|
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rocco
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 7
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
- 6
|
49
|
+
- 0
|
50
|
+
version: 0.6.0
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
37
53
|
description: Bookland is an ISBN class in Ruby.
|
38
54
|
email: code@papercavalier.com
|
39
55
|
executables: []
|
@@ -47,17 +63,16 @@ files:
|
|
47
63
|
- .rspec
|
48
64
|
- .rvmrc
|
49
65
|
- Gemfile
|
50
|
-
- Gemfile.lock
|
51
66
|
- History.md
|
52
67
|
- LICENSE
|
53
68
|
- README.md
|
54
69
|
- Rakefile
|
55
|
-
- VERSION
|
56
70
|
- bookland.gemspec
|
57
71
|
- lib/bookland.rb
|
58
72
|
- lib/bookland/isbn.rb
|
59
73
|
- lib/bookland/version.rb
|
60
74
|
- spec/bookland/isbn_spec.rb
|
75
|
+
- spec/bookland_spec.rb
|
61
76
|
- spec/fixtures/isbn
|
62
77
|
- spec/spec_helper.rb
|
63
78
|
has_rdoc: true
|
@@ -96,5 +111,6 @@ specification_version: 3
|
|
96
111
|
summary: An ISBN class in Ruby
|
97
112
|
test_files:
|
98
113
|
- spec/bookland/isbn_spec.rb
|
114
|
+
- spec/bookland_spec.rb
|
99
115
|
- spec/fixtures/isbn
|
100
116
|
- spec/spec_helper.rb
|
data/Gemfile.lock
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
bookland (1.0.0)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: http://rubygems.org/
|
8
|
-
specs:
|
9
|
-
diff-lcs (1.1.2)
|
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)
|
18
|
-
|
19
|
-
PLATFORMS
|
20
|
-
ruby
|
21
|
-
|
22
|
-
DEPENDENCIES
|
23
|
-
bookland!
|
24
|
-
rspec (~> 2.5.0)
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.3.1
|