ghazel-validates_email_format_of 1.3.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,22 @@
1
+ = CHANGELOG
2
+
3
+ == Version 1.0
4
+ * initial version
5
+
6
+ == Version 1.1 (the Francis Hwang edition)
7
+ * moved Regexp out of class methods into the ValidatesEmailFormatOf module
8
+
9
+ == Version 1.2 (the Ismael Santos Kafeltz and Michael MacDonald edition)
10
+ * added support for un-escaped and escaped special characters in the local part, per RFC 3696
11
+ * added :allow_nil option
12
+
13
+ == Version 1.2.1 (the RTFM edition)
14
+ * added support for quoted local parts
15
+ * added length checks for domain and local parts
16
+ * corrected escaped character support for RFC 3696 Errata
17
+ * added :allow_blank option
18
+ * added :unless option
19
+
20
+ == Unreleased
21
+ * Now available as a gem on GitHub
22
+ * added should_validate_email_format_of
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 Alex Dunae
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,58 @@
1
+ Validates email format
2
+ ======================
3
+
4
+ Validate various formats of email address against RFC 2822.
5
+
6
+ Usage
7
+ -----
8
+
9
+ class PersonTest < ActiveSupport::TestCase
10
+ should_validate_email_format_of :email
11
+ end
12
+
13
+ class Person < ActiveRecord::Base
14
+ validates_email_format_of :email
15
+ end
16
+
17
+ Options
18
+ -------
19
+
20
+ :message =>
21
+ String. A custom error message (default is: " does not appear to be a valid e-mail address")
22
+
23
+ :on =>
24
+ Symbol. Specifies when this validation is active (default is :save, other options :create, :update)
25
+
26
+ :allow_nil =>
27
+ Boolean. Allow nil values (default is false)
28
+
29
+ :allow_blank =>
30
+ Boolean. Allow blank values (default is false)
31
+
32
+ :if =>
33
+ Specifies a method, proc or string to call to determine if the validation should occur
34
+ (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method,
35
+ proc or string should return or evaluate to a true or false value.
36
+
37
+ :unless =>
38
+ See :if option.
39
+
40
+ Testing
41
+ -------
42
+
43
+ To execute the unit tests run <tt>rake test</tt>.
44
+
45
+ The unit tests for this plugin use an in-memory sqlite3 database.
46
+
47
+ Installing the gem
48
+ ------------------
49
+
50
+ * gem sources -a http://gems.github.com (only needed once)
51
+ * sudo gem install dancroak-validates\_email\_format\_of
52
+
53
+ Credits
54
+ -------
55
+
56
+ Written by Alex Dunae (dunae.ca), 2006-07.
57
+
58
+ Thanks to Francis Hwang (http://fhwang.net/) at Diversion Media for creating the 1.1 update.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ test_files_pattern = 'test/*_test.rb'
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'lib'
7
+ t.pattern = test_files_pattern
8
+ t.verbose = false
9
+ end
10
+
11
+ desc "Run the test suite"
12
+ task :default => :test
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rails', 'init')
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ module ValidatesEmailFormatOf
4
+ LocalPartSpecialChars = Regexp.escape('!#$%&\'*-/=?+-^_`{|}~')
5
+ LocalPartUnquoted = '(([[:alnum:]' + LocalPartSpecialChars + ']+[\.\+]+))*[[:alnum:]' + LocalPartSpecialChars + '+]+'
6
+ LocalPartQuoted = '\"(([[:alnum:]' + LocalPartSpecialChars + '\.\+]*|(\\\\[\u0001-\uFFFF]))*)\"'
7
+ DomainWordChar = '[a-zA-Z0-9]'
8
+ Regex = Regexp.new('^((' + LocalPartUnquoted + ')|(' + LocalPartQuoted + ')+)@(((' + DomainWordChar + '+\-+)|(' + DomainWordChar + '+\.))*' + DomainWordChar + '{1,63}\.[a-z]{2,6}$)', Regexp::EXTENDED | Regexp::IGNORECASE)
9
+ end
10
+
11
+ module ActiveRecord
12
+ module Validations
13
+ module ClassMethods
14
+ # Validates whether the value of the specified attribute is a valid email address
15
+ #
16
+ # class User < ActiveRecord::Base
17
+ # validates_email_format_of :email, :on => :create
18
+ # end
19
+ #
20
+ # Configuration options:
21
+ # * <tt>message</tt> - A custom error message (default is: " does not appear to be a valid e-mail address")
22
+ # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
23
+ # * <tt>allow_nil</tt> - Allow nil values (default is false)
24
+ # * <tt>allow_blank</tt> - Allow blank values (default is false)
25
+ # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
26
+ # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
27
+ # method, proc or string should return or evaluate to a true or false value.
28
+ # * <tt>unless</tt> - See <tt>:if</tt>
29
+ def validates_email_format_of(*attr_names)
30
+ options = { :message => ' does not appear to be a valid e-mail address',
31
+ :on => :save,
32
+ :allow_nil => false,
33
+ :allow_blank => false,
34
+ :with => ValidatesEmailFormatOf::Regex }
35
+
36
+ options.update(attr_names.pop) if attr_names.last.is_a?(Hash)
37
+
38
+ validates_each(attr_names, options) do |record, attr_name, value|
39
+ v = value.to_s
40
+
41
+ # local part max is 64 chars, domain part max is 255 chars
42
+ # TODO: should this decode escaped entities before counting?
43
+ begin
44
+ domain, local = v.reverse.split('@', 2)
45
+ rescue
46
+ record.errors.add(attr_name, options[:message])
47
+ next
48
+ end
49
+
50
+ unless v =~ options[:with] and not v =~ /\.\./ and domain.length <= 255 and local.length <= 64
51
+ record.errors.add(attr_name, options[:message])
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'validates_email_format_of'
@@ -0,0 +1,47 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'rubygems'
4
+ require 'active_record'
5
+ require 'active_record/base'
6
+
7
+ require 'validates_email_format_of'
8
+
9
+ ActiveRecord::Base.establish_connection(
10
+ :adapter => 'sqlite3',
11
+ :database => ':memory:')
12
+
13
+ ActiveRecord::Schema.define(:version => 0) do
14
+ create_table :users, :force => true do |t|
15
+ t.column 'email', :string
16
+ end
17
+ end
18
+
19
+ class Person < ActiveRecord::Base
20
+ validates_email_format_of :email, :on => :create, :message => 'fails with custom message', :allow_nil => true
21
+ end
22
+
23
+ require 'test/unit'
24
+ require 'shoulda'
25
+ require "#{File.dirname(__FILE__)}/../init"
26
+
27
+ class Test::Unit::TestCase #:nodoc:
28
+ def self.should_allow_values(klass,*good_values)
29
+ good_values.each do |v|
30
+ should "allow email to be set to #{v.inspect}" do
31
+ user = klass.new(:email => v)
32
+ user.save
33
+ assert_nil user.errors.on(:email)
34
+ end
35
+ end
36
+ end
37
+
38
+ def self.should_not_allow_values(klass,*bad_values)
39
+ bad_values.each do |v|
40
+ should "not allow email to be set to #{v.inspect}" do
41
+ user = klass.new(:email => v)
42
+ assert !user.save, "Saved user with email set to \"#{v}\""
43
+ assert user.errors.on(:email), "There are no errors set on email after being set to \"#{v}\""
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require File.dirname(__FILE__) + '/../shoulda_macros/validates_email_format_of'
3
+
4
+ class User < ActiveRecord::Base
5
+ validates_email_format_of :email,
6
+ :on => :create,
7
+ :message => 'fails with custom message',
8
+ :allow_nil => true
9
+ end
10
+
11
+ class ValidatesEmailFormatOfTest < Test::Unit::TestCase
12
+ should_validate_email_format_of_klass(User, :email)
13
+
14
+ context 'An invalid user on update' do
15
+ setup do
16
+ @user = User.new(:email => 'dcroak@thoughtbot.com')
17
+ assert @user.save
18
+ assert @user.update_attribute(:email, '..dcroak@thoughtbot.com')
19
+ end
20
+
21
+ should 'pass validation' do
22
+ assert @user.valid?
23
+ assert @user.save
24
+ assert_nil @user.errors.on(:email)
25
+ end
26
+ end
27
+
28
+ context 'A user with a nil email' do
29
+ setup { @user = User.new(:email => nil) }
30
+
31
+ should 'pass validation' do
32
+ assert @user.valid?
33
+ assert @user.save
34
+ assert_nil @user.errors.on(:email)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,32 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "ghazel-validates_email_format_of"
3
+ s.version = "1.3.0.1"
4
+ s.date = "2009-06-08"
5
+ s.summary = "Validate e-mail addreses against RFC 2822 and RFC 3696."
6
+ s.email = "dcroak@thoughtbot.com"
7
+ s.description = "Validate e-mail addreses against RFC 2822 and RFC 3696."
8
+ s.authors = ["Alex Dunae", "Dan Croak", "Mike Burns"]
9
+ s.extra_rdoc_files = ["CHANGELOG",
10
+ "lib/validates_email_format_of.rb",
11
+ "README.markdown"]
12
+ s.files = ["CHANGELOG",
13
+ "init.rb",
14
+ "lib/validates_email_format_of.rb",
15
+ "MIT-LICENSE",
16
+ "rails/init.rb",
17
+ "Rakefile",
18
+ #"README",
19
+ #"test/database.yml",
20
+ #"test/fixtures/people.yml",
21
+ #"test/fixtures/person.rb",
22
+ #"test/schema.rb",
23
+ "test/test_helper.rb",
24
+ "test/validates_email_format_of_test.rb",
25
+ "Rakefile",
26
+ "validates_email_format_of.gemspec"]
27
+ s.has_rdoc = true
28
+ s.homepage = %q{http://code.dunae.ca/validates_email_format_of.html}
29
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Validates_email_format_of"]
30
+ s.require_paths = ["lib"]
31
+ end
32
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ghazel-validates_email_format_of
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Dunae
8
+ - Dan Croak
9
+ - Mike Burns
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-06-08 00:00:00 -07:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description: Validate e-mail addreses against RFC 2822 and RFC 3696.
19
+ email: dcroak@thoughtbot.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files:
25
+ - CHANGELOG
26
+ - lib/validates_email_format_of.rb
27
+ - README.markdown
28
+ files:
29
+ - CHANGELOG
30
+ - init.rb
31
+ - lib/validates_email_format_of.rb
32
+ - MIT-LICENSE
33
+ - rails/init.rb
34
+ - Rakefile
35
+ - test/test_helper.rb
36
+ - test/validates_email_format_of_test.rb
37
+ - validates_email_format_of.gemspec
38
+ - README.markdown
39
+ has_rdoc: true
40
+ homepage: http://code.dunae.ca/validates_email_format_of.html
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --line-numbers
46
+ - --inline-source
47
+ - --title
48
+ - Validates_email_format_of
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Validate e-mail addreses against RFC 2822 and RFC 3696.
70
+ test_files: []
71
+