bookland 3.0.0 → 3.1.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.
- checksums.yaml +4 -4
- data/README.md +39 -9
- data/bookland.gemspec +1 -1
- data/lib/bookland.rb +1 -0
- data/lib/bookland/identifier.rb +1 -1
- data/lib/bookland/validators.rb +29 -0
- data/lib/bookland/version.rb +1 -1
- data/test/identifier_test.rb +4 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb28ebe351f2678644bfb9e06d41cfef77fe6726
|
4
|
+
data.tar.gz: 1e76248492dfe5eb72823704ae491d628db3a170
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d750aee9d54437ce127c0f460b586b219e544ec74a8aadf9266347f4f4412f90ff85d03b2ce032384f2083afd3c56cbcc5818a4d16cf35040965af8cc0d9e310
|
7
|
+
data.tar.gz: b49e799178349e883c5f8226af3ef53a157fb9fb98ce40327ea5d268618dd539f75e5ef8679c449982470b89f19fc81e55ae18ae9f8b65c45a53439fcdc7b636
|
data/README.md
CHANGED
@@ -1,21 +1,51 @@
|
|
1
1
|
# Bookland
|
2
2
|
|
3
|
-
[Bookland][bo]
|
4
|
-
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
```bash
|
8
|
-
gem install bookland
|
9
|
-
```
|
3
|
+
> [Bookland][bo] is a fictitious country that exists solely for the purposes of non-geographically cataloguing books in the otherwise geographically keyed EAN coding system.
|
10
4
|
|
11
5
|
## Usage
|
12
6
|
|
7
|
+
**Bookland** provides EAN and ISBN classes:
|
8
|
+
|
13
9
|
```ruby
|
14
10
|
include 'bookland'
|
15
11
|
|
12
|
+
# functional
|
16
13
|
ISBN.valid?('9780262011532') # => true
|
17
|
-
|
18
|
-
|
14
|
+
|
15
|
+
# oo
|
16
|
+
isbn = ISBN.new('9780262011532')
|
17
|
+
isbn.valid? # => true
|
19
18
|
```
|
20
19
|
|
20
|
+
**Bookland** also comes with an :poop: ASIN class:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
isbn = '9780262011532'
|
24
|
+
asin = ASIN.from_isbn(isbn) # => "0262011530"
|
25
|
+
ASIN.to_isbn(asin) # => "9780262011532"
|
26
|
+
```
|
27
|
+
|
28
|
+
Caveat: `ASIN` does not calculate the checksum digit for propietary ASINs. If you happen to break their code, ping me.
|
29
|
+
|
30
|
+
All three classes expose a class-level `calculate_checksum_digit` method:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
data_digits = [9, 7, 8, 0, 2, 6, 2, 1, 1, 5, 3]
|
34
|
+
ISBN.calculate_checksum_digit(data_digits) # => 2
|
35
|
+
```
|
36
|
+
|
37
|
+
Rails-heads: **Bookland** will include custom EAN, ISBN, and ASIN validators if Active Model is loaded. Use it like so:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class Book
|
41
|
+
include ActiveModel::Model
|
42
|
+
|
43
|
+
attr_accessor :isbn
|
44
|
+
|
45
|
+
validates :isbn, isbn: true
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
:metal:
|
50
|
+
|
21
51
|
[bo]: http://en.wikipedia.org/wiki/Bookland
|
data/bookland.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Bookland::VERSION
|
9
9
|
spec.authors = ['Hakan Ensari']
|
10
10
|
spec.email = 'hakan.ensari@papercavalier.com'
|
11
|
-
spec.description = %q{
|
11
|
+
spec.description = %q{Provides EAN, ISBN, and ASIN classes and validators in Ruby}
|
12
12
|
spec.summary = %q{An ISBN toolkit}
|
13
13
|
spec.homepage = 'https://github.com/hakanensari/bookland'
|
14
14
|
spec.license = 'MIT'
|
data/lib/bookland.rb
CHANGED
data/lib/bookland/identifier.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
class AsinValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
return if value.nil? && options[:allow_nil]
|
4
|
+
|
5
|
+
unless Bookland::ASIN.valid?(value)
|
6
|
+
record.errors[attribute] << (options[:message] || 'is not an ASIN')
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class EanValidator < ActiveModel::EachValidator
|
12
|
+
def validate_each(record, attribute, value)
|
13
|
+
return if value.nil? && options[:allow_nil]
|
14
|
+
|
15
|
+
unless Bookland::EAN.valid?(value.to_s)
|
16
|
+
record.errors[attribute] << (options[:message] || 'is not an EAN')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class IsbnValidator < ActiveModel::EachValidator
|
22
|
+
def validate_each(record, attribute, value)
|
23
|
+
return if value.nil? && options[:allow_nil]
|
24
|
+
|
25
|
+
unless Bookland::ISBN.valid?(value)
|
26
|
+
record.errors[attribute] << (options[:message] || 'is not an ISBN')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/bookland/version.rb
CHANGED
data/test/identifier_test.rb
CHANGED
@@ -26,4 +26,8 @@ class TestIdentifier < MiniTest::Test
|
|
26
26
|
assert_raises(NotImplementedError) { Identifier.valid?('123') }
|
27
27
|
assert_raises(NotImplementedError) { @id.valid? }
|
28
28
|
end
|
29
|
+
|
30
|
+
def test_validator_casts_input_to_string
|
31
|
+
assert_raises(NotImplementedError) { Identifier.valid?(nil) }
|
32
|
+
end
|
29
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bookland
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hakan Ensari
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
|
-
description:
|
55
|
+
description: Provides EAN, ISBN, and ASIN classes and validators in Ruby
|
56
56
|
email: hakan.ensari@papercavalier.com
|
57
57
|
executables: []
|
58
58
|
extensions: []
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- lib/bookland/ean.rb
|
71
71
|
- lib/bookland/identifier.rb
|
72
72
|
- lib/bookland/isbn.rb
|
73
|
+
- lib/bookland/validators.rb
|
73
74
|
- lib/bookland/version.rb
|
74
75
|
- test/asin_test.rb
|
75
76
|
- test/data/isbns
|
@@ -97,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
98
|
version: '0'
|
98
99
|
requirements: []
|
99
100
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.0.
|
101
|
+
rubygems_version: 2.0.3
|
101
102
|
signing_key:
|
102
103
|
specification_version: 4
|
103
104
|
summary: An ISBN toolkit
|
@@ -108,4 +109,3 @@ test_files:
|
|
108
109
|
- test/identifier_test.rb
|
109
110
|
- test/isbn_test.rb
|
110
111
|
- test/test_helper.rb
|
111
|
-
has_rdoc:
|