validates 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -15,6 +15,7 @@ Add this to your `Gemfile`:
15
15
  model User < ActiveRecord::Base
16
16
  validates :email, :email => true
17
17
  validates :site, :url => true, :allow_blank => true
18
+ validates :inn, :inn => true
18
19
  end
19
20
 
20
21
  model Page < ActiveRecord::Base
@@ -30,6 +31,8 @@ Email, Slug, Url, Money
30
31
 
31
32
  0.0.3 - Added Money validator
32
33
 
34
+ 0.0.4 - Add inn validator.
35
+
33
36
  ## Similar
34
37
 
35
38
  * http://github.com/balexand/email_validator
@@ -3,4 +3,4 @@ require "validates/email_validator"
3
3
  require "validates/slug_validator"
4
4
  require "validates/url_validator"
5
5
  require "validates/money_validator"
6
-
6
+ require "validates/inn_validator"
@@ -0,0 +1,7 @@
1
+ module EmailRegexp
2
+ def regexp_compare(value)
3
+ value =~ /\A[-.\w+]+@([a-z\d][-a-z\d]+\.?[a-z\d]+)+\.[a-z]{2,}\z/i
4
+ end
5
+
6
+ end
7
+
@@ -1,6 +1,8 @@
1
1
  class EmailValidator < ActiveModel::EachValidator
2
+ include EmailRegexp
3
+
2
4
  def validate_each(record, attribute, value)
3
- unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
5
+ unless regexp_compare(value)
4
6
  record.errors.add(attribute, :email, options.merge(:value => value))
5
7
  end
6
8
  end
@@ -0,0 +1,28 @@
1
+ class InnValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, inn)
3
+ result = false
4
+ p10 = [2, 4, 10, 3, 5, 9, 4, 6, 8]
5
+ p11 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
6
+ p12 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
7
+ inn = inn.gsub(/\D/, "") unless inn.nil?
8
+ inn ||= []
9
+ if inn.length == 10
10
+ n10 = calc(p10, inn)
11
+ result = (n10 == inn[9].to_i)
12
+ end
13
+
14
+ if inn.length == 12
15
+ n11 = calc(p11, inn)
16
+ n12 = calc(p12, inn)
17
+ result = (n11 == inn[10].to_i) && (n12 == inn[11].to_i)
18
+ end
19
+
20
+ unless result
21
+ record.errors.add(attribute, :inn, options.merge(:value => inn))
22
+ end
23
+ end
24
+
25
+ def calc(p, inn)
26
+ p.to_enum(:each_with_index).inject(0){|sum, elem| sum + elem[0].to_f * inn[elem[1]].to_f}%11%10
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module Validates
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,53 @@
1
+ require 'test/unit'
2
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/validates/email_regexp')
3
+
4
+ class EmailRegexpTest < Test::Unit::TestCase
5
+ include EmailRegexp
6
+
7
+ def test_valid
8
+ valid_emails = [
9
+ 'user@example.com',
10
+ 'user.mail@example.com',
11
+ 'user@example-domain.com',
12
+ 'user@example-d-a.com',
13
+ 'user@example.domain.com',
14
+ 'user_mail@example.com',
15
+ 'user+mail@example.com',
16
+ 'user123@example.com',
17
+ 'user@example123.com',
18
+ 'user@example.domain',
19
+ 'UseR@example.com',
20
+ 'user-mail@example.com'
21
+ ]
22
+
23
+ valid_emails.each do |email|
24
+ assert regexp_compare(email), "#{email} not valid"
25
+ end
26
+ end
27
+
28
+ def test_invalid
29
+ invalid_emails = [
30
+ 'example_email',
31
+ 'us`er@example.com',
32
+ 'use"r@example.com',
33
+ 'user@example_domain.com',
34
+ 'user@domain.12',
35
+ 'user@domain',
36
+ 'user@domain..com',
37
+ 'user@.domain.com',
38
+ 'user@-domain.com',
39
+ 'user@domain-.com',
40
+ 'user@domain.-a.com',
41
+ 'user@domain-com',
42
+ 'user mail@example.com',
43
+ 'us[er@example.com',
44
+ 'us\'er@example.com',
45
+ 'us\er@example.com'
46
+ ]
47
+
48
+ invalid_emails.each do |email|
49
+ assert !regexp_compare(email), "#{email} valid"
50
+ end
51
+ end
52
+
53
+ end
@@ -5,11 +5,11 @@ require "validates/version"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "validates"
7
7
  s.version = Validates::VERSION
8
- s.authors = ["Mikhail Stolbov"]
9
- s.email = ["mstolbov@gmail.com"]
8
+ s.authors = ["Mikhail Stolbov","Anton Taraev","Konstantin Kosmatov"]
9
+ s.email = ["mstolbov@gmail.com","anti191@gmail.com","key@kosmatov.su"]
10
10
  s.homepage = "https://github.com/kaize/validates"
11
11
  s.summary = "Collection of simple validators for Rails 3"
12
- s.description = "Email, Slug, Url, Money validators for Rails 3"
12
+ s.description = "Email, Slug, Url, Money, INN validators for Rails 3"
13
13
 
14
14
  s.rubyforge_project = "validates"
15
15
 
metadata CHANGED
@@ -1,15 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mikhail Stolbov
9
+ - Anton Taraev
10
+ - Konstantin Kosmatov
9
11
  autorequire:
10
12
  bindir: bin
11
13
  cert_chain: []
12
- date: 2012-05-31 00:00:00.000000000 Z
14
+ date: 2012-09-06 00:00:00.000000000 Z
13
15
  dependencies:
14
16
  - !ruby/object:Gem::Dependency
15
17
  name: activemodel
@@ -27,9 +29,11 @@ dependencies:
27
29
  - - ! '>='
28
30
  - !ruby/object:Gem::Version
29
31
  version: 3.0.0
30
- description: Email, Slug, Url, Money validators for Rails 3
32
+ description: Email, Slug, Url, Money, INN validators for Rails 3
31
33
  email:
32
34
  - mstolbov@gmail.com
35
+ - anti191@gmail.com
36
+ - key@kosmatov.su
33
37
  executables: []
34
38
  extensions: []
35
39
  extra_rdoc_files: []
@@ -39,11 +43,14 @@ files:
39
43
  - README.md
40
44
  - Rakefile
41
45
  - lib/validates.rb
46
+ - lib/validates/email_regexp.rb
42
47
  - lib/validates/email_validator.rb
48
+ - lib/validates/inn_validator.rb
43
49
  - lib/validates/money_validator.rb
44
50
  - lib/validates/slug_validator.rb
45
51
  - lib/validates/url_validator.rb
46
52
  - lib/validates/version.rb
53
+ - test/email_regexp_test.rb
47
54
  - validates.gemspec
48
55
  homepage: https://github.com/kaize/validates
49
56
  licenses: []
@@ -70,3 +77,4 @@ signing_key:
70
77
  specification_version: 3
71
78
  summary: Collection of simple validators for Rails 3
72
79
  test_files: []
80
+ has_rdoc: