isbn10 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/CHANGELOG +3 -0
  2. data/lib/isbn10.rb +69 -1
  3. data/spec/isbn10_spec.rb +14 -1
  4. metadata +12 -4
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ v1.2 (6th October 2010)
2
+ * Add grouped() method for returning a ISBN with hyphens
3
+
1
4
  v1.1 (19th July 2010)
2
5
  * correctly recognise ISBN10s that end in a lower x
3
6
 
@@ -6,7 +6,7 @@ class ISBN10
6
6
 
7
7
  class Version #:nodoc:
8
8
  Major = 1
9
- Minor = 1
9
+ Minor = 2
10
10
  Tiny = 0
11
11
 
12
12
  String = [Major, Minor, Tiny].join('.')
@@ -48,9 +48,77 @@ class ISBN10
48
48
  end
49
49
  end
50
50
 
51
+ def grouped
52
+ return nil unless self.valid?
53
+
54
+ if @number[0,1] == "0"
55
+ grouped_zero
56
+ elsif @number[0,1] == "1"
57
+ grouped_one
58
+ else
59
+ nil
60
+ end
61
+ end
62
+
51
63
  def to_ean
52
64
  return nil unless self.valid?
53
65
  EAN13.complete("978#{@number[0,9]}")
54
66
  end
55
67
 
68
+ private
69
+
70
+ def grouped_zero
71
+ groups = [0]
72
+ buffer = @number[1,7].to_i
73
+
74
+ if buffer < 2000000
75
+ groups << @number[1,2]
76
+ groups << @number[3,6]
77
+ elsif buffer >= 2000000 && buffer < 7000000
78
+ groups << @number[1,3]
79
+ groups << @number[4,5]
80
+ elsif buffer >= 7000000 && buffer < 8500000
81
+ groups << @number[1,4]
82
+ groups << @number[5,4]
83
+ elsif buffer >= 8500000 && buffer < 9000000
84
+ groups << @number[1,5]
85
+ groups << @number[6,3]
86
+ elsif buffer >= 9000000 && buffer < 9500000
87
+ groups << @number[1,6]
88
+ groups << @number[7,2]
89
+ else
90
+ groups << @number[1,7]
91
+ groups << @number[8,1]
92
+ end
93
+ groups << @number[9,1]
94
+ groups.join("-")
95
+ end
96
+
97
+ def grouped_one
98
+ groups = [1]
99
+ buffer = @number[1,7].to_i
100
+
101
+ if buffer < 1000000
102
+ groups << @number[1,2]
103
+ groups << @number[3,6]
104
+ elsif buffer >= 1000000 && buffer < 4000000
105
+ groups << @number[1,3]
106
+ groups << @number[4,5]
107
+ elsif buffer >= 4000000 && buffer < 5500000
108
+ groups << @number[1,4]
109
+ groups << @number[5,4]
110
+ elsif buffer >= 5500000 && buffer < 8698000
111
+ groups << @number[1,5]
112
+ groups << @number[6,3]
113
+ elsif buffer >= 8698000 && buffer < 9990000
114
+ groups << @number[1,6]
115
+ groups << @number[7,2]
116
+ else
117
+ groups << @number[1,7]
118
+ groups << @number[8,1]
119
+ end
120
+ groups << @number[9,1]
121
+ groups.join("-")
122
+ end
123
+
56
124
  end
@@ -15,7 +15,7 @@ describe ISBN10 do
15
15
  ISBN10.valid?("0140449043").should be_true
16
16
  ISBN10.valid?("0-140-44904-3").should be_true
17
17
  end
18
-
18
+
19
19
  it "should identify an invalid ISBN10" do
20
20
  ISBN10.valid?(nil).should be_false
21
21
  ISBN10.valid?("0-1404-4904-2").should be_false
@@ -36,4 +36,17 @@ describe ISBN10 do
36
36
  ISBN10.new("043429067x").to_ean.should eql("9780434290673")
37
37
  end
38
38
 
39
+ it "should convert to a grouped ISBN10 correctly" do
40
+ ISBN10.new("0809141875").grouped.should be_nil
41
+ ISBN10.new("0140361278").grouped.should eql("0-14-036127-8")
42
+ ISBN10.new("0321350316").grouped.should eql("0-321-35031-6")
43
+ ISBN10.new("0809141876").grouped.should eql("0-8091-4187-6")
44
+ ISBN10.new("0855615346").grouped.should eql("0-85561-534-6")
45
+ ISBN10.new("094727748X").grouped.should eql("0-947277-48-X")
46
+ ISBN10.new("0975601873").grouped.should eql("0-9756018-7-3")
47
+ ISBN10.new("1430219483").grouped.should eql("1-4302-1948-3")
48
+ ISBN10.new("1855548453").grouped.should eql("1-85554-845-3")
49
+ ISBN10.new("1921640065").grouped.should eql("1-921640-06-5")
50
+ end
51
+
39
52
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isbn10
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 31
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
- - 1
8
+ - 2
8
9
  - 0
9
- version: 1.1.0
10
+ version: 1.2.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - James Healy
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-07-19 00:00:00 +10:00
18
+ date: 2010-10-06 00:00:00 +11:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: ean13
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 3
27
30
  segments:
28
31
  - 0
29
32
  version: "0"
@@ -42,6 +45,7 @@ files:
42
45
  - MIT-LICENSE
43
46
  - README.rdoc
44
47
  - CHANGELOG
48
+ - spec/isbn10_spec.rb
45
49
  has_rdoc: true
46
50
  homepage: http://github.com/yob/isbn10/tree/master
47
51
  licenses: []
@@ -54,23 +58,27 @@ rdoc_options:
54
58
  require_paths:
55
59
  - lib
56
60
  required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
57
62
  requirements:
58
63
  - - ">="
59
64
  - !ruby/object:Gem::Version
65
+ hash: 3
60
66
  segments:
61
67
  - 0
62
68
  version: "0"
63
69
  required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
64
71
  requirements:
65
72
  - - ">="
66
73
  - !ruby/object:Gem::Version
74
+ hash: 3
67
75
  segments:
68
76
  - 0
69
77
  version: "0"
70
78
  requirements: []
71
79
 
72
80
  rubyforge_project:
73
- rubygems_version: 1.3.6
81
+ rubygems_version: 1.3.7
74
82
  signing_key:
75
83
  specification_version: 3
76
84
  summary: a (very) small library for working with ISBN10 codes