validates_as_phone_number 0.7.5

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.
@@ -0,0 +1,10 @@
1
+ 0.7.3 - Aug 15th, 2008
2
+ * Gem'ified plugin
3
+ * Move to github
4
+
5
+ 0.2.0 - Jan 30th, 2008
6
+ * RegEx was updated to accomodate different delimiters and extensions
7
+ * length of extension increased to allow 7 digits
8
+
9
+ 0.1.0 - Aug 25th, 2006
10
+ * First release
data/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Licensing terms follow:
2
+
3
+ This software is placed under the Creative Commons Attribution-Share ALike 3.0 Unported License
4
+
5
+ http://creativecommons.org/licenses/by-sa/3.0/
data/README ADDED
@@ -0,0 +1,43 @@
1
+ ValidatesAsPhoneNumber
2
+ ======================
3
+
4
+ This Ruby on Rails plugin/gem implements an ActiveRecord validation helper called
5
+ validates_as_phone_number which validates U.S. phone numbers. The helper acts as
6
+ if validates_format_of was used with a regular expression that defines:
7
+
8
+ (000) 000 0000
9
+ (000) 000-0000
10
+ 000.000.0000
11
+ 000-000-0000
12
+ 000-0000
13
+ 000-000-0000x000 (extension can be up to 7 digits)
14
+ 000-000-0000 x1234567 (extension can be up to 7 digits with or without leading space before 'x')
15
+ 000-0000x000 (extension can be up to 7 digits)
16
+
17
+
18
+ Installation:
19
+ =============
20
+ gem sources -a http://gems.github.com
21
+
22
+ Install the gem(s):
23
+ sudo gem install gbdev-validates_as_phone_number
24
+
25
+ Add to environment.rb initializer block:
26
+ config.gem 'gbdev-validates_as_phone_number', :version => '>=0.7.5', :lib => 'validates_as_phone_number', :source => 'http://gems.github.com'
27
+
28
+
29
+ Usage:
30
+ ======
31
+ In your model file do something like:
32
+
33
+ class MyClass < ActiveRecord::Base
34
+ validates_as_phone_number :work_phone, :message => 'Invalid work phone number format', :allow_nil => true
35
+ end
36
+
37
+ Tests:
38
+ ======
39
+ Some tests have been added.
40
+
41
+ License:
42
+ ========
43
+ See the LICENSE file.
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the validates_as_phone_number plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the validates_as_phone_number plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ValidatesAsPhoneNumber'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'validates_as_phone_number'
@@ -0,0 +1,34 @@
1
+ #
2
+ # Made this a plugin because there was not one out there yet. (yes I did an exhaustive search)
3
+ #
4
+ # Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
5
+ # http://creativecommons.org/licenses/by-sa/2.5/
6
+ #
7
+
8
+ # Validation helper for ActiveRecord derived objects that cleanly and simply
9
+ # allows the model to check if the given string is a valid US phone number
10
+ # in the form 000-000-0000, 000-0000 (delimiter may be space, hyphen or period and area code can have parentheses).
11
+ # For extensions: 000-000-0000 x12345 or 000-0000 x1234567
12
+ # Extensions can be up to 7 digits (valid with or without leading space before 'x')
13
+ #
14
+
15
+ module ActiveRecord
16
+ module Validations
17
+ module ClassMethods
18
+ def validates_as_phone_number(*attr_names)
19
+ regExpStr = '(([0-9]{3})|((\()([0-9]{3})(\))))?([\s\.-])?([0-9]{3})([\s\.-])?([0-9]{4})([\s\.-])?([x] ?[0-9]{1,7})?'
20
+ configuration = {
21
+ :message => 'is an invalid phone number',
22
+ :with => /^#{regExpStr}$/,
23
+ :allow_nil => false }
24
+
25
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
26
+
27
+ configuration[:with] = /^(#{regExpStr})?$/ if configuration[:allow_nil]
28
+
29
+ validates_format_of attr_names, configuration
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,78 @@
1
+ require 'test/unit'
2
+
3
+ begin
4
+ require File.dirname(__FILE__) + '/../../../../config/boot'
5
+ require 'active_record'
6
+ require 'validates_as_phone_number'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ require 'activerecord'
10
+ require File.dirname(__FILE__) + '/../lib/validates_as_phone_number'
11
+ end
12
+
13
+ class TestRecord < ActiveRecord::Base
14
+ def self.columns; []; end
15
+ attr_accessor :phone_number
16
+ validates_as_phone_number :phone_number, :allow_nil => false # false is default
17
+ end
18
+
19
+ class TestRecord2 < ActiveRecord::Base
20
+ def self.columns; []; end
21
+ attr_accessor :phone_number
22
+ validates_as_phone_number :phone_number, :allow_nil => true
23
+ end
24
+
25
+ class ValidatesAsPhoneNumberTest < Test::Unit::TestCase
26
+
27
+ def test_valid_phone_numbers
28
+ phone_numbers = [
29
+ '3219876',
30
+ '800.321.9876',
31
+ '8003219876',
32
+ '321-9876',
33
+ '800-321-9876',
34
+ '321-9876x12345',
35
+ '800-321-9876x1',
36
+ '800-321-9876x 1',
37
+ '800-321-9876x1234567',
38
+ '800-321-9876 x1',
39
+ '800-321-9876 x 1',
40
+ '800-321-9876 x1234567',
41
+ '(800)-321-9876',
42
+ '(800) 321-9876'
43
+ ]
44
+ phone_numbers.each do |number|
45
+ assert TestRecord.new(:phone_number => number).valid?, "#{number} should be valid."
46
+ end
47
+ end
48
+
49
+ def test_invalid_phone_numbers
50
+ phone_numbers = [
51
+ '(800 321-9876',
52
+ '800) 321-9876',
53
+ '321-9876x1234567890234',
54
+ '800-321-9876x',
55
+ '800-321-98761',
56
+ '800-321-9876 x',
57
+ '800-321-9876 xa',
58
+ '800a321-9876',
59
+ '800-321-9876x12345678',
60
+ '800-321-9876 x12345678'
61
+ ]
62
+ phone_numbers.each do |number|
63
+ assert !TestRecord.new(:phone_number => number).valid?, "#{number} should be invalid."
64
+ end
65
+ end
66
+
67
+ def test_blank_phone_number_not_allowed
68
+ assert !TestRecord.new(:phone_number => nil).valid?, "Blank phone number should be invalid."
69
+ assert !TestRecord.new(:phone_number => '').valid?, "Blank phone number should be invalid."
70
+ end
71
+
72
+ def test_blank_phone_number_allowed
73
+ assert TestRecord2.new(:phone_number => nil).valid?, "Blank phone number should be valid."
74
+ assert TestRecord2.new(:phone_number => '').valid?, "Blank phone number should be valid."
75
+ end
76
+
77
+ end
78
+
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_as_phone_number
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.5
5
+ platform: ruby
6
+ authors:
7
+ - Wes Hays
8
+ - John Dell
9
+ - Josh Susser
10
+ - Brandon Keene
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+
15
+ date: 2009-01-27 00:00:00 -08:00
16
+ default_executable:
17
+ dependencies: []
18
+
19
+ description: Rails gem that implements an ActiveRecord validation helper called validates_as_phone_number which validates U.S. phone numbers
20
+ email:
21
+ - gems@gbdev.com
22
+ - opensource@pivotallabs.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - CHANGELOG
31
+ - LICENSE
32
+ - README
33
+ - Rakefile
34
+ - init.rb
35
+ - lib/validates_as_phone_number.rb
36
+ - test/validates_as_phone_number_test.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/gbdev/validates_as_phone_number
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Rails gem to validate format of U.S. phone numbers
65
+ test_files:
66
+ - test/validates_as_phone_number_test.rb