validates_as_email_address 0.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,5 @@
1
+ *SVN*
2
+
3
+ *0.0.1* (September 26th, 2007)
4
+
5
+ * Add documentation
data/LICENSE ADDED
@@ -0,0 +1,2 @@
1
+ Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
2
+ http://creativecommons.org/licenses/by-sa/2.5/
data/README ADDED
@@ -0,0 +1,50 @@
1
+ = validates_as_email_address
2
+
3
+ +validates_as_email_address+ adds support for validating the format/length of
4
+ email addresses.
5
+
6
+ == Resources
7
+
8
+ API
9
+
10
+ * http://api.pluginaweek.org/validates_as_email_address
11
+
12
+ Wiki
13
+
14
+ * http://wiki.pluginaweek.org/Validates_as_email_address
15
+
16
+ Announcement
17
+
18
+ * http://www.pluginaweek.org
19
+
20
+ Source
21
+
22
+ * http://svn.pluginaweek.org/trunk/plugins/active_record/validations/validates_as_email_address
23
+
24
+ Development
25
+
26
+ * http://dev.pluginaweek.org/browser/trunk/plugins/active_record/validations/validates_as_email_address
27
+
28
+ == Description
29
+
30
+ Consistently reliable email address validations are difficult to find and hard
31
+ to choose as there are far too many implementations in various programming
32
+ languages. This plugins builds on Thijs van der Vossen's validates_as_email
33
+ by adding advanced validation option support and also validating the length of
34
+ the email address.
35
+
36
+ == Usage
37
+
38
+ === Example
39
+
40
+ class Person < ActiveRecord::Base
41
+ validates_as_email_address :email, :on => :create
42
+ end
43
+
44
+ == References
45
+
46
+ * Cal Henderson - {Parsing Email Adresses in PHP}[http://iamcal.com/publish/articles/php/parsing_email]
47
+ * Tim Fletcher - {Ruby Translation}[http://tfletcher.com/lib/rfc822.rb]
48
+ * Dan Kubb[dan.kubb@autopilotmarketing.com]
49
+ * Ximon Eighteen[ximon.eightee@int.greenpeace.org]
50
+ * Thijs van der Vossen - validates_as_email[https://svn.greenpeace.org/repositories/rails_plugins/validates_as_email]
data/Rakefile ADDED
@@ -0,0 +1,79 @@
1
+ require 'rake/testtask'
2
+ require 'rake/rdoctask'
3
+ require 'rake/gempackagetask'
4
+ require 'rake/contrib/sshpublisher'
5
+
6
+ PKG_NAME = 'validates_as_email_address'
7
+ PKG_VERSION = '0.0.1'
8
+ PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
9
+ RUBY_FORGE_PROJECT = 'pluginaweek'
10
+
11
+ desc 'Default: run unit tests.'
12
+ task :default => :test
13
+
14
+ desc 'Test the validates_as_email_address plugin.'
15
+ Rake::TestTask.new(:test) do |t|
16
+ t.libs << 'lib'
17
+ t.pattern = 'test/**/*_test.rb'
18
+ t.verbose = true
19
+ end
20
+
21
+ desc 'Generate documentation for the validates_as_email_address plugin.'
22
+ Rake::RDocTask.new(:rdoc) do |rdoc|
23
+ rdoc.rdoc_dir = 'rdoc'
24
+ rdoc.title = 'ValidatesAsEmailAddress'
25
+ rdoc.options << '--line-numbers' << '--inline-source'
26
+ rdoc.rdoc_files.include('README')
27
+ rdoc.rdoc_files.include('lib/**/*.rb')
28
+ end
29
+
30
+ spec = Gem::Specification.new do |s|
31
+ s.name = PKG_NAME
32
+ s.version = PKG_VERSION
33
+ s.platform = Gem::Platform::RUBY
34
+ s.summary = 'Adds support for validating the format/length of email addresses'
35
+
36
+ s.files = FileList['{lib,test}/**/*'].to_a + %w(CHANGELOG init.rb LICENSE Rakefile README)
37
+ s.require_path = 'lib'
38
+ s.autorequire = 'validates_as_email_address'
39
+ s.has_rdoc = true
40
+ s.test_files = Dir['test/**/*_test.rb']
41
+
42
+ s.author = 'Aaron Pfeifer, Neil Abraham'
43
+ s.email = 'info@pluginaweek.org'
44
+ s.homepage = 'http://www.pluginaweek.org'
45
+ end
46
+
47
+ Rake::GemPackageTask.new(spec) do |p|
48
+ p.gem_spec = spec
49
+ p.need_tar = true
50
+ p.need_zip = true
51
+ end
52
+
53
+ desc 'Publish the beta gem'
54
+ task :pgem => [:package] do
55
+ Rake::SshFilePublisher.new('pluginaweek@pluginaweek.org', '/home/pluginaweek/gems.pluginaweek.org/gems', 'pkg', "#{PKG_FILE_NAME}.gem").upload
56
+ end
57
+
58
+ desc 'Publish the API documentation'
59
+ task :pdoc => [:rdoc] do
60
+ Rake::SshDirPublisher.new('pluginaweek@pluginaweek.org', "/home/pluginaweek/api.pluginaweek.org/#{PKG_NAME}", 'rdoc').upload
61
+ end
62
+
63
+ desc 'Publish the API docs and gem'
64
+ task :publish => [:pdoc, :release]
65
+
66
+ desc 'Publish the release files to RubyForge.'
67
+ task :release => [:gem, :package] do
68
+ require 'rubyforge'
69
+
70
+ ruby_forge = RubyForge.new
71
+ ruby_forge.login
72
+
73
+ %w( gem tgz zip ).each do |ext|
74
+ file = "pkg/#{PKG_FILE_NAME}.#{ext}"
75
+ puts "Releasing #{File.basename(file)}..."
76
+
77
+ ruby_forge.add_release(RUBY_FORGE_PROJECT, PKG_NAME, PKG_VERSION, file)
78
+ end
79
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'validates_as_email_address'
@@ -0,0 +1,104 @@
1
+ # The standard describing the format of email addresses
2
+ module RFC822
3
+ EmailAddress = begin
4
+ qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
5
+ dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
6
+ atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
7
+ quoted_pair = '\\x5c[\\x00-\\x7f]'
8
+ domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
9
+ quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
10
+ domain_ref = atom
11
+ sub_domain = "(?:#{domain_ref}|#{domain_literal})"
12
+ word = "(?:#{atom}|#{quoted_string})"
13
+ domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
14
+ local_part = "#{word}(?:\\x2e#{word})*"
15
+ addr_spec = "(#{local_part})\\x40(#{domain})"
16
+ pattern = /\A#{addr_spec}\z/
17
+ end
18
+ end
19
+
20
+ module PluginAWeek #:nodoc:
21
+ module Validates #:nodoc:
22
+ # Adds validations for email addresses
23
+ module EmailAddress
24
+ # The options that can be used when validating the format of the email address
25
+ EMAIL_FORMAT_OPTIONS = [
26
+ :wrong_format,
27
+ :allow_nil,
28
+ :on,
29
+ :if
30
+ ]
31
+
32
+ # The options that can be used when validating the length of the email address
33
+ EMAIL_LENGTH_OPTIONS = [
34
+ :minimum,
35
+ :maximum,
36
+ :is,
37
+ :within,
38
+ :in,
39
+ :too_long,
40
+ :too_short,
41
+ :wrong_length,
42
+ :allow_nil,
43
+ :on,
44
+ :if
45
+ ]
46
+
47
+ # Validates whether the value of the specific attribute matches against the
48
+ # RFC822 specificiation.
49
+ #
50
+ # class Person < ActiveRecord::Base
51
+ # validates_as_email_address :email, :on => :create
52
+ # end
53
+ #
54
+ # This will also validate that the email address is within the specification
55
+ # limits, specifically between 3 and 320 characters in length.
56
+ #
57
+ # Configuration options for length:
58
+ # * +minimum+ - The minimum size of the attribute
59
+ # * +maximum+ - The maximum size of the attribute
60
+ # * +is+ - The exact size of the attribute
61
+ # * +within+ - A range specifying the minimum and maximum size of the attribute
62
+ # * +in+ - A synonym(or alias) for :within
63
+ # * +too_long+ - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %d characters)")
64
+ # * +too_short+ - The error message if the attribute goes under the minimum (default is: "is too short (minimum is %d characters)")
65
+ # * +wrong_length+ - The error message if using the :is method and the attribute is the wrong size (default is: "is the wrong length (should be %d characters)")
66
+ #
67
+ # Configuration options for format:
68
+ # * +wrong_format+ - A custom error message (default is: "is an invalid email address")
69
+ #
70
+ # Configuration options for both length and format:
71
+ # * +allow_nil+ - Attribute may be nil; skip validation.
72
+ # * +on+ - Specifies when this validation is active (default is :save, other options :create, :update)
73
+ # * +if+ - Specifies a method, proc or string to call to determine if the validation should
74
+ # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
75
+ # method, proc or string should return or evaluate to a true or false value.
76
+ def validates_as_email_address(*attr_names)
77
+ configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
78
+ configuration.assert_valid_keys(EMAIL_FORMAT_OPTIONS | EMAIL_LENGTH_OPTIONS)
79
+ configuration.reverse_merge!(
80
+ :wrong_format => ActiveRecord::Errors.default_error_messages[:invalid_email]
81
+ )
82
+
83
+ # Add format validation
84
+ format_configuration = configuration.reject {|key, value| !EMAIL_FORMAT_OPTIONS.include?(key)}
85
+ format_configuration[:message] = format_configuration.delete(:wrong_format)
86
+ format_configuration[:with] = RFC822::EmailAddress
87
+ validates_format_of attr_names, format_configuration
88
+
89
+ # Add length validation
90
+ length_configuration = configuration.reject {|key, value| !EMAIL_LENGTH_OPTIONS.include?(key)}
91
+ length_configuration.reverse_merge!(:within => 3..320)
92
+ validates_length_of attr_names, length_configuration
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ ActiveRecord::Base.class_eval do
99
+ extend PluginAWeek::Validates::EmailAddress
100
+ end
101
+
102
+ ActiveRecord::Errors.default_error_messages.update(
103
+ :invalid_email => 'is an invalid email address'
104
+ )
@@ -0,0 +1,6 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+
5
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
6
+ require File.dirname(__FILE__) + '/../init'
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class User < ActiveRecord::Base
4
+ def self.columns
5
+ []
6
+ end
7
+
8
+ attr_accessor :email
9
+ validates_as_email_address :email
10
+ end
11
+
12
+ class ValidatesAsEmailAddressTest < Test::Unit::TestCase
13
+ def test_illegal_rfc822_email_address_should_not_be_valid
14
+ [
15
+ 'Max@Job 3:14',
16
+ 'Job@Book of Job',
17
+ 'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com',
18
+ ].each do |address|
19
+ assert !User.new(:email => address).valid?, "#{address} should be illegal."
20
+ end
21
+ end
22
+
23
+ def test_legal_rfc822_email_address_should_be_valid
24
+ [
25
+ 'test@example',
26
+ 'test@example.com',
27
+ 'test@example.co.uk',
28
+ '"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
29
+ 'me@[187.223.45.119]',
30
+ 'someone@123.com',
31
+ ].each do |address|
32
+ assert User.new(:email => address).valid?, "#{address} should be legal."
33
+ end
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: validates_as_email_address
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-09-26 00:00:00 -04:00
8
+ summary: Adds support for validating the format/length of email addresses
9
+ require_paths:
10
+ - lib
11
+ email: info@pluginaweek.org
12
+ homepage: http://www.pluginaweek.org
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: validates_as_email_address
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Aaron Pfeifer, Neil Abraham
31
+ files:
32
+ - lib/validates_as_email_address.rb
33
+ - test/test_helper.rb
34
+ - test/validates_as_email_address_test.rb
35
+ - CHANGELOG
36
+ - init.rb
37
+ - LICENSE
38
+ - Rakefile
39
+ - README
40
+ test_files:
41
+ - test/validates_as_email_address_test.rb
42
+ rdoc_options: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ requirements: []
51
+
52
+ dependencies: []
53
+