validates_and_formats_phones 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile
CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'rake/gempackagetask'
|
4
4
|
require 'spec/rake/spectask'
|
5
|
+
require 'metric_fu'
|
5
6
|
|
6
7
|
desc 'Default: run specs.'
|
7
8
|
task :default => :spec
|
@@ -23,7 +24,7 @@ PKG_FILES = FileList[
|
|
23
24
|
|
24
25
|
spec = Gem::Specification.new do |s|
|
25
26
|
s.name = "validates_and_formats_phones"
|
26
|
-
s.version = "0.0.
|
27
|
+
s.version = "0.0.5"
|
27
28
|
|
28
29
|
s.author = "Bernie Telles"
|
29
30
|
s.email = "btelles@gmail.com"
|
@@ -5,7 +5,7 @@ String.class_eval do
|
|
5
5
|
def to_phone(*args)
|
6
6
|
args.empty? ?
|
7
7
|
formats = {10 => '(###) ###-####'} :
|
8
|
-
formats =
|
8
|
+
formats = args.flatten.inject({}) { |all_formats, format| all_formats.merge({format.count('#') => format}) }
|
9
9
|
|
10
10
|
to_formatted_number(formats)
|
11
11
|
end
|
@@ -14,8 +14,9 @@ String.class_eval do
|
|
14
14
|
|
15
15
|
def to_formatted_number(formats = {})
|
16
16
|
digits = scan(/\d/)
|
17
|
-
|
18
|
-
|
17
|
+
total_digits = digits.size
|
18
|
+
if formats[total_digits]
|
19
|
+
final_string = formats[total_digits].each_char.inject('') do |result, character|
|
19
20
|
character == '#' ? result << digits.shift : result << character
|
20
21
|
result
|
21
22
|
end
|
@@ -25,12 +26,4 @@ String.class_eval do
|
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
28
|
-
private
|
29
|
-
|
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
|
35
|
-
end
|
36
29
|
end
|
@@ -2,44 +2,44 @@
|
|
2
2
|
DEFAULT_FORMAT = "(###) ###-####"
|
3
3
|
def self.included(base)
|
4
4
|
base.send :extend, ClassMethods
|
5
|
+
base.send :include, InstanceMethods
|
5
6
|
end
|
6
7
|
|
7
|
-
|
8
|
+
def self.extract_formats_and_fields(formats_and_fields)
|
9
|
+
formats = []
|
10
|
+
fields = []
|
11
|
+
formats_and_fields.each do |option|
|
12
|
+
option.to_s =~ /#/ ?
|
13
|
+
formats << option :
|
14
|
+
fields << option.to_sym
|
15
|
+
end
|
16
|
+
formats << DEFAULT_FORMAT if formats.empty?
|
17
|
+
fields << :phone if fields.empty?
|
18
|
+
[formats, fields]
|
19
|
+
end
|
8
20
|
|
21
|
+
module ClassMethods
|
9
22
|
|
10
23
|
def validates_and_formats_phones(*args)
|
11
|
-
formats, fields = extract_formats_and_fields(args)
|
12
|
-
|
24
|
+
formats, fields = ValidatesAndFormatsPhones.extract_formats_and_fields(args)
|
25
|
+
|
26
|
+
size_options = formats.collect {|format| format.count '#'}
|
13
27
|
|
14
28
|
validates_each *fields do |record, attr, value|
|
15
|
-
size_options = formats.collect {|a| a.count '#'}
|
16
29
|
unless value.blank? || size_options.include?(value.scan(/\d/).size)
|
17
30
|
if size_options.size > 1
|
18
31
|
last = size_options.pop
|
19
|
-
|
32
|
+
message = "must have #{size_options.join(', ')} or #{last} digits."
|
20
33
|
else
|
21
|
-
|
34
|
+
message = "must have #{size_options[0]} digits."
|
22
35
|
end
|
36
|
+
record.errors.add attr, message
|
23
37
|
else
|
24
38
|
record.format_phone_field(attr, formats)
|
25
39
|
end
|
26
40
|
end
|
27
41
|
end
|
28
42
|
|
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
|
43
43
|
end
|
44
44
|
|
45
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.5
|
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-04 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|