bookland 0.1.0 → 0.2.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/.gitignore CHANGED
@@ -1,3 +1,3 @@
1
- .DS_Store
2
- pkg
3
-
1
+ .DS_Store
2
+ pkg
3
+
@@ -0,0 +1,6 @@
1
+ # 0.1.0 / 2010-04-22
2
+ * First public release
3
+
4
+ # 0.2.0 / 2009-04-28
5
+ * Made valid? public
6
+ * Will raise error if invalid ISBN instance is cast as string or recast as ISBN
data/README.md CHANGED
@@ -6,10 +6,12 @@ Bookland provides a simple ISBN class in Ruby.
6
6
  >> include Bookland
7
7
  >> book = ISBN.new("0262011530")
8
8
  >> book.to_isbn13
9
- => 9780262011532
10
- >> book.to_s(1,3,5)
9
+ => "9780262011532"
10
+ >> book.to_s(1, 3, 5)
11
11
  => "0-262-01153-0"
12
12
  >> ISBN.new("9780262011532") == book
13
13
  => true
14
- >> ISBN.new("0262011531") # This is an invalid ISBN
15
- Bookland::ISBNError: ISBN not valid...
14
+ >> invalid_book = ISBN.new("0262011531") # This is an invalid ISBN
15
+ => false
16
+ >> invalid_book.to_isbn13
17
+ => Bookland::ISBNError: ISBN not valid
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -1,45 +1,67 @@
1
1
  module Bookland
2
+
3
+ # A simple ISBN class
2
4
  class ISBN
3
- def initialize(seed)
4
- @raw = seed.gsub(/[^Xx0-9]/, '').split(//) rescue @raw = []
5
- raise ISBNError unless valid?
5
+ def ==(other)
6
+ self.to_isbn13.to_s == other.to_isbn13.to_s
6
7
  end
7
-
8
+
9
+ def initialize(seed=nil)
10
+ self.seed = seed
11
+ end
12
+
8
13
  def inspect
9
14
  to_s
10
15
  end
11
16
 
17
+ def seed=(seed)
18
+ @raw = seed.gsub(/[^Xx0-9]/, '').split(//) rescue @raw = []
19
+ end
20
+
12
21
  def to_isbn10
22
+ raise ISBNError unless valid?
23
+
13
24
  if isbn13?
14
25
  raw = @raw[3..11]
15
- ISBN.new((raw << cd10(raw)).to_s)
26
+ ISBN.new((raw << check_digit_10(raw)).to_s)
16
27
  else
17
28
  self.dup
18
29
  end
19
30
  end
20
31
 
21
32
  def to_isbn13
33
+ raise ISBNError unless valid?
34
+
22
35
  if isbn10?
23
36
  raw = @raw[0..8].unshift('9', '7', '8')
24
- ISBN.new((raw << cd13(raw)).to_s)
37
+ ISBN.new((raw << check_digit_13(raw)).to_s)
25
38
  else
26
39
  self.dup
27
40
  end
28
41
  end
29
42
 
30
- def ==(other)
31
- self.to_isbn13.to_s == other.to_isbn13.to_s
32
- end
33
-
34
43
  def to_s(*blocks)
44
+ return false unless valid?
45
+
35
46
  raw = @raw.dup
36
47
  blocks.any? ? (blocks.map { |i| raw.shift(i).to_s } << raw.to_s).delete_if(&:empty?).join('-') : raw.to_s
37
48
  end
38
49
 
50
+ def valid?
51
+ if isbn10?
52
+ @raw[9] == check_digit_10(@raw)
53
+ elsif isbn13?
54
+ @raw[12] == check_digit_13(@raw)
55
+ else
56
+ false
57
+ end
58
+ end
59
+
39
60
  private
40
61
 
41
- def cd10(raw)
42
- cd = 11 - [10, 9, 8, 7, 6, 5, 4, 3, 2].zip(raw[0..8].map(&:to_i)).map { |n,m| n * m }.inject(0) { |i,j| i + j } % 11
62
+ def check_digit_10(raw)
63
+ 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
64
+
43
65
  case cd
44
66
  when 0..9 then cd.to_s
45
67
  when 10 then 'X'
@@ -47,8 +69,8 @@ module Bookland
47
69
  end
48
70
  end
49
71
 
50
- def cd13(raw)
51
- ((10 - [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3].zip(raw[0..11].map(&:to_i)).map { |n,m| n * m }.inject(0) { |i,j| i + j } % 10) % 10).to_s
72
+ def check_digit_13(raw)
73
+ ((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
52
74
  end
53
75
 
54
76
  def isbn10?
@@ -58,18 +80,9 @@ module Bookland
58
80
  def isbn13?
59
81
  @raw.length == 13
60
82
  end
61
-
62
- def valid?
63
- if isbn10?
64
- @raw[9] == cd10(@raw)
65
- elsif isbn13?
66
- @raw[12] == cd13(@raw)
67
- else
68
- false
69
- end
70
- end
71
83
  end
72
84
 
85
+ # A simple error wrapper for a simple ISBN class
73
86
  class ISBNError < StandardError
74
87
  def initialize(msg='ISBN not valid')
75
88
  super
@@ -9,65 +9,75 @@ module Bookland
9
9
  end
10
10
 
11
11
  it "should convert an ISBN-13 to ISBN-10" do
12
- isbns do |isbn10, isbn13|
13
- ISBN.new(isbn13).to_isbn10.should == ISBN.new(isbn10)
14
- end
15
- end
12
+ isbns do |isbn10, isbn13|
13
+ ISBN.new(isbn13).to_isbn10.should == ISBN.new(isbn10)
14
+ end
15
+ end
16
16
 
17
- it "should equate ISBN-10 and ISBN-13" do
18
- isbns do |isbn10, isbn13|
19
- ISBN.new(isbn10).should == ISBN.new(isbn13)
20
- end
21
- end
17
+ it "should equate ISBN-10 and ISBN-13" do
18
+ isbns do |isbn10, isbn13|
19
+ ISBN.new(isbn10).should == ISBN.new(isbn13)
20
+ end
21
+ end
22
22
 
23
- it "should validate ISBN-10s" do
24
- isbns do |isbn10, isbn13|
25
- lambda { ISBN.new(isbn10) }.should_not raise_error
26
- end
27
- end
23
+ it "should validate ISBN-10s" do
24
+ isbns do |isbn10, isbn13|
25
+ ISBN.new(isbn10).valid?.should be_true
26
+ end
27
+ end
28
28
 
29
- it "should validate ISBN-13s" do
30
- isbns do |isbn10, isbn13|
31
- lambda { ISBN.new(isbn13) }.should_not raise_error
32
- end
33
- end
29
+ it "should validate ISBN-13s" do
30
+ isbns do |isbn10, isbn13|
31
+ ISBN.new(isbn13).valid?.should be_true
32
+ end
33
+ end
34
+
35
+ it "should not validate if seed looks like an ISBN-13 but has an invalid check digit" do
36
+ isbns do |isbn10, isbn13|
37
+ invalid = isbn13.gsub(/(.)$/, "#{(isbn13.split(//).last.to_i - 2) % 10}")
38
+ ISBN.new(invalid).valid?.should be_false
39
+ end
40
+ end
34
41
 
35
- it "should not validate if seed looks like an ISBN-13 but has an invalid check digit" do
36
- isbns do |isbn10, isbn13|
37
- inv = isbn13.gsub(/(.)$/, "#{(isbn13.split(//).last.to_i - 2) % 10}")
38
- lambda { ISBN.new(inv) }.should raise_error ISBNError
39
- end
40
- end
42
+ it "should not validate if seed looks like an ISBN-10 but has an invalid check digit" do
43
+ isbns do |isbn10, isbn13|
44
+ invalid = isbn10.gsub(/(.)$/, "#{(isbn10.split(//).last.to_i - 2) % 10}")
45
+ ISBN.new(invalid).valid?.should be_false
46
+ end
47
+ end
41
48
 
42
- it "should not validate if seed looks like an ISBN-10 but has an invalid check digit" do
43
- isbns do |isbn10, isbn13|
44
- inv = isbn10.gsub(/(.)$/, "#{(isbn10.split(//).last.to_i - 2) % 10}")
45
- lambda { ISBN.new(inv) }.should raise_error ISBNError
46
- end
47
- end
49
+ it "should not validate if seed is not an ISBN" do
50
+ ['foo', 1, '', nil].each { |seed| ISBN.new(seed).valid?.should be_false }
51
+ end
48
52
 
49
- it "should not validate if seed is not an ISBN" do
50
- %w{foo 1}.each { |seed| lambda { ISBN.new(seed) }.should raise_error ISBNError }
51
- end
53
+ it "should cast as string" do
54
+ Bookland::ISBN.new('0262011530').to_s.should == '0262011530'
55
+ end
52
56
 
53
- it "should not validate if seed is blank" do
54
- lambda { ISBN.new('') }.should raise_error ISBNError
55
- end
57
+ it "should cast as string and hyphenate an ISBN-13" do
58
+ ISBN.new("9780485113358").to_s(3, 10).should == '978-0485113358'
59
+ end
56
60
 
57
- it "should not validate if seed is nil" do
58
- lambda { ISBN.new(nil) }.should raise_error ISBNError
59
- end
61
+ it "should cast as string and hyphenate an ISBN-10" do
62
+ ISBN.new("048511335X").to_s(1, 3, 5, 1).should == '0-485-11335-X'
63
+ end
60
64
 
61
- it "should cast as string" do
62
- Bookland::ISBN.new('0262011530').to_s.should == '0262011530'
63
- end
65
+ it "should return false if an invalid ISBN is cast as string" do
66
+ ['foo', 1, '', nil].each { |seed| ISBN.new(seed).to_s.should be_false }
67
+ end
64
68
 
65
- it "should cast as string and hyphenate an ISBN-13" do
66
- ISBN.new("9780485113358").to_s(3, 10).should == '978-0485113358'
67
- end
69
+ it "should raise an error if an invalid ISBN-13 is converted to ISBN-10" do
70
+ isbns do |isbn10, isbn13|
71
+ invalid = isbn13.gsub(/(.)$/, "#{(isbn13.split(//).last.to_i - 2) % 10}")
72
+ lambda { ISBN.new(invalid).to_isbn10 }.should raise_error ISBNError
73
+ end
74
+ end
68
75
 
69
- it "should cast as string and hyphenate an ISBN-10" do
70
- ISBN.new("048511335X").to_s(1, 3, 5, 1).should == '0-485-11335-X'
71
- end
76
+ it "should raise an error if an invalid ISBN-10 is converted to ISBN-13" do
77
+ isbns do |isbn10, isbn13|
78
+ invalid = isbn10.gsub(/(.)$/, "#{(isbn10.split(//).last.to_i - 2) % 10}")
79
+ lambda { ISBN.new(invalid).to_isbn13 }.should raise_error ISBNError
80
+ end
81
+ end
72
82
  end
73
83
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Hakan Ensari
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-04-22 00:00:00 +01:00
18
+ date: 2010-04-28 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -30,6 +30,7 @@ extra_rdoc_files:
30
30
  - README.md
31
31
  files:
32
32
  - .gitignore
33
+ - History.md
33
34
  - LICENSE
34
35
  - README.md
35
36
  - Rakefile