validates_and_formats_phones 0.0.7 → 0.0.8

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.
@@ -0,0 +1,4 @@
1
+ *.swp
2
+ *.log
3
+ *.db
4
+ *.swo
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Rakefile CHANGED
@@ -1,45 +1,2 @@
1
- require 'rubygems'
2
- require 'rake'
3
- require 'rake/gempackagetask'
4
- require 'spec/rake/spectask'
5
- require 'metric_fu'
6
-
7
- desc 'Default: run specs.'
8
- task :default => :spec
9
-
10
- desc 'Run the specs'
11
- Spec::Rake::SpecTask.new(:spec) do |t|
12
- t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
13
- t.spec_files = FileList['spec/**/*_spec.rb']
14
- end
15
-
16
- PKG_FILES = FileList[
17
- '[a-zA-Z]*',
18
- 'generators/**/*',
19
- 'lib/**/*',
20
- 'rails/**/*',
21
- 'tasks/**/*',
22
- 'test/**/*'
23
- ]
24
-
25
- spec = Gem::Specification.new do |s|
26
- s.name = "validates_and_formats_phones"
27
- s.version = "0.0.7"
28
-
29
- s.author = "Bernie Telles"
30
- s.email = "btelles@gmail.com"
31
- s.homepage = "http://github.com/btelles/validates_and_formats_phones"
32
- s.platform = Gem::Platform::RUBY
33
- s.summary = "Allows you to easily format and validate phone numbers in any format you desire (sensible defaults provided)."
34
- s.files = PKG_FILES.to_a
35
- s.require_path = "lib"
36
-
37
- s.has_rdoc = false
38
- s.extra_rdoc_files = ["README"]
39
- end
40
-
41
- desc 'Turn this plugin into a gem.'
42
- Rake::GemPackageTask.new(spec) do |pkg|
43
- pkg.gem_spec = spec
44
- end
45
-
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -1,3 +1,4 @@
1
1
  require 'active_record'
2
2
  require 'validates_and_formats_phones/phone_formatter'
3
3
  require 'validates_and_formats_phones/validates_and_formats_phones'
4
+ require 'validates_and_formats_phones/railtie'
@@ -0,0 +1,17 @@
1
+ module ValidatesAndFormatsPhones
2
+ if defined? Rails::Railtie
3
+ class Railtie < Rails::Railtie
4
+ initializer "validates_and_formats_phones.extend_activerecord_base" do |app|
5
+ ActiveSupport.on_load(:active_record) do
6
+ ValidatesAndFormatsPhones::Railtie.insert
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ class Railtie
13
+ def self.insert
14
+ ActiveRecord::Base.send(:include, ValidatesAndFormatsPhones)
15
+ end
16
+ end
17
+ end
@@ -1,56 +1,54 @@
1
- module ValidatesAndFormatsPhones
2
- DEFAULT_FORMAT = "(###) ###-####"
3
- def self.included(base)
4
- base.send :extend, ClassMethods
5
- base.send :include, InstanceMethods
6
- end
1
+ module ValidatesAndFormatsPhones
2
+ DEFAULT_FORMAT = "(###) ###-####"
3
+ def self.included(base)
4
+ base.send :extend, ClassMethods
5
+ base.send :include, InstanceMethods
6
+ end
7
7
 
8
- def self.extract_formats_and_fields(formats_and_fields)
9
- options = {:on => :save, :allow_nil => false}
10
- options.merge!(formats_and_fields.extract_options!)
11
- formats = []
12
- fields = []
13
- formats_and_fields.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
- [formats, fields, options]
8
+ def self.extract_formats_and_fields(formats_and_fields)
9
+ options = {:on => :save, :allow_nil => false}
10
+ options.merge!(formats_and_fields.extract_options!)
11
+ formats = []
12
+ fields = []
13
+ formats_and_fields.each do |option|
14
+ option.to_s =~ /#/ ?
15
+ formats << option :
16
+ fields << option.to_sym
21
17
  end
18
+ formats << DEFAULT_FORMAT if formats.empty?
19
+ fields << :phone if fields.empty?
20
+ [formats, fields, options]
21
+ end
22
22
 
23
- module ClassMethods
23
+ module ClassMethods
24
24
 
25
- def validates_and_formats_phones(*args)
26
- formats, fields, options = ValidatesAndFormatsPhones.extract_formats_and_fields(args)
25
+ def validates_and_formats_phones(*args)
26
+ formats, fields, options = ValidatesAndFormatsPhones.extract_formats_and_fields(args)
27
27
 
28
- size_options = formats.collect {|format| format.count '#'}
28
+ size_options = formats.collect {|format| format.count '#'}
29
29
 
30
- validates_each(fields, options) do |record, attr, value|
31
- unless value.blank? || size_options.include?(value.scan(/\d/).size)
32
- if size_options.size > 1
33
- last = size_options.pop
34
- message = "must have #{size_options.join(', ')} or #{last} digits."
35
- else
36
- message = "must have #{size_options[0]} digits."
37
- end
38
- record.errors.add attr, message
30
+ validates_each(*args) do |record, attr, value|
31
+ unless (value.present? && size_options.include?(value.scan(/\d/).size)) || options[:allow_nil]
32
+ if size_options.size > 1
33
+ last = size_options.pop
34
+ message = "must have #{size_options.join(', ')} or #{last} digits."
39
35
  else
40
- record.format_phone_field(attr, formats)
36
+ message = "must have #{size_options[0]} digits."
41
37
  end
38
+ record.errors.add attr, message
39
+ else
40
+ record.format_phone_field(attr, formats)
42
41
  end
43
42
  end
44
-
45
43
  end
46
44
 
47
- module InstanceMethods
45
+ end
48
46
 
49
- def format_phone_field(field_name, formats = [])
50
- formats << DEFAULT_FORMAT if formats.empty?
51
- self.send("#{field_name}=", self.send(field_name).to_s.to_phone(formats)) unless send(field_name).blank?
52
- end
47
+ module InstanceMethods
48
+
49
+ def format_phone_field(field_name, formats = [])
50
+ formats << DEFAULT_FORMAT if formats.empty?
51
+ self.send("#{field_name}=", self.send(field_name).to_s.to_phone(formats)) unless send(field_name).blank?
53
52
  end
54
53
  end
55
-
56
- ActiveRecord::Base.send :include, ValidatesAndFormatsPhones
54
+ end
@@ -0,0 +1,3 @@
1
+ module ValidatesAndFormatsPhones
2
+ VERSION= '0.0.8'
3
+ end
@@ -0,0 +1,3 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: vendor/plugins/validates_and_formats_phones/spec/validates_phones_plugin.sqlite3.db
@@ -0,0 +1,18 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+
3
+ create_table :phones, :force => true do |t|
4
+ t.string :phone
5
+ end
6
+
7
+ create_table :options_phones, :force => true do |t|
8
+ t.string :other_phone
9
+ t.string :fax
10
+ end
11
+
12
+ create_table :if_options, :force => true do |t|
13
+ t.string :phone_if_true
14
+ t.string :phone_if_false
15
+ t.string :phone_unless_true
16
+ t.string :phone_unless_false
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "String#to_formatted_number" do
4
+ it 'should accept no arguments and an invalid phone number and return same string' do
5
+ 'aoeu'.to_phone.should == 'aoeu'
6
+ end
7
+ it 'should accept no arguments and a valid phone number and return a U.S. phone string' do
8
+ '1234567890'.to_phone.should == '(123) 456-7890'
9
+ end
10
+ it "should accept a number escaped by '#' and return a formatted string" do
11
+ 'a123b123b12'.to_phone('####-####').should == '1231-2312'
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,16 @@
1
+ begin
2
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
+ #require 'validates_and_formats_phones'
4
+ rescue LoadError
5
+ puts "You need to install rspec in your base app"
6
+ exit
7
+ end
8
+
9
+ plugin_spec_dir = File.dirname(__FILE__)
10
+ ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
11
+
12
+ databases = YAML::load(IO.read(plugin_spec_dir + "/db/database.yml"))
13
+
14
+ ActiveRecord::Base.establish_connection(databases[ENV["DB"] || "sqlite3"])
15
+ load(File.join(plugin_spec_dir, "db", "schema.rb"))
16
+
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ class Phone < ActiveRecord::Base
4
+ validates_and_formats_phones
5
+ end
6
+ class OptionsPhone < ActiveRecord::Base
7
+ validates_and_formats_phones :other_phone, :fax,
8
+ '####-####',
9
+ '(###) ###-####'
10
+ end
11
+ class IfOption < ActiveRecord::Base
12
+ validates_and_formats_phones :phone_if_true, :if => :true_function
13
+ validates_and_formats_phones :phone_if_false, :if => :false_function
14
+ validates_and_formats_phones :phone_unless_false, :unless => :false_function
15
+ validates_and_formats_phones :phone_unless_true, :unless => :true_function
16
+
17
+ def true_function
18
+ true
19
+ end
20
+ def false_function
21
+ false
22
+ end
23
+ end
24
+
25
+ describe "ValidatesAndFormatsPhones" do
26
+
27
+ valid_phones = ['1234567890', '123-456-7890', '(123) 456-7890', 'o123.456-7890']
28
+ invalid_phones = ['01234567890', '12-456-7890', '(123) 456-8', 'o1123.456-7890', 'invalid']
29
+
30
+ valid_phones.each do |phone|
31
+ it "allows saving of valid phone #{phone}" do
32
+ lambda {
33
+ Phone.create!(:phone => phone)
34
+ }.should_not raise_error
35
+
36
+ Phone.create!(:phone => phone).should be_instance_of(Phone) end
37
+ it "formats the phone according to a reasonable default option" do
38
+ new_phone = Phone.create!(:phone => phone)
39
+ new_phone.phone.should == "(123) 456-7890"
40
+ end
41
+ end
42
+
43
+ describe "accepts different phone formats" do
44
+ it "by passing validation" do
45
+ lambda {
46
+ OptionsPhone.create!(:other_phone => '12345678')
47
+ }.should_not raise_error
48
+ end
49
+ it "by formatting the string in the new format" do
50
+ phone = OptionsPhone.create!(:other_phone => '12345678')
51
+ phone.other_phone.should == "1234-5678"
52
+ end
53
+ end
54
+
55
+ describe "accepts more phone fields" do
56
+ it ". They pass validation when the format is correct." do
57
+ lambda {
58
+ OptionsPhone.create!(:other_phone => '12345678')
59
+ }.should_not raise_error
60
+ end
61
+ it ". Their format ends up corrrected" do
62
+ phone = OptionsPhone.create!(:other_phone => '12345678')
63
+ phone.other_phone.should == "1234-5678"
64
+ end
65
+ end
66
+
67
+ describe "rejects invalid formats" do
68
+ invalid_phones.each do |invalid_number|
69
+ it "when on the default field" do
70
+ phone = Phone.create(:phone => invalid_number)
71
+ phone.errors.full_messages[0].should =~ /one must have .*digits\./
72
+ end
73
+ it "when on a non-default field" do
74
+ phone = OptionsPhone.create(:other_phone => invalid_number)
75
+ phone.errors.full_messages[0].should =~ /one must have.*digits\./
76
+ end
77
+
78
+ end
79
+ end
80
+
81
+ # These just test a few options from the standard validates_each configuration.
82
+ describe "validates if " do
83
+ it "'if' option is used and evaluates to true" do
84
+ IfOption.create(:phone_if_true => 'invalid').valid?.should == false
85
+ IfOption.create(:phone_if_true => '3333333333').valid?.should == true
86
+ end
87
+ it "'unless' option is used and evaluates to true" do
88
+ IfOption.create(:phone_unless_false => '123-invalid').valid?.should == false
89
+ IfOption.create(:phone_unless_false => '123-456-7890').valid?.should == true
90
+ end
91
+ end
92
+
93
+ describe "does not validate if " do
94
+ it "'if' option is used and evaluates to false" do
95
+ IfOption.create(:phone_if_false => 'invalid').valid?.should == true
96
+ end
97
+ it "'unless' option is used and evaluates to true" do
98
+ IfOption.create(:phone_unless_true => '123-invalid').valid?.should == true
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "validates_and_formats_phones/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "validates_and_formats_phones"
7
+ s.version = ValidatesAndFormatsPhones::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Bernardo Telles"]
10
+ s.email = ["btelles@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Allows you to easily format and validate phone numbers in any format you desire (sensible defaults provided).}
13
+ s.description = %q{Allows you to easily format and validate phone numbers in any format you desire (sensible defaults provided).}
14
+
15
+ s.rubyforge_project = "validates_and_formats_phones"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+
23
+ s.add_development_dependency 'rspec'
24
+ s.add_development_dependency 'jeweler'
25
+
26
+ s.add_dependency 'active_record'
27
+ end
metadata CHANGED
@@ -1,38 +1,96 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_and_formats_phones
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 8
10
+ version: 0.0.8
5
11
  platform: ruby
6
12
  authors:
7
- - Bernie Telles
13
+ - Bernardo Telles
8
14
  autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-02-08 00:00:00 -05:00
18
+ date: 2011-04-28 00:00:00 -04:00
13
19
  default_executable:
14
- dependencies: []
15
-
16
- description:
17
- email: btelles@gmail.com
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: jeweler
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: active_record
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ description: Allows you to easily format and validate phone numbers in any format you desire (sensible defaults provided).
64
+ email:
65
+ - btelles@gmail.com
18
66
  executables: []
19
67
 
20
68
  extensions: []
21
69
 
22
- extra_rdoc_files:
23
- - README
70
+ extra_rdoc_files: []
71
+
24
72
  files:
25
- - README.rdoc
73
+ - .gitignore
74
+ - Gemfile
26
75
  - README
27
- - discover.rb
76
+ - README.rdoc
28
77
  - Rakefile
29
- - lib/validates_and_formats_phones/validates_and_formats_phones.rb
30
- - lib/validates_and_formats_phones/phone_formatter.rb
78
+ - discover.rb
31
79
  - lib/validates_and_formats_phones.rb
32
- - rails/init.rb
80
+ - lib/validates_and_formats_phones/phone_formatter.rb
81
+ - lib/validates_and_formats_phones/railtie.rb
82
+ - lib/validates_and_formats_phones/validates_and_formats_phones.rb
83
+ - lib/validates_and_formats_phones/version.rb
84
+ - spec/db/database.yml
85
+ - spec/db/schema.rb
86
+ - spec/lib/phone_formatter_spec.rb
87
+ - spec/spec.opts
88
+ - spec/spec_helper.rb
89
+ - spec/validates_and_formats_phones_spec.rb
33
90
  - tasks/validates_and_formats_phones_tasks.rake
91
+ - validates_and_formats_phones.gemspec
34
92
  has_rdoc: true
35
- homepage: http://github.com/btelles/validates_and_formats_phones
93
+ homepage: ""
36
94
  licenses: []
37
95
 
38
96
  post_install_message:
@@ -41,21 +99,27 @@ rdoc_options: []
41
99
  require_paths:
42
100
  - lib
43
101
  required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
44
103
  requirements:
45
104
  - - ">="
46
105
  - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
47
109
  version: "0"
48
- version:
49
110
  required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
50
112
  requirements:
51
113
  - - ">="
52
114
  - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
53
118
  version: "0"
54
- version:
55
119
  requirements: []
56
120
 
57
- rubyforge_project:
58
- rubygems_version: 1.3.5
121
+ rubyforge_project: validates_and_formats_phones
122
+ rubygems_version: 1.3.7
59
123
  signing_key:
60
124
  specification_version: 3
61
125
  summary: Allows you to easily format and validate phone numbers in any format you desire (sensible defaults provided).
@@ -1,2 +0,0 @@
1
- require 'validates_and_formats_phones/phone_formatter'
2
- require 'validates_and_formats_phones/validates_and_formats_phones'