validates_and_formats_phones 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc
CHANGED
@@ -65,4 +65,15 @@ You can also format and specify fields, like so:
|
|
65
65
|
validates_and_formats_phones :fax, mobile, '####-####'
|
66
66
|
end
|
67
67
|
|
68
|
+
As a tiny added benefit, String is extended with a to_phone method.
|
69
|
+
|
70
|
+
> '123 456 7890'.to_phone
|
71
|
+
> => '(123) 456-7890'
|
72
|
+
>
|
73
|
+
> '12345678'.to_phone('####-####')
|
74
|
+
> => '1234-5678'
|
75
|
+
|
76
|
+
If you find any bugs, please report them at our github page:
|
77
|
+
|
78
|
+
http://github.com/btelles-validates_and_formats_phones
|
68
79
|
Copyright (c) 2010 [Bernie Telles], released under the MIT license
|
data/Rakefile
CHANGED
@@ -1,23 +1,36 @@
|
|
1
1
|
String.class_eval do
|
2
2
|
#Formats a phone according to the provided formats.
|
3
|
-
# _formats_ is
|
4
|
-
#
|
5
|
-
# For example {10 => "(###) ###-####"}
|
3
|
+
# _formats_ is an array whose values are a string
|
4
|
+
# with '#' signs indicating where digits should appear.
|
6
5
|
def to_phone(*args)
|
7
|
-
|
8
|
-
formats = {10 => '(###) ###-####'}
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
args.empty? ?
|
7
|
+
formats = {10 => '(###) ###-####'} :
|
8
|
+
formats = format_sizes(args)
|
9
|
+
|
10
|
+
to_formatted_number(formats)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def to_formatted_number(formats = {})
|
15
16
|
digits = scan(/\d/)
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
if formats[digits.size]
|
18
|
+
final_string = formats[digits.size].each_char.inject('') do |result, character|
|
19
|
+
character == '#' ? result << digits.shift : result << character
|
20
|
+
result
|
21
|
+
end
|
22
|
+
final_string
|
23
|
+
else
|
24
|
+
self
|
19
25
|
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
20
29
|
|
21
|
-
|
30
|
+
def format_sizes(formats_array)
|
31
|
+
formats_array.flatten.inject({}) do |all_formats, format|
|
32
|
+
all_formats[format.count('#')] = format
|
33
|
+
all_formats
|
34
|
+
end
|
22
35
|
end
|
23
36
|
end
|
@@ -8,18 +8,7 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
def validates_and_formats_phones(*args)
|
11
|
-
formats =
|
12
|
-
fields = []
|
13
|
-
args.each do |option|
|
14
|
-
if option.to_s =~ /#/
|
15
|
-
formats << option
|
16
|
-
else
|
17
|
-
fields << option.to_sym
|
18
|
-
end
|
19
|
-
end
|
20
|
-
formats << DEFAULT_FORMAT if formats.empty?
|
21
|
-
fields << :phone if fields.empty?
|
22
|
-
|
11
|
+
formats, fields = extract_formats_and_fields(args)
|
23
12
|
send :include, InstanceMethods
|
24
13
|
|
25
14
|
validates_each *fields do |record, attr, value|
|
@@ -36,6 +25,21 @@
|
|
36
25
|
end
|
37
26
|
end
|
38
27
|
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def extract_formats_and_fields(formats_and_fields)
|
32
|
+
formats = []
|
33
|
+
fields = []
|
34
|
+
formats_and_fields.each do |option|
|
35
|
+
option.to_s =~ /#/ ?
|
36
|
+
formats << option :
|
37
|
+
fields << option.to_sym
|
38
|
+
end
|
39
|
+
formats << DEFAULT_FORMAT if formats.empty?
|
40
|
+
fields << :phone if fields.empty?
|
41
|
+
[formats, fields]
|
42
|
+
end
|
39
43
|
end
|
40
44
|
|
41
45
|
module InstanceMethods
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_and_formats_phones
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernie Telles
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-03 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -22,13 +22,13 @@ extensions: []
|
|
22
22
|
extra_rdoc_files:
|
23
23
|
- README
|
24
24
|
files:
|
25
|
-
- install.rb
|
26
|
-
- uninstall.rb
|
27
|
-
- README.rdoc
|
28
25
|
- init.rb
|
26
|
+
- Rakefile
|
29
27
|
- README
|
28
|
+
- install.rb
|
30
29
|
- discover.rb
|
31
|
-
-
|
30
|
+
- uninstall.rb
|
31
|
+
- README.rdoc
|
32
32
|
- lib/validates_and_formats_phones/validates_and_formats_phones.rb
|
33
33
|
- lib/validates_and_formats_phones/phone_formatter.rb
|
34
34
|
- lib/validates_and_formats_phones.rb
|