validates_and_formats_phones 0.0.3 → 0.0.4

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.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
@@ -23,7 +23,7 @@ PKG_FILES = FileList[
23
23
 
24
24
  spec = Gem::Specification.new do |s|
25
25
  s.name = "validates_and_formats_phones"
26
- s.version = "0.0.3"
26
+ s.version = "0.0.4"
27
27
 
28
28
  s.author = "Bernie Telles"
29
29
  s.email = "btelles@gmail.com"
@@ -1,23 +1,36 @@
1
1
  String.class_eval do
2
2
  #Formats a phone according to the provided formats.
3
- # _formats_ is a hash whose keys are the number of digits
4
- # and values are the formats of the phone number.
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
- if args.empty?
8
- formats = {10 => '(###) ###-####'}
9
- else
10
- formats = args.flatten.inject({}) do |all_formats, format|
11
- all_formats[format.count('#')] = format
12
- all_formats
13
- end
14
- end
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
- final_string = formats[scan(/\d/).size].each_char.inject('') do |result, character|
17
- character == '#' ? result << digits.shift : result << character
18
- result
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
- final_string
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.3
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-02 00:00:00 -05:00
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
- - Rakefile
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