isbn_validation 0.1.2 → 1.0.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/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008, 2009 Nick Plante
1
+ Copyright (c) 2011 Nick Plante
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # IsbnValidation
2
+
3
+ Custom ActiveRecord Validation for International Standard Book Number (ISBN)
4
+ fields. Supports both ISBN-10 and ISBN-13. Will guarantee that validated fields
5
+ contain valid ISBNs.
6
+
7
+ Default behaviour is to allow either ISBN-10 or ISBN-13, but this can be
8
+ altered by specifying the :with option as shown in the example below.
9
+
10
+ For more information on ISBN, see http://en.wikipedia.org/wiki/Isbn
11
+
12
+ ## Installation
13
+
14
+ To use it, add it to your Gemfile:
15
+
16
+ gem 'isbn_validation'
17
+
18
+ The current version of isbn_validation only supports Rails 3. For Rails 2.x
19
+ support, please use v0.1.2.
20
+
21
+ ## Example
22
+
23
+ class Book < ActiveRecord::Base
24
+ validates_isbn :isbn
25
+ validates_isbn :isbn10, :with => isbn10
26
+ validates_isbn :isbn13, :with => isbn13
27
+ end
28
+
29
+ ------
30
+
31
+ ## Contributors
32
+
33
+ * Nick Plante
34
+ * Omer Jakobinsky - Rails 3.0 compatibility
35
+
36
+ Copyright &copy; 2011 Nick Plante, released under the MIT license
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
+ require 'bundler'
1
2
  require 'rake'
2
3
  require 'rake/testtask'
3
- require 'rake/rdoctask'
4
+
5
+ Bundler::GemHelper.install_tasks
4
6
 
5
7
  desc 'Default: run unit tests.'
6
8
  task :default => :test
@@ -11,41 +13,3 @@ Rake::TestTask.new(:test) do |t|
11
13
  t.pattern = 'test/**/*_test.rb'
12
14
  t.verbose = true
13
15
  end
14
-
15
- desc 'Generate documentation for the isbn_validation plugin.'
16
- Rake::RDocTask.new(:rdoc) do |rdoc|
17
- rdoc.rdoc_dir = 'rdoc'
18
- rdoc.title = 'IsbnValidation'
19
- rdoc.options << '--line-numbers' << '--inline-source'
20
- rdoc.rdoc_files.include('README')
21
- rdoc.rdoc_files.include('lib/**/*.rb')
22
- end
23
-
24
- spec = Gem::Specification.new do |s|
25
- s.name = %q{isbn_validation}
26
- s.name = %q{isbn_validation}
27
- s.version = "0.1.2"
28
- s.summary = %q{isbn_validation adds an isbn validation routine to active record models.}
29
- s.description = %q{isbn_validation adds an isbn validation routine to active record models.}
30
-
31
- s.files = FileList['[A-Z]*', '{lib,test}/**/*.rb']
32
- s.require_path = 'lib'
33
- s.test_files = Dir[*['test/**/*_test.rb']]
34
-
35
- s.has_rdoc = true
36
- s.extra_rdoc_files = ["README"]
37
- s.rdoc_options = ['--line-numbers', '--inline-source', "--main", "README"]
38
-
39
- s.authors = ["Nick Plante"]
40
- s.email = %q{nap@zerosum.org}
41
-
42
- s.platform = Gem::Platform::RUBY
43
- s.add_dependency(%q<activerecord>, [">= 2.1.2"])
44
- end
45
-
46
- desc "Generate a gemspec file"
47
- task :gemspec do
48
- File.open("#{spec.name}.gemspec", 'w') do |f|
49
- f.write spec.to_ruby
50
- end
51
- end
@@ -1,87 +1,97 @@
1
+ require "isbn_validation/version"
2
+
3
+ # The base module that gets included in ActiveRecord::Base.
4
+ # Validates whether the value of the specified attribute is a proper ISBN number.
5
+ # Defaults to verifying that the value is either a valid ISBN-10 or ISBN-13 number
6
+ # but this behavior can be modified using configuration options (as shown below).
7
+ #
8
+ # class Book < ActiveRecord::Base
9
+ # validates_isbn :isbn
10
+ # #validates_isbn :isbn10, :with => :isbn10
11
+ # #validates_isbn :isbn13, :with => :isbn13
12
+ # end
13
+ #
14
+ # Configuration options:
15
+ # * <tt>:message</tt> - A custom error message (default is: "is not a valid ISBN")
16
+ # * <tt>:with</tt> - A symbol referencing the type of ISBN to validate (:isbn10 or :isbn13)
17
+ # * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
18
+ # * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
19
+ # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
20
+ # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
21
+ # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
22
+ # method, proc or string should return or evaluate to a true or false value.
23
+ # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
24
+ # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
25
+ # method, proc or string should return or evaluate to a true or false value.
1
26
  module Zerosum
2
27
  module ValidationExtensions
3
28
  module IsbnValidation
4
29
  ISBN10_REGEX = /^(?:\d[\ |-]?){9}[\d|X]$/
5
30
  ISBN13_REGEX = /^(?:\d[\ |-]?){13}$/
6
31
 
7
- # Validates whether the value of the specified attribute is a proper ISBN number.
8
- # Defaults to verifying that the value is either a valid ISBN-10 or ISBN-13 number
9
- # but this behavior can be modified using configuration options (as shown below).
10
- #
11
- # class Book < ActiveRecord::Base
12
- # validates_isbn :isbn
13
- # #validates_isbn :isbn10, :with => :isbn10
14
- # #validates_isbn :isbn13, :with => :isbn13
15
- # end
16
- #
17
- # Configuration options:
18
- # * <tt>:message</tt> - A custom error message (default is: "is not a valid ISBN")
19
- # * <tt>:with</tt> - A symbol referencing the type of ISBN to validate (:isbn10 or :isbn13)
20
- # * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
21
- # * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
22
- # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
23
- # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
24
- # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
25
- # method, proc or string should return or evaluate to a true or false value.
26
- # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
27
- # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
28
- # method, proc or string should return or evaluate to a true or false value.
29
- def validates_isbn(*attr_names)
30
- configuration = {
31
- :message => "is not a valid ISBN code"
32
- }
32
+ def self.included(base)
33
+ base.extend(ClassMethods)
34
+ end
33
35
 
34
- configuration.update(attr_names.extract_options!)
35
- validates_each(attr_names, configuration) do |record, attr_name, value|
36
- valid = case configuration[:with]
37
- when :isbn10
38
- validate_with_isbn10(value)
39
- when :isbn13
40
- validate_with_isbn13(value)
41
- else
42
- validate_with_isbn10(value) || validate_with_isbn13(value)
36
+ module ClassMethods
37
+ def validates_isbn(*attr_names)
38
+ configuration = {
39
+ :message => "is not a valid ISBN code"
40
+ }
41
+
42
+ configuration.update(attr_names.extract_options!)
43
+ validates_each(attr_names, configuration) do |record, attr_name, value|
44
+ valid = case configuration[:with]
45
+ when :isbn10
46
+ validate_with_isbn10(value)
47
+ when :isbn13
48
+ validate_with_isbn13(value)
49
+ else
50
+ validate_with_isbn10(value) || validate_with_isbn13(value)
51
+ end
52
+ record.errors.add(attr_name, configuration[:message]) unless valid
43
53
  end
44
- record.errors.add(attr_name, configuration[:message]) unless valid
45
54
  end
46
- end
47
55
 
48
- def validate_with_isbn10(isbn) #:nodoc:
49
- if (isbn || '').match(ISBN10_REGEX)
50
- isbn_values = isbn.upcase.gsub(/\ |-/, '').split('')
51
- check_digit = isbn_values.pop # last digit is check digit
52
- check_digit = (check_digit == 'X') ? 10 : check_digit.to_i
56
+ def validate_with_isbn10(isbn) #:nodoc:
57
+ if (isbn || '').match(ISBN10_REGEX)
58
+ isbn_values = isbn.upcase.gsub(/\ |-/, '').split('')
59
+ check_digit = isbn_values.pop # last digit is check digit
60
+ check_digit = (check_digit == 'X') ? 10 : check_digit.to_i
53
61
 
54
- sum = 0
55
- isbn_values.each_with_index do |value, index|
56
- sum += (index + 1) * value.to_i
57
- end
62
+ sum = 0
63
+ isbn_values.each_with_index do |value, index|
64
+ sum += (index + 1) * value.to_i
65
+ end
58
66
 
59
- (sum % 11) == check_digit
60
- else
61
- false
67
+ (sum % 11) == check_digit
68
+ else
69
+ false
70
+ end
62
71
  end
63
- end
64
72
 
65
- def validate_with_isbn13(isbn) #:nodoc:
66
- if (isbn || '').match(ISBN13_REGEX)
67
- isbn_values = isbn.upcase.gsub(/\ |-/, '').split('')
68
- check_digit = isbn_values.pop.to_i # last digit is check digit
73
+ def validate_with_isbn13(isbn) #:nodoc:
74
+ if (isbn || '').match(ISBN13_REGEX)
75
+ isbn_values = isbn.upcase.gsub(/\ |-/, '').split('')
76
+ check_digit = isbn_values.pop.to_i # last digit is check digit
69
77
 
70
- sum = 0
71
- isbn_values.each_with_index do |value, index|
72
- multiplier = (index % 2 == 0) ? 1 : 3
73
- sum += multiplier * value.to_i
74
- end
78
+ sum = 0
79
+ isbn_values.each_with_index do |value, index|
80
+ multiplier = (index % 2 == 0) ? 1 : 3
81
+ sum += multiplier * value.to_i
82
+ end
75
83
 
76
- result = (10 - (sum % 10))
77
- result = 0 if result == 10
84
+ result = (10 - (sum % 10))
85
+ result = 0 if result == 10
78
86
 
79
- result == check_digit
80
- else
81
- false
87
+ result == check_digit
88
+ else
89
+ false
90
+ end
82
91
  end
83
92
  end
84
93
  end
85
94
  end
86
95
  end
87
96
 
97
+ ActiveRecord::Base.send(:include, Zerosum::ValidationExtensions::IsbnValidation)
@@ -0,0 +1,3 @@
1
+ module IsbnValidation
2
+ VERSION = "1.0.0"
3
+ end
@@ -87,7 +87,7 @@ class IsbnValidationTest < Test::Unit::TestCase
87
87
  def test_should_have_custom_error_message
88
88
  @book.isbn = '978-159059AAAAAA'
89
89
  @book.valid?
90
- assert_equal 'is too fantastical!', @book.errors.on(:isbn)
90
+ assert_equal 'is too fantastical!', @book.errors[:isbn].first
91
91
  end
92
92
 
93
93
  def test_isbn13_with_zero_check_digit_should_validate
data/test/test_helper.rb CHANGED
@@ -1,13 +1,15 @@
1
1
  $:.unshift(File.dirname(__FILE__) + '/../lib')
2
2
  RAILS_ROOT = File.dirname(__FILE__)
3
3
 
4
+ require 'logger'
4
5
  require 'rubygems'
5
6
  require 'test/unit'
6
7
  require 'active_record'
8
+ require "active_record/test_case"
7
9
  require "#{File.dirname(__FILE__)}/../init"
8
10
 
9
11
  config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
10
- ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
12
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
11
13
  ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
12
14
 
13
15
  load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
metadata CHANGED
@@ -1,39 +1,39 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: isbn_validation
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Nick Plante
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
-
12
- date: 2009-09-10 00:00:00 -04:00
12
+ date: 2009-09-10 00:00:00.000000000 -04:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
16
  name: activerecord
17
+ requirement: &2153555480 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
17
23
  type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 2.1.2
24
- version:
24
+ prerelease: false
25
+ version_requirements: *2153555480
25
26
  description: isbn_validation adds an isbn validation routine to active record models.
26
27
  email: nap@zerosum.org
27
28
  executables: []
28
-
29
29
  extensions: []
30
-
31
- extra_rdoc_files:
32
- - README
33
- files:
30
+ extra_rdoc_files:
31
+ - README.md
32
+ files:
34
33
  - MIT-LICENSE
35
34
  - Rakefile
36
- - README
35
+ - README.md
36
+ - lib/isbn_validation/version.rb
37
37
  - lib/isbn_validation.rb
38
38
  - test/isbn_validation_test.rb
39
39
  - test/models.rb
@@ -42,33 +42,31 @@ files:
42
42
  has_rdoc: true
43
43
  homepage:
44
44
  licenses: []
45
-
46
45
  post_install_message:
47
- rdoc_options:
46
+ rdoc_options:
48
47
  - --line-numbers
49
48
  - --inline-source
50
49
  - --main
51
- - README
52
- require_paths:
50
+ - README.md
51
+ require_paths:
53
52
  - lib
54
- required_ruby_version: !ruby/object:Gem::Requirement
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- version: "0"
59
- version:
60
- required_rubygems_version: !ruby/object:Gem::Requirement
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- version: "0"
65
- version:
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
66
65
  requirements: []
67
-
68
66
  rubyforge_project:
69
- rubygems_version: 1.3.5
67
+ rubygems_version: 1.6.2
70
68
  signing_key:
71
69
  specification_version: 3
72
70
  summary: isbn_validation adds an isbn validation routine to active record models.
73
- test_files:
71
+ test_files:
74
72
  - test/isbn_validation_test.rb
data/README DELETED
@@ -1,27 +0,0 @@
1
- IsbnValidation
2
- ==============
3
-
4
- Custom ActiveRecord Validation for International Standard Book Number (ISBN) fields.
5
- Supports both ISBN-10 and ISBN-13. Will guarantee that validated fields contain valid
6
- ISBNs.
7
-
8
- Default behavior is to allow either ISBN-10 or ISBN-13, but this can be altered by
9
- specifying the :with option as shown in the example below.
10
-
11
- For more information on ISBN, see http://en.wikipedia.org/wiki/Isbn
12
-
13
- Installation
14
- ============
15
-
16
- ruby script/plugin install git://github.com/zapnap/isbn_validation
17
-
18
- Example
19
- =======
20
-
21
- class Book < ActiveRecord::Base
22
- validates_isbn :isbn
23
- validates_isbn :isbn10, :with => :isbn10
24
- validates_isbn :isbn13, :with => :isbn13
25
- end
26
-
27
- Copyright (c) 2008 Nick Plante, released under the MIT license