validates_and_formats_phones 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,17 +1,27 @@
1
- ValidatesAndFormatsPhones
2
- =========================
1
+ = ValidatesAndFormatsPhones
3
2
 
4
3
  Allows you to easily format and validate phone numbers in your desired format
5
4
  (sensible defaults provided).
6
5
 
7
6
  Just add 'validates_and_formats_phones' to your ActiveRecord model.
8
7
 
9
- The default format is a 10-digit american phone number on the column 'phone'.
8
+ The default format is a 10-digit US phone number on the column 'phone'.
10
9
  But you can change all that...just read on.
11
10
 
11
+ == Installation
12
12
 
13
- Example
14
- =======
13
+ 1. Install it as a gem:
14
+
15
+ sudo gem install validates_and_formats_phones
16
+
17
+ 2. Then add it to your <tt>environment.rb</tt> file
18
+
19
+ config.gem 'validates_and_formats_phones'
20
+
21
+ 3. Add the method <tt>validates_and_formats_phones</tt> to your model
22
+
23
+
24
+ == Example
15
25
 
16
26
 
17
27
  Adding the validates_and_formats_phones method by itself gives you
@@ -38,16 +48,21 @@ You can pass your own fields like so:
38
48
  end
39
49
 
40
50
 
41
- Or your own formats like so:
51
+ Or your own formats by using a # symbol for number substitutions:
42
52
 
43
53
  class Person
44
- validates_and_formats_phones(8 => '####-####')
54
+ validates_and_formats_phones '####-####', '### ###-####'
45
55
  end
46
56
 
47
- or both, like so:
57
+ Note that the validation is simplistic. It counts the number of digits in the given field.
58
+ If the number of digits in the given field matches one of the formats, then the validation
59
+ passes and the format that has the same number of '#' symbols will be used to format
60
+ the phone number.
61
+
62
+ You can also format and specify fields, like so:
48
63
 
49
64
  class Person
50
- validates_and_formats_phones(:fax, mobile, 8 => '####-####')
65
+ validates_and_formats_phones :fax, mobile, '####-####'
51
66
  end
52
67
 
53
68
  Copyright (c) 2010 [Bernie Telles], released under the MIT license
data/README.rdoc ADDED
@@ -0,0 +1,68 @@
1
+ = ValidatesAndFormatsPhones
2
+
3
+ Allows you to easily format and validate phone numbers in your desired format
4
+ (sensible defaults provided).
5
+
6
+ Just add 'validates_and_formats_phones' to your ActiveRecord model.
7
+
8
+ The default format is a 10-digit US phone number on the column 'phone'.
9
+ But you can change all that...just read on.
10
+
11
+ == Installation
12
+
13
+ 1. Install it as a gem:
14
+
15
+ sudo gem install validates_and_formats_phones
16
+
17
+ 2. Then add it to your <tt>environment.rb</tt> file
18
+
19
+ config.gem 'validates_and_formats_phones'
20
+
21
+ 3. Add the method <tt>validates_and_formats_phones</tt> to your model
22
+
23
+
24
+ == Example
25
+
26
+
27
+ Adding the validates_and_formats_phones method by itself gives you
28
+ validation and formatting for the field :phone like so:
29
+
30
+
31
+ class Person
32
+ validates_and_formats_phones
33
+ end
34
+
35
+ > person = Person.new(:phone => '123 456 7890')
36
+ > person.save
37
+ > person.phone
38
+ > => '(123) 456-7890'
39
+ >
40
+ > person.phone = '123'
41
+ > person.save
42
+ > => false
43
+
44
+ You can pass your own fields like so:
45
+
46
+ class Person
47
+ validates_and_formats_phones :fax, :mobile
48
+ end
49
+
50
+
51
+ Or your own formats by using a # symbol for number substitutions:
52
+
53
+ class Person
54
+ validates_and_formats_phones '####-####', '### ###-####'
55
+ end
56
+
57
+ Note that the validation is simplistic. It counts the number of digits in the given field.
58
+ If the number of digits in the given field matches one of the formats, then the validation
59
+ passes and the format that has the same number of '#' symbols will be used to format
60
+ the phone number.
61
+
62
+ You can also format and specify fields, like so:
63
+
64
+ class Person
65
+ validates_and_formats_phones :fax, mobile, '####-####'
66
+ end
67
+
68
+ 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.1"
26
+ s.version = "0.0.2"
27
27
 
28
28
  s.author = "Bernie Telles"
29
29
  s.email = "btelles@gmail.com"
@@ -1,10 +1,17 @@
1
-
2
1
  String.class_eval do
3
2
  #Formats a phone according to the provided formats.
4
3
  # _formats_ is a hash whose keys are the number of digits
5
4
  # and values are the formats of the phone number.
6
5
  # For example {10 => "(###) ###-####"}
7
- def to_phone(formats = { 10 => "(###) ###-####"})
6
+ 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
8
15
  digits = scan(/\d/)
9
16
  final_string = formats[scan(/\d/).size].each_char.inject('') do |result, character|
10
17
  character == '#' ? result << digits.shift : result << character
@@ -13,5 +20,4 @@ String.class_eval do
13
20
 
14
21
  final_string
15
22
  end
16
-
17
23
  end
@@ -1,5 +1,5 @@
1
1
  module ValidatesAndFormatsPhones
2
- DEFAULT_FORMAT = { 10 => "(###) ###-####"}
2
+ DEFAULT_FORMAT = "(###) ###-####"
3
3
  def self.included(base)
4
4
  base.send :extend, ClassMethods
5
5
  end
@@ -8,39 +8,45 @@
8
8
 
9
9
 
10
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})"
11
+ formats = []
12
+ fields = []
13
+ args.each do |option|
14
+ option.to_s =~ /#/ ?
15
+ formats << option :
16
+ fields << option.to_sym
17
+ end
18
+ formats << DEFAULT_FORMAT if formats.empty?
19
+ fields << :phone if fields.empty?
20
+
21
+ send :include, InstanceMethods
22
+
23
+ validates_each *fields do |record, attr, value|
24
+ size_options = formats.collect {|a| a.count '#'}
25
+ unless value.blank? || size_options.include?(value.scan(/\d/).size)
26
+ if size_options.size > 1
27
+ last = size_options.pop
28
+ record.errors.add attr, "must have #{size_options.join(', ')} or #{last} digits."
29
+ else
30
+ record.errors.add attr, "must have #{size_options[0]} digits."
31
+ end
32
+ else
33
+ record.format_phone_fields(fields, formats)
34
+ end
29
35
  end
30
36
  end
31
37
  end
32
38
 
33
39
  module InstanceMethods
34
40
 
35
- def format_phone_fields(fields = [:phone], *args)
36
- options = args.empty? ? DEFAULT_FORMAT : args.extract_options!
41
+ def format_phone_fields(fields = [:phone], formats = [])
42
+ formats << DEFAULT_FORMAT if formats.empty?
37
43
  fields.each do |field_name|
38
- format_phone_field(field_name, options) unless send(field_name).blank?
44
+ format_phone_field(field_name, formats) unless send(field_name).blank?
39
45
  end
40
46
  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))
47
+ def format_phone_field(field_name, formats = [])
48
+ formats << DEFAULT_FORMAT if formats.empty?
49
+ self.send("#{field_name}=", self.send(field_name).to_s.to_phone(formats))
44
50
  end
45
51
  end
46
52
  end
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.1
4
+ version: 0.0.2
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-01-29 00:00:00 -05:00
12
+ date: 2010-02-01 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,6 +24,7 @@ extra_rdoc_files:
24
24
  files:
25
25
  - install.rb
26
26
  - uninstall.rb
27
+ - README.rdoc
27
28
  - init.rb
28
29
  - README
29
30
  - discover.rb