gbdev-validates_as_phone_number 0.7.3

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/CHANGELOG ADDED
@@ -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,37 @@
1
+ ValidatesAsPhoneNumber
2
+ ======================
3
+
4
+ This Ruby on Rails plugin/gem implements an ActiveRecord validation helper called
5
+ validates_as_phone_number which validates US 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
+
21
+
22
+ Usage:
23
+ ======
24
+ In your model file do something like:
25
+
26
+ class MyClass < ActiveRecord::Base
27
+ validates_presence_of :work_number
28
+ validates_as_phone_number :work_number
29
+ end
30
+
31
+ Tests:
32
+ ======
33
+ Some tests have been added.
34
+
35
+ License:
36
+ ========
37
+ See the LICENSE file
data/Rakefile ADDED
@@ -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-9876x123456',
37
+ '800-321-9876x123457',
38
+ '800-321-9876 x1',
39
+ '800-321-9876 x123456',
40
+ '800-321-9876 x123457',
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,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gbdev-validates_as_phone_number
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.3
5
+ platform: ruby
6
+ authors:
7
+ - Wes Hays
8
+ - John Dell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-08-15 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Rails gem that implements an ActiveRecord validation helper called validates_as_phone_number which validates U.S. phone numbers
18
+ email: gems@gbdev.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - CHANGELOG
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ - init.rb
31
+ - lib/validates_as_phone_number.rb
32
+ - test/validates_as_phone_number_test.rb
33
+ has_rdoc: false
34
+ homepage: http://github.com/gbdev/validates_as_phone_number
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: Rails gem to validate format of U.S. phone numbers
59
+ test_files:
60
+ - test/validates_as_phone_number_test.rb