bookland 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -1
- data/.rspec +1 -0
- data/.rvmrc +3 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +31 -0
- data/History.md +5 -2
- data/README.md +12 -4
- data/Rakefile +21 -13
- data/VERSION +1 -1
- data/bookland.gemspec +57 -0
- data/lib/bookland.rb +13 -3
- data/rake_rubies.sh +26 -0
- data/spec/bookland_spec.rb +52 -29
- data/spec/spec_helper.rb +7 -1
- metadata +15 -4
data/.gitignore
CHANGED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format=documentation
|
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
gemcutter (0.6.1)
|
6
|
+
git (1.2.5)
|
7
|
+
jeweler (1.4.0)
|
8
|
+
gemcutter (>= 0.1.0)
|
9
|
+
git (>= 1.2.5)
|
10
|
+
rubyforge (>= 2.0.0)
|
11
|
+
json_pure (1.4.6)
|
12
|
+
rake (0.8.7)
|
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)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
bundler (~> 1.0.0.rc.3)
|
29
|
+
jeweler
|
30
|
+
rake
|
31
|
+
rspec (~> 2.0.0.beta.19)
|
data/History.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# 0.1.0 / 2010-04-22
|
2
2
|
* First public release
|
3
3
|
|
4
|
-
# 0.2.0 /
|
4
|
+
# 0.2.0 / 2010-04-28
|
5
5
|
* Made valid? public
|
6
|
-
* Will raise error if invalid ISBN instance is cast as string or recast as ISBN
|
6
|
+
* Will raise error if invalid ISBN instance is cast as string or recast as ISBN
|
7
|
+
|
8
|
+
# 0.3.0 / 2010-08-09
|
9
|
+
* Added class methods to cast to ISBN-10 and ISBN-13
|
data/README.md
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
![ISBN](http://upload.wikimedia.org/wikipedia/commons/thumb/2/28/EAN-13-ISBN-13.svg/
|
1
|
+
![ISBN](http://upload.wikimedia.org/wikipedia/commons/thumb/2/28/EAN-13-ISBN-13.svg/540px-EAN-13-ISBN-13.svg.png)
|
2
2
|
|
3
3
|
Bookland provides a simple ISBN class in Ruby.
|
4
4
|
|
5
|
-
|
6
|
-
>> include Bookland
|
7
|
-
>> book = ISBN.new("0262011530")
|
5
|
+
>> book = Bookland::ISBN.new("0262011530")
|
8
6
|
>> book.to_isbn13
|
9
7
|
=> "9780262011532"
|
10
8
|
>> book.to_s(1, 3, 5)
|
@@ -12,6 +10,16 @@ Bookland provides a simple ISBN class in Ruby.
|
|
12
10
|
>> ISBN.new("9780262011532") == book
|
13
11
|
=> true
|
14
12
|
>> invalid_book = ISBN.new("0262011531") # This is an invalid ISBN
|
13
|
+
>> invalid_book.valid?
|
15
14
|
=> false
|
16
15
|
>> invalid_book.to_isbn13
|
17
16
|
=> Bookland::ISBNError: ISBN not valid
|
17
|
+
|
18
|
+
Conversely, use the class methods:
|
19
|
+
|
20
|
+
>> Bookland::ISBN.to_13("0262011530")
|
21
|
+
=> "9780262011532"
|
22
|
+
>> Bookland::ISBN.to_10("9780262011532")
|
23
|
+
=> "0262011530"
|
24
|
+
|
25
|
+
Specs pass against Ruby 1.8.7, REE, and Ruby 1.9.1.
|
data/Rakefile
CHANGED
@@ -1,14 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
gemspec.authors = ["Hakan Ensari", "Piotr Laszewski"]
|
10
|
-
end
|
11
|
-
Jeweler::GemcutterTasks.new
|
12
|
-
rescue LoadError
|
13
|
-
puts "Jeweler not available. Install it with: gem install jeweler"
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
require "jeweler"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
desc "Run all specs in spec directory"
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
8
|
+
spec.pattern = "spec/**/*_spec.rb"
|
14
9
|
end
|
10
|
+
|
11
|
+
Jeweler::Tasks.new do |gemspec|
|
12
|
+
gemspec.name = "bookland"
|
13
|
+
gemspec.summary = "A simple ISBN class in Ruby"
|
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"]
|
18
|
+
end
|
19
|
+
|
20
|
+
Jeweler::GemcutterTasks.new
|
21
|
+
|
22
|
+
task :default => :spec
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/bookland.gemspec
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{bookland}
|
8
|
+
s.version = "0.3.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Hakan Ensari", "Piotr Laszewski"]
|
12
|
+
s.date = %q{2010-08-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
|
+
"rake_rubies.sh",
|
33
|
+
"spec/bookland_spec.rb",
|
34
|
+
"spec/fixtures/isbn",
|
35
|
+
"spec/spec_helper.rb"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/papercavalier/bookland}
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.3.7}
|
41
|
+
s.summary = %q{A simple ISBN class in Ruby}
|
42
|
+
s.test_files = [
|
43
|
+
"spec/bookland_spec.rb",
|
44
|
+
"spec/spec_helper.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
else
|
53
|
+
end
|
54
|
+
else
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
data/lib/bookland.rb
CHANGED
@@ -2,14 +2,24 @@ module Bookland
|
|
2
2
|
|
3
3
|
# A simple ISBN class
|
4
4
|
class ISBN
|
5
|
-
|
6
|
-
|
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
|
7
13
|
end
|
8
14
|
|
9
15
|
def initialize(seed=nil)
|
10
16
|
self.seed = seed
|
11
17
|
end
|
12
18
|
|
19
|
+
def ==(other)
|
20
|
+
self.to_isbn13.to_s == other.to_isbn13.to_s
|
21
|
+
end
|
22
|
+
|
13
23
|
def inspect
|
14
24
|
to_s
|
15
25
|
end
|
@@ -44,7 +54,7 @@ module Bookland
|
|
44
54
|
return false unless valid?
|
45
55
|
|
46
56
|
raw = @raw.dup
|
47
|
-
blocks.any? ? (blocks.map { |i| raw.shift(i).
|
57
|
+
blocks.any? ? (blocks.map { |i| raw.shift(i).join } << raw.join).delete_if(&:empty?).join('-') : raw.join
|
48
58
|
end
|
49
59
|
|
50
60
|
def valid?
|
data/rake_rubies.sh
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
# http://github.com/papercavalier/rake_rubies
|
4
|
+
|
5
|
+
if which rvm > /dev/null; then
|
6
|
+
RUBIES=$(echo `rvm list strings` | tr " " "\n")
|
7
|
+
if [ -e ".rvmrc" ]; then
|
8
|
+
GEMSET=`grep -m 1 -o -e "\@[^ ]*" .rvmrc`
|
9
|
+
fi
|
10
|
+
for RUBY in $RUBIES; do
|
11
|
+
if [ $RUBY != "default" ]; then
|
12
|
+
if [ -e "Gemfile" ]; then
|
13
|
+
if !(which bundle > /dev/null); then
|
14
|
+
rvm "$RUBY@global" gem install bundler --pre
|
15
|
+
fi
|
16
|
+
rvm "$RUBY$GEMSET" ruby -S bundle install > /dev/null
|
17
|
+
fi
|
18
|
+
rvm "$RUBY$GEMSET" rake $1
|
19
|
+
fi
|
20
|
+
done
|
21
|
+
if [ -e ".rvmrc" ]; then
|
22
|
+
sh .rvmrc > /dev/null
|
23
|
+
fi
|
24
|
+
else
|
25
|
+
rake $1
|
26
|
+
fi
|
data/spec/bookland_spec.rb
CHANGED
@@ -1,82 +1,105 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
module Bookland
|
4
|
-
describe ISBN do
|
5
|
-
|
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
|
+
end
|
27
|
+
|
28
|
+
it "converts an ISBN-10 to ISBN-13" do
|
6
29
|
isbns do |isbn10, isbn13|
|
7
|
-
ISBN.new(isbn10).to_isbn13.should == ISBN.new(isbn13)
|
30
|
+
Bookland::ISBN.new(isbn10).to_isbn13.should == Bookland::ISBN.new(isbn13)
|
8
31
|
end
|
9
32
|
end
|
10
33
|
|
11
|
-
it "
|
34
|
+
it "converts an ISBN-13 to ISBN-10" do
|
12
35
|
isbns do |isbn10, isbn13|
|
13
|
-
ISBN.new(isbn13).to_isbn10.should == ISBN.new(isbn10)
|
36
|
+
Bookland::ISBN.new(isbn13).to_isbn10.should == Bookland::ISBN.new(isbn10)
|
14
37
|
end
|
15
38
|
end
|
16
39
|
|
17
|
-
it "
|
40
|
+
it "equates ISBN-10 and ISBN-13" do
|
18
41
|
isbns do |isbn10, isbn13|
|
19
|
-
ISBN.new(isbn10)
|
42
|
+
(Bookland::ISBN.new(isbn10) == Bookland::ISBN.new(isbn13)).should be_true
|
20
43
|
end
|
21
44
|
end
|
22
45
|
|
23
|
-
it "
|
46
|
+
it "validates ISBN-10s" do
|
24
47
|
isbns do |isbn10, isbn13|
|
25
|
-
ISBN.new(isbn10).valid?.should be_true
|
48
|
+
Bookland::ISBN.new(isbn10).valid?.should be_true
|
26
49
|
end
|
27
50
|
end
|
28
51
|
|
29
|
-
it "
|
52
|
+
it "validates ISBN-13s" do
|
30
53
|
isbns do |isbn10, isbn13|
|
31
|
-
ISBN.new(isbn13).valid?.should be_true
|
54
|
+
Bookland::ISBN.new(isbn13).valid?.should be_true
|
32
55
|
end
|
33
56
|
end
|
34
57
|
|
35
|
-
it "
|
58
|
+
it "does not validate if seed looks like an ISBN-13 but has an invalid check digit" do
|
36
59
|
isbns do |isbn10, isbn13|
|
37
60
|
invalid = isbn13.gsub(/(.)$/, "#{(isbn13.split(//).last.to_i - 2) % 10}")
|
38
|
-
ISBN.new(invalid).valid?.should be_false
|
61
|
+
Bookland::ISBN.new(invalid).valid?.should be_false
|
39
62
|
end
|
40
63
|
end
|
41
64
|
|
42
|
-
it "
|
65
|
+
it "does not validate if seed looks like an ISBN-10 but has an invalid check digit" do
|
43
66
|
isbns do |isbn10, isbn13|
|
44
67
|
invalid = isbn10.gsub(/(.)$/, "#{(isbn10.split(//).last.to_i - 2) % 10}")
|
45
|
-
ISBN.new(invalid).valid?.should be_false
|
68
|
+
Bookland::ISBN.new(invalid).valid?.should be_false
|
46
69
|
end
|
47
70
|
end
|
48
71
|
|
49
|
-
it "
|
50
|
-
['foo', 1, '', nil].each { |seed| ISBN.new(seed).valid?.should be_false }
|
72
|
+
it "does not validate if seed is not an ISBN" do
|
73
|
+
['foo', 1, '', nil].each { |seed| Bookland::ISBN.new(seed).valid?.should be_false }
|
51
74
|
end
|
52
75
|
|
53
|
-
it "
|
54
|
-
Bookland::ISBN.new('0262011530').to_s.should
|
76
|
+
it "casts as string" do
|
77
|
+
Bookland::ISBN.new('0262011530').to_s.should eql '0262011530'
|
55
78
|
end
|
56
79
|
|
57
|
-
it "
|
58
|
-
ISBN.new("9780485113358").to_s(3, 10).should
|
80
|
+
it "casts as string and hyphenate an ISBN-13" do
|
81
|
+
Bookland::ISBN.new("9780485113358").to_s(3, 10).should eql '978-0485113358'
|
59
82
|
end
|
60
83
|
|
61
|
-
it "
|
62
|
-
ISBN.new("048511335X").to_s(1, 3, 5, 1).should
|
84
|
+
it "casts as string and hyphenate an ISBN-10" do
|
85
|
+
Bookland::ISBN.new("048511335X").to_s(1, 3, 5, 1).should eql '0-485-11335-X'
|
63
86
|
end
|
64
87
|
|
65
|
-
it "
|
66
|
-
['foo', 1, '', nil].each { |seed| ISBN.new(seed).to_s.should be_false }
|
88
|
+
it "returns false if an invalid ISBN is cast as string" do
|
89
|
+
['foo', 1, '', nil].each { |seed| Bookland::ISBN.new(seed).to_s.should be_false }
|
67
90
|
end
|
68
91
|
|
69
|
-
it "
|
92
|
+
it "raises an ISBNError if an invalid ISBN-13 is converted to ISBN-10" do
|
70
93
|
isbns do |isbn10, isbn13|
|
71
94
|
invalid = isbn13.gsub(/(.)$/, "#{(isbn13.split(//).last.to_i - 2) % 10}")
|
72
|
-
lambda { ISBN.new(invalid).to_isbn10 }.should raise_error ISBNError
|
95
|
+
lambda { Bookland::ISBN.new(invalid).to_isbn10 }.should raise_error Bookland::ISBNError
|
73
96
|
end
|
74
97
|
end
|
75
98
|
|
76
|
-
it "
|
99
|
+
it "raises an ISBNError if an invalid ISBN-10 is converted to ISBN-13" do
|
77
100
|
isbns do |isbn10, isbn13|
|
78
101
|
invalid = isbn10.gsub(/(.)$/, "#{(isbn10.split(//).last.to_i - 2) % 10}")
|
79
|
-
lambda { ISBN.new(invalid).to_isbn13 }.should raise_error ISBNError
|
102
|
+
lambda { Bookland::ISBN.new(invalid).to_isbn13 }.should raise_error Bookland::ISBNError
|
80
103
|
end
|
81
104
|
end
|
82
105
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
|
-
|
1
|
+
unless defined?(Bundler)
|
2
|
+
require "rubygems"
|
3
|
+
require "bundler/setup"
|
4
|
+
end
|
2
5
|
|
6
|
+
require "rspec"
|
3
7
|
require "yaml"
|
4
8
|
|
9
|
+
require File.expand_path("../../lib/bookland", __FILE__)
|
10
|
+
|
5
11
|
def isbns
|
6
12
|
File.open(File.expand_path("../fixtures/isbn", __FILE__)).each do |line|
|
7
13
|
yield line.split
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bookland
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
+
- 3
|
8
9
|
- 0
|
9
|
-
version: 0.
|
10
|
+
version: 0.3.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Hakan Ensari
|
@@ -15,7 +16,7 @@ autorequire:
|
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2010-
|
19
|
+
date: 2010-08-09 00:00:00 +01:00
|
19
20
|
default_executable:
|
20
21
|
dependencies: []
|
21
22
|
|
@@ -30,12 +31,18 @@ extra_rdoc_files:
|
|
30
31
|
- README.md
|
31
32
|
files:
|
32
33
|
- .gitignore
|
34
|
+
- .rspec
|
35
|
+
- .rvmrc
|
36
|
+
- Gemfile
|
37
|
+
- Gemfile.lock
|
33
38
|
- History.md
|
34
39
|
- LICENSE
|
35
40
|
- README.md
|
36
41
|
- Rakefile
|
37
42
|
- VERSION
|
43
|
+
- bookland.gemspec
|
38
44
|
- lib/bookland.rb
|
45
|
+
- rake_rubies.sh
|
39
46
|
- spec/bookland_spec.rb
|
40
47
|
- spec/fixtures/isbn
|
41
48
|
- spec/spec_helper.rb
|
@@ -49,23 +56,27 @@ rdoc_options:
|
|
49
56
|
require_paths:
|
50
57
|
- lib
|
51
58
|
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
52
60
|
requirements:
|
53
61
|
- - ">="
|
54
62
|
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
55
64
|
segments:
|
56
65
|
- 0
|
57
66
|
version: "0"
|
58
67
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
59
69
|
requirements:
|
60
70
|
- - ">="
|
61
71
|
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
62
73
|
segments:
|
63
74
|
- 0
|
64
75
|
version: "0"
|
65
76
|
requirements: []
|
66
77
|
|
67
78
|
rubyforge_project:
|
68
|
-
rubygems_version: 1.3.
|
79
|
+
rubygems_version: 1.3.7
|
69
80
|
signing_key:
|
70
81
|
specification_version: 3
|
71
82
|
summary: A simple ISBN class in Ruby
|