bookland 3.1.0 → 3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb28ebe351f2678644bfb9e06d41cfef77fe6726
4
- data.tar.gz: 1e76248492dfe5eb72823704ae491d628db3a170
3
+ metadata.gz: 5b5ae359f32e9fe781b86320bcf0b7a7b095caa4
4
+ data.tar.gz: 80143d0efdb7a3b958ab4e5fddefb93579d3b9da
5
5
  SHA512:
6
- metadata.gz: d750aee9d54437ce127c0f460b586b219e544ec74a8aadf9266347f4f4412f90ff85d03b2ce032384f2083afd3c56cbcc5818a4d16cf35040965af8cc0d9e310
7
- data.tar.gz: b49e799178349e883c5f8226af3ef53a157fb9fb98ce40327ea5d268618dd539f75e5ef8679c449982470b89f19fc81e55ae18ae9f8b65c45a53439fcdc7b636
6
+ metadata.gz: 772184fabb0aef7f79fa41a7dfaa9abfac58c27bb85dd518f2282ef64b3cfeb7db0e132fb4291391ef47d4d08cf5f0bea78daa757e3cd9e6bb3d118b2e5916a9
7
+ data.tar.gz: 2da6323dc8a21306674b50199d653be13bd71f89dc725ccc4b38a9af198b5e67b5cfcb962bc3121a9810984718037274a5d855080068754a9c85d9f4f7b1e903
data/README.md CHANGED
@@ -10,6 +10,7 @@
10
10
  include 'bookland'
11
11
 
12
12
  # functional
13
+ EAN.valid?('0814916013890') # => true
13
14
  ISBN.valid?('9780262011532') # => true
14
15
 
15
16
  # oo
@@ -17,7 +18,7 @@ isbn = ISBN.new('9780262011532')
17
18
  isbn.valid? # => true
18
19
  ```
19
20
 
20
- **Bookland** also comes with an :poop: ASIN class:
21
+ **Bookland** also comes with an ASIN class:
21
22
 
22
23
  ```ruby
23
24
  isbn = '9780262011532'
@@ -25,7 +26,7 @@ asin = ASIN.from_isbn(isbn) # => "0262011530"
25
26
  ASIN.to_isbn(asin) # => "9780262011532"
26
27
  ```
27
28
 
28
- Caveat: `ASIN` does not calculate the checksum digit for propietary ASINs. If you happen to break their code, ping me.
29
+ Caveat: `ASIN` does not calculate the checksum digit for propietary ASINs. If you happen to break their algo, ping me.
29
30
 
30
31
  All three classes expose a class-level `calculate_checksum_digit` method:
31
32
 
@@ -34,7 +35,7 @@ data_digits = [9, 7, 8, 0, 2, 6, 2, 1, 1, 5, 3]
34
35
  ISBN.calculate_checksum_digit(data_digits) # => 2
35
36
  ```
36
37
 
37
- Rails-heads: **Bookland** will include custom EAN, ISBN, and ASIN validators if Active Model is loaded. Use it like so:
38
+ **Bookland** includes custom EAN, ISBN, and ASIN validators if Active Model is loaded. Use it like so:
38
39
 
39
40
  ```ruby
40
41
  class Book
@@ -46,6 +47,6 @@ class Book
46
47
  end
47
48
  ```
48
49
 
49
- :metal:
50
+ :metal::metal:
50
51
 
51
52
  [bo]: http://en.wikipedia.org/wiki/Bookland
@@ -20,5 +20,6 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency 'bundler', '~> 1.3'
22
22
  spec.add_development_dependency 'rake'
23
- spec.add_development_dependency 'minitest', '~> 5.0'
23
+ spec.add_development_dependency 'minitest', '~> 4.2'
24
+ spec.add_development_dependency 'activemodel', '~> 4.0'
24
25
  end
@@ -1,29 +1,17 @@
1
- class AsinValidator < ActiveModel::EachValidator
2
- def validate_each(record, attribute, value)
3
- return if value.nil? && options[:allow_nil]
1
+ %w(ASIN EAN ISBN).each do |klass|
2
+ Object.class_eval <<-EOF
3
+ class #{klass.capitalize}Validator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ return if value.nil? && options[:allow_nil]
4
6
 
5
- unless Bookland::ASIN.valid?(value)
6
- record.errors[attribute] << (options[:message] || 'is not an ASIN')
7
+ unless Bookland::#{klass}.valid?(value)
8
+ if options[:strict]
9
+ raise ActiveModel::StrictValidationFailed, "\#{value} \#{options[:message] || "is not an #{klass}"}"
10
+ else
11
+ record.errors[attribute] << (options[:message] || 'is not an #{klass}')
12
+ end
13
+ end
14
+ end
7
15
  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
16
+ EOF
29
17
  end
@@ -1,3 +1,3 @@
1
1
  module Bookland
2
- VERSION = '3.1.0'
2
+ VERSION = '3.2.0'
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class TestASIN < MiniTest::Test
3
+ class TestASIN < MiniTest::Unit::TestCase
4
4
  def test_validates_isbn_like_asins
5
5
  File.open('./test/data/isbns').each do |line|
6
6
  asin = line.split.first.chomp
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class TestEAN < MiniTest::Test
3
+ class TestEAN < MiniTest::Unit::TestCase
4
4
  def test_validates
5
5
  assert EAN.valid?('0814916013890')
6
6
  end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class TestIdentifier < MiniTest::Test
3
+ class TestIdentifier < MiniTest::Unit::TestCase
4
4
  def setup
5
5
  @id = Identifier.new('123')
6
6
  end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class TestISBN < MiniTest::Test
3
+ class TestISBN < MiniTest::Unit::TestCase
4
4
  def test_validates
5
5
  File.open('./test/data/isbns').each do |line|
6
6
  isbn = line.split.last.chomp
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
2
+
3
+ require 'active_model'
4
+ require 'bookland/validators'
5
+
6
+ class TestValidators < MiniTest::Unit::TestCase
7
+ def setup
8
+ @klass = Class.new do
9
+ include ActiveModel::Model
10
+ attr_accessor :attribute
11
+ end
12
+ end
13
+
14
+ def test_validates_asin
15
+ @klass.validates(:attribute, asin: true)
16
+ instance = @klass.new
17
+ refute instance.valid?
18
+ instance.attribute = '123'
19
+ refute instance.valid?
20
+ instance.attribute = '0262011530'
21
+ assert instance.valid?
22
+ end
23
+
24
+ def test_allows_nil_asin
25
+ @klass.validates(:attribute, asin: true, allow_nil: true)
26
+ instance = @klass.new
27
+ assert instance.valid?
28
+ end
29
+
30
+ def test_validates_asin_strictly
31
+ @klass.validates!(:attribute, asin: true)
32
+ instance = @klass.new(attribute: '0262011530')
33
+ assert instance.valid?
34
+ instance.attribute = nil
35
+ instance = @klass.new
36
+ assert_raises(ActiveModel::StrictValidationFailed) { instance.valid? }
37
+ end
38
+
39
+ def test_validates_ean
40
+ @klass.validates(:attribute, ean: true)
41
+ instance = @klass.new
42
+ refute instance.valid?
43
+ instance.attribute = '123'
44
+ refute instance.valid?
45
+ instance.attribute = '0814916013890'
46
+ assert instance.valid?
47
+ end
48
+
49
+ def test_allows_nil_ean
50
+ @klass.validates(:attribute, ean: true, allow_nil: true)
51
+ instance = @klass.new
52
+ assert instance.valid?
53
+ end
54
+
55
+ def test_validates_ean_strictly
56
+ @klass.validates!(:attribute, ean: true)
57
+ instance = @klass.new(attribute: '0814916013890')
58
+ assert instance.valid?
59
+ instance.attribute = nil
60
+ assert_raises(ActiveModel::StrictValidationFailed) { instance.valid? }
61
+ end
62
+
63
+ def test_validates_isbn
64
+ @klass.validates(:attribute, isbn: true)
65
+ instance = @klass.new
66
+ refute instance.valid?
67
+ instance.attribute = '123'
68
+ refute instance.valid?
69
+ instance.attribute = '9780262011532'
70
+ assert instance.valid?
71
+ end
72
+
73
+ def test_allows_nil_isbn
74
+ @klass.validates(:attribute, isbn: true, allow_nil: true)
75
+ instance = @klass.new
76
+ assert instance.valid?
77
+ end
78
+
79
+ def test_validates_isbn_strictly
80
+ @klass.validates!(:attribute, isbn: true)
81
+ instance = @klass.new(attribute: '9780262011532')
82
+ assert instance.valid?
83
+ instance.attribute = nil
84
+ assert_raises(ActiveModel::StrictValidationFailed) { instance.valid? }
85
+ end
86
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bookland
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hakan Ensari
@@ -44,14 +44,28 @@ dependencies:
44
44
  requirements:
45
45
  - - ~>
46
46
  - !ruby/object:Gem::Version
47
- version: '5.0'
47
+ version: '4.2'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: '5.0'
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
55
69
  description: Provides EAN, ISBN, and ASIN classes and validators in Ruby
56
70
  email: hakan.ensari@papercavalier.com
57
71
  executables: []
@@ -78,6 +92,7 @@ files:
78
92
  - test/identifier_test.rb
79
93
  - test/isbn_test.rb
80
94
  - test/test_helper.rb
95
+ - test/validators_test.rb
81
96
  homepage: https://github.com/hakanensari/bookland
82
97
  licenses:
83
98
  - MIT
@@ -109,3 +124,4 @@ test_files:
109
124
  - test/identifier_test.rb
110
125
  - test/isbn_test.rb
111
126
  - test/test_helper.rb
127
+ - test/validators_test.rb