validates_and_formats_phones 0.0.1
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/README +53 -0
- data/Rakefile +44 -0
- data/discover.rb +6 -0
- data/init.rb +2 -0
- data/install.rb +1 -0
- data/lib/validates_and_formats_phones.rb +3 -0
- data/lib/validates_and_formats_phones/phone_formatter.rb +17 -0
- data/lib/validates_and_formats_phones/validates_and_formats_phones.rb +48 -0
- data/rails/init.rb +1 -0
- data/tasks/validates_and_formats_phones_tasks.rake +4 -0
- data/uninstall.rb +1 -0
- metadata +65 -0
data/README
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
ValidatesAndFormatsPhones
|
|
2
|
+
=========================
|
|
3
|
+
|
|
4
|
+
Allows you to easily format and validate phone numbers in your desired format
|
|
5
|
+
(sensible defaults provided).
|
|
6
|
+
|
|
7
|
+
Just add 'validates_and_formats_phones' to your ActiveRecord model.
|
|
8
|
+
|
|
9
|
+
The default format is a 10-digit american phone number on the column 'phone'.
|
|
10
|
+
But you can change all that...just read on.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
Example
|
|
14
|
+
=======
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
Adding the validates_and_formats_phones method by itself gives you
|
|
18
|
+
validation and formatting for the field :phone like so:
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Person
|
|
22
|
+
validates_and_formats_phones
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
> person = Person.new(:phone => '123 456 7890')
|
|
26
|
+
> person.save
|
|
27
|
+
> person.phone
|
|
28
|
+
> => '(123) 456-7890'
|
|
29
|
+
>
|
|
30
|
+
> person.phone = '123'
|
|
31
|
+
> person.save
|
|
32
|
+
> => false
|
|
33
|
+
|
|
34
|
+
You can pass your own fields like so:
|
|
35
|
+
|
|
36
|
+
class Person
|
|
37
|
+
validates_and_formats_phones :fax, :mobile
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
Or your own formats like so:
|
|
42
|
+
|
|
43
|
+
class Person
|
|
44
|
+
validates_and_formats_phones(8 => '####-####')
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
or both, like so:
|
|
48
|
+
|
|
49
|
+
class Person
|
|
50
|
+
validates_and_formats_phones(:fax, mobile, 8 => '####-####')
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
Copyright (c) 2010 [Bernie Telles], released under the MIT license
|
data/Rakefile
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake'
|
|
3
|
+
require 'rake/gempackagetask'
|
|
4
|
+
require 'spec/rake/spectask'
|
|
5
|
+
|
|
6
|
+
desc 'Default: run specs.'
|
|
7
|
+
task :default => :spec
|
|
8
|
+
|
|
9
|
+
desc 'Run the specs'
|
|
10
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
|
11
|
+
t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
|
|
12
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
PKG_FILES = FileList[
|
|
16
|
+
'[a-zA-Z]*',
|
|
17
|
+
'generators/**/*',
|
|
18
|
+
'lib/**/*',
|
|
19
|
+
'rails/**/*',
|
|
20
|
+
'tasks/**/*',
|
|
21
|
+
'test/**/*'
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
spec = Gem::Specification.new do |s|
|
|
25
|
+
s.name = "validates_and_formats_phones"
|
|
26
|
+
s.version = "0.0.1"
|
|
27
|
+
|
|
28
|
+
s.author = "Bernie Telles"
|
|
29
|
+
s.email = "btelles@gmail.com"
|
|
30
|
+
s.homepage = "http://github.com/btelles/validates_and_formats_phones"
|
|
31
|
+
s.platform = Gem::Platform::RUBY
|
|
32
|
+
s.summary = "Allows you to easily format and validate phone numbers in any format you desire (sensible defaults provided)."
|
|
33
|
+
s.files = PKG_FILES.to_a
|
|
34
|
+
s.require_path = "lib"
|
|
35
|
+
|
|
36
|
+
s.has_rdoc = false
|
|
37
|
+
s.extra_rdoc_files = ["README"]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
desc 'Turn this plugin into a gem.'
|
|
41
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
|
42
|
+
pkg.gem_spec = spec
|
|
43
|
+
end
|
|
44
|
+
|
data/discover.rb
ADDED
data/init.rb
ADDED
data/install.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Install hook code here
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
String.class_eval do
|
|
3
|
+
#Formats a phone according to the provided formats.
|
|
4
|
+
# _formats_ is a hash whose keys are the number of digits
|
|
5
|
+
# and values are the formats of the phone number.
|
|
6
|
+
# For example {10 => "(###) ###-####"}
|
|
7
|
+
def to_phone(formats = { 10 => "(###) ###-####"})
|
|
8
|
+
digits = scan(/\d/)
|
|
9
|
+
final_string = formats[scan(/\d/).size].each_char.inject('') do |result, character|
|
|
10
|
+
character == '#' ? result << digits.shift : result << character
|
|
11
|
+
result
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
final_string
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
module ValidatesAndFormatsPhones
|
|
2
|
+
DEFAULT_FORMAT = { 10 => "(###) ###-####"}
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.send :extend, ClassMethods
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
module ClassMethods
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def validates_and_formats_phones(*args)
|
|
11
|
+
options = args.extract_options!
|
|
12
|
+
options = DEFAULT_FORMAT if options.empty?
|
|
13
|
+
args.empty? ? field_names = [:phone] : field_names = args
|
|
14
|
+
|
|
15
|
+
send :include, InstanceMethods
|
|
16
|
+
|
|
17
|
+
validates_each *field_names do |record, attr, value|
|
|
18
|
+
size_options = options.keys
|
|
19
|
+
unless value.blank? || size_options.include?(value.scan(/\d/).size)
|
|
20
|
+
if size_options.size > 1
|
|
21
|
+
last = size_options.pop
|
|
22
|
+
record.errors.add attr, "must have #{size_options.join(', ')} or #{last} digits."
|
|
23
|
+
else
|
|
24
|
+
record.errors.add attr, "must have #{size_options[0]} digits."
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
28
|
+
after_validation "format_phone_fields(#{field_names.inspect}, #{options.inspect})"
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
module InstanceMethods
|
|
34
|
+
|
|
35
|
+
def format_phone_fields(fields = [:phone], *args)
|
|
36
|
+
options = args.empty? ? DEFAULT_FORMAT : args.extract_options!
|
|
37
|
+
fields.each do |field_name|
|
|
38
|
+
format_phone_field(field_name, options) unless send(field_name).blank?
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
def format_phone_field(field_name, *args)
|
|
42
|
+
options = args.extract_options! || DEFAULT_FORMAT
|
|
43
|
+
self.send("#{field_name}=", self.send(field_name).to_s.to_phone(options))
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
ActiveRecord::Base.send :include, ValidatesAndFormatsPhones
|
data/rails/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'validates_and_formats_phones'
|
data/uninstall.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: validates_and_formats_phones
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Bernie Telles
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2010-01-29 00:00:00 -05:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description:
|
|
17
|
+
email: btelles@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- README
|
|
24
|
+
files:
|
|
25
|
+
- install.rb
|
|
26
|
+
- uninstall.rb
|
|
27
|
+
- init.rb
|
|
28
|
+
- README
|
|
29
|
+
- discover.rb
|
|
30
|
+
- Rakefile
|
|
31
|
+
- lib/validates_and_formats_phones/validates_and_formats_phones.rb
|
|
32
|
+
- lib/validates_and_formats_phones/phone_formatter.rb
|
|
33
|
+
- lib/validates_and_formats_phones.rb
|
|
34
|
+
- rails/init.rb
|
|
35
|
+
- tasks/validates_and_formats_phones_tasks.rake
|
|
36
|
+
has_rdoc: true
|
|
37
|
+
homepage: http://github.com/btelles/validates_and_formats_phones
|
|
38
|
+
licenses: []
|
|
39
|
+
|
|
40
|
+
post_install_message:
|
|
41
|
+
rdoc_options: []
|
|
42
|
+
|
|
43
|
+
require_paths:
|
|
44
|
+
- lib
|
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - ">="
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: "0"
|
|
50
|
+
version:
|
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: "0"
|
|
56
|
+
version:
|
|
57
|
+
requirements: []
|
|
58
|
+
|
|
59
|
+
rubyforge_project:
|
|
60
|
+
rubygems_version: 1.3.5
|
|
61
|
+
signing_key:
|
|
62
|
+
specification_version: 3
|
|
63
|
+
summary: Allows you to easily format and validate phone numbers in any format you desire (sensible defaults provided).
|
|
64
|
+
test_files: []
|
|
65
|
+
|