bookland 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f62422a2d6bb33379862e355d5fc372afc75aea1
4
- data.tar.gz: 47be6f9d863d31e9ce5edc605fac48338e09e88a
3
+ metadata.gz: bb28ebe351f2678644bfb9e06d41cfef77fe6726
4
+ data.tar.gz: 1e76248492dfe5eb72823704ae491d628db3a170
5
5
  SHA512:
6
- metadata.gz: 401687a31df67958367e50699e0a574f26f0370eb4c5edf684828446492261f9101aac55d4f0d9c22cd5f85f985534cc075d63cbbfc89c9074c6a189621c9680
7
- data.tar.gz: 2e53af118ccd411f724a17fc74548cdfa0df3898a03c10ccedf2068de0680097fb6ba16e1a22e35ed0e44586ccf0c62dadea787edec2b26aede85987cd3143ee
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] provides ISBN and ASIN classes, which should come in handy when trading books online.
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
- ASIN.from_isbn('9780262011532') # => "0262011530"
18
- ASIN.to_isbn('0262011530') # => "9780262011532"
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
@@ -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{An ISBN toolkit}
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'
@@ -2,3 +2,4 @@ require 'bookland/identifier'
2
2
  require 'bookland/ean'
3
3
  require 'bookland/isbn'
4
4
  require 'bookland/asin'
5
+ require 'bookland/validators' if defined?(ActiveModel)
@@ -1,7 +1,7 @@
1
1
  module Bookland
2
2
  class Identifier
3
3
  def self.valid?(raw)
4
- new(raw).valid?
4
+ new(raw.to_s).valid?
5
5
  end
6
6
 
7
7
  def self.calculate_checksum_digit(data_digits)
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Bookland
2
- VERSION = '3.0.0'
2
+ VERSION = '3.1.0'
3
3
  end
@@ -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.0.0
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-06-19 00:00:00.000000000 Z
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: An ISBN toolkit
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.2
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: