phone_number 1.0.0
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/.document +5 -0
- data/.gitignore +23 -0
- data/LICENSE +20 -0
- data/README.rdoc +104 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/phone_number.rb +10 -0
- data/lib/phone_number/active_record_extensions.rb +30 -0
- data/lib/phone_number/number.rb +72 -0
- data/phone_number.gemspec +68 -0
- data/rails_generators/phone_number_migration/phone_number_migration_generator.rb +21 -0
- data/rails_generators/phone_number_migration/templates/migration.rb +9 -0
- data/script/console +11 -0
- data/spec/database.yml +3 -0
- data/spec/phone_number/number_parsing_shared_spec.rb +45 -0
- data/spec/phone_number/number_spec.rb +84 -0
- data/spec/phone_number_spec.rb +90 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +26 -0
- metadata +96 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 C. Jason Harrelson (midas)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
= phone_number
|
2
|
+
|
3
|
+
http://github.com/midas/guilded
|
4
|
+
|
5
|
+
== DESCRIPTION
|
6
|
+
|
7
|
+
Encapsulates the composed of pattern for phone numbers into any easy to use library.
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
== FEATURES
|
12
|
+
|
13
|
+
* Store a phone number unformatted in a single database field, but load it as a PhoneNumber::Number object.
|
14
|
+
* Parse virtually any formatting a phone number can be represented with.
|
15
|
+
* Format strings to contrl output.
|
16
|
+
* Pre-defined named format strings to control output.
|
17
|
+
|
18
|
+
|
19
|
+
== REQUIREMENTS
|
20
|
+
|
21
|
+
* ActiveRecord >= 2.3
|
22
|
+
|
23
|
+
|
24
|
+
== INSTALL
|
25
|
+
|
26
|
+
gem sources -a http://gemcutter.org
|
27
|
+
sudo gem install phone_number
|
28
|
+
|
29
|
+
|
30
|
+
== INSTALL FOR RAILS
|
31
|
+
|
32
|
+
Add to environment file:
|
33
|
+
|
34
|
+
config.gem "phone_number", :version => '0.0.1', :source => 'http://gemcutter.org'
|
35
|
+
|
36
|
+
Run:
|
37
|
+
|
38
|
+
sudo rake:gems:install
|
39
|
+
|
40
|
+
|
41
|
+
== USAGE
|
42
|
+
|
43
|
+
Generate a migration to add the necessary field to the database:
|
44
|
+
|
45
|
+
script/generate phone_number_migration {file_name} {table_name} {field_name}
|
46
|
+
script/generate phone_number_migration users_have_mobile_phone_numbers users homee_phone
|
47
|
+
|
48
|
+
Call the macro in an ActiveRecord descendant:
|
49
|
+
|
50
|
+
class User < ActiveRecord::Base
|
51
|
+
phone_number :home_phone
|
52
|
+
end
|
53
|
+
|
54
|
+
Set the field equal to something:
|
55
|
+
|
56
|
+
@user.phone_number = '+01 234-576-8901 x 123'
|
57
|
+
|
58
|
+
Use the field:
|
59
|
+
|
60
|
+
@user.to_s # (234) 576-8901
|
61
|
+
|
62
|
+
Use formats to control the output:
|
63
|
+
|
64
|
+
@user.to_s( '%a.%m.%p' ) # 234.576.8901
|
65
|
+
|
66
|
+
Use pre-defined named formats to control the output:
|
67
|
+
|
68
|
+
@user.to_s( :us ) # 01 (234) 567-8901 x 123
|
69
|
+
|
70
|
+
Reference the raw database field:
|
71
|
+
|
72
|
+
@user.raw_home_phone # 012345768901x123
|
73
|
+
|
74
|
+
Reference the sub parts of the field:
|
75
|
+
|
76
|
+
@user.home_phone.country_code # 01
|
77
|
+
@user.home_phone.area_code # 234
|
78
|
+
@user.home_phone.subscriber_number_prefix # 567
|
79
|
+
@user.home_phone.subscriber_number_postfix # 8901
|
80
|
+
@user.home_phone.extension # 123
|
81
|
+
|
82
|
+
|
83
|
+
== Note on Patches/Pull Requests
|
84
|
+
|
85
|
+
* Fork the project.
|
86
|
+
* Make your feature addition or bug fix.
|
87
|
+
* Add tests for it. This is important so I don't break it in a
|
88
|
+
future version unintentionally.
|
89
|
+
* Commit, do not mess with rakefile, version, or history.
|
90
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
91
|
+
* Send me a pull request. Bonus points for topic branches.
|
92
|
+
|
93
|
+
|
94
|
+
== Copyright
|
95
|
+
|
96
|
+
Copyright (c) 2009 C. Jason Harrelson (midas)
|
97
|
+
|
98
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
99
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
100
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
101
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
102
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
103
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
104
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "phone_number"
|
8
|
+
gem.summary = %Q{Encapsulates the composed of pattern for phone numbers into any easy to use library.}
|
9
|
+
gem.description = %Q{Encapsulates the composed of pattern for phone numbers into any easy to use library.}
|
10
|
+
gem.email = "jason@lookforwardenterprises.com"
|
11
|
+
gem.homepage = "http://github.com/midas/phone_number"
|
12
|
+
gem.authors = ["C. Jason Harrelson (midas)"]
|
13
|
+
gem.add_dependency "activerecord", ">= 2.3"
|
14
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "phone_number #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/phone_number.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
require 'phone_number/number'
|
4
|
+
require 'phone_number/active_record_extensions'
|
5
|
+
|
6
|
+
module PhoneNumber
|
7
|
+
VERSION = '1.0.0'
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveRecord::Base.send( :include, PhoneNumber::ActiveRecordExtensions ) if defined?( ActiveRecord::Base )
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module PhoneNumber::ActiveRecordExtensions
|
2
|
+
def self.included( base )
|
3
|
+
base.extend ActsMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ActsMethods
|
7
|
+
def phone_number( *args )
|
8
|
+
unless included_modules.include? InstanceMethods
|
9
|
+
self.class_eval { extend ClassMethods }
|
10
|
+
include InstanceMethods
|
11
|
+
end
|
12
|
+
initialize_compositions( args )
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method :phone_numbers, :phone_number
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def initialize_compositions( attrs )
|
20
|
+
attrs.each do |attr|
|
21
|
+
composed_of attr, :class_name => 'PhoneNumber::Number', :mapping => ["raw_#{attr}", 'raw'], :converter => :convert,
|
22
|
+
:allow_nil => true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module InstanceMethods
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module PhoneNumber
|
2
|
+
class Number
|
3
|
+
attr_reader :country_code, :area_code, :subscriber_number_prefix, :subscriber_number_postfix, :extension
|
4
|
+
|
5
|
+
def initialize( number )
|
6
|
+
if number.is_a?( String ) && m = number.match( /^(\d{1,2})(\d{3})(\d{3})(\d{4})x?(\d*)$/ )
|
7
|
+
@country_code, @area_code, @subscriber_number_prefix, @subscriber_number_postfix, @extension = m[1], m[2], m[3], m[4], m[5]
|
8
|
+
elsif number.is_a?( Hash )
|
9
|
+
@country_code, @area_code, @subscriber_number_prefix, @subscriber_number_postfix, @extension = number[:country_code], number[:area_code], number[:subscriber_number_prefix], number[:subscriber_number_postfix], number[:extension]
|
10
|
+
end
|
11
|
+
|
12
|
+
@@pattern_map = {
|
13
|
+
/%c/ => (@country_code || '').gsub( /0/, '' ) || "",
|
14
|
+
/%C/ => @country_code || "",
|
15
|
+
/%a/ => @area_code || "",
|
16
|
+
/%m/ => @subscriber_number_prefix || "",
|
17
|
+
/%p/ => @subscriber_number_postfix || "",
|
18
|
+
/%x/ => @extension || ""
|
19
|
+
}
|
20
|
+
|
21
|
+
@@patterns = {
|
22
|
+
:us => "%c (%a) %m-%p x %x",
|
23
|
+
:us_short => "(%a) %m-%p",
|
24
|
+
:nanp_short => "(%a) %m-%p"
|
25
|
+
}
|
26
|
+
|
27
|
+
self.freeze
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.parse( number )
|
31
|
+
number.gsub!( / x /, 'x' )
|
32
|
+
if m = number.match( /^\+?(\d{0,2})[ \.\-]?\(?(\d{3})\)?[ \.\-]?(\d{3})[ \.\-]?(\d{4})[ x]?(\d*)$/ )
|
33
|
+
cntry_cd = m[1].size == 2 ? m[1] : "0#{m[1]}"
|
34
|
+
cntry_cd = '01' if cntry_cd.nil? || cntry_cd.empty? || cntry_cd == '0'
|
35
|
+
Number.new( :country_code => cntry_cd, :area_code => m[2], :subscriber_number_prefix => m[3], :subscriber_number_postfix => m[4],
|
36
|
+
:extension => m[5] )
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def empty?
|
41
|
+
return (@country_code.nil? || @country_code.empty?) && (@area_code.nil? || @area_code.empty?) && (@subscriber_number_prefix.nil? || @subscriber_number_prefix.empty?) &&
|
42
|
+
(@subscriber_number_postfix.nil? || @subscriber_number_postfix.empty?) && (@extension.nil? || @extension.empty?)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Creates a phone number based on pattern provided. Defaults to US (NANP) formatting (111) 111-1111.
|
46
|
+
#
|
47
|
+
# Symbols:
|
48
|
+
# %c - country code
|
49
|
+
# %a - area code
|
50
|
+
# %m - subscriber prefix
|
51
|
+
# %p - subscriber postfix
|
52
|
+
#
|
53
|
+
def to_s( pattern="" )
|
54
|
+
return '' if self.empty?
|
55
|
+
to_return = pattern.is_a?( Symbol ) ? @@patterns[pattern] : pattern
|
56
|
+
to_return = @@patterns[:us_short] if to_return.empty?
|
57
|
+
@@pattern_map.each { |pat, replacement| to_return = to_return.gsub( pat, replacement ) }
|
58
|
+
to_return.strip
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.convert( number )
|
62
|
+
parse( number )
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def raw
|
68
|
+
ext = (@extension.nil? || @extension.empty?) ? "" : "x#{@extension}"
|
69
|
+
"#{@country_code}#{@area_code}#{@subscriber_number_prefix}#{@subscriber_number_postfix}#{ext}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{phone_number}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["C. Jason Harrelson (midas)"]
|
12
|
+
s.date = %q{2009-12-25}
|
13
|
+
s.description = %q{Encapsulates the composed of pattern for phone numbers into any easy to use library.}
|
14
|
+
s.email = %q{jason@lookforwardenterprises.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/phone_number.rb",
|
27
|
+
"lib/phone_number/active_record_extensions.rb",
|
28
|
+
"lib/phone_number/number.rb",
|
29
|
+
"phone_number.gemspec",
|
30
|
+
"rails_generators/phone_number_migration/phone_number_migration_generator.rb",
|
31
|
+
"rails_generators/phone_number_migration/templates/migration.rb",
|
32
|
+
"script/console",
|
33
|
+
"spec/database.yml",
|
34
|
+
"spec/phone_number/number_parsing_shared_spec.rb",
|
35
|
+
"spec/phone_number/number_spec.rb",
|
36
|
+
"spec/phone_number_spec.rb",
|
37
|
+
"spec/spec.opts",
|
38
|
+
"spec/spec_helper.rb"
|
39
|
+
]
|
40
|
+
s.homepage = %q{http://github.com/midas/phone_number}
|
41
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
42
|
+
s.require_paths = ["lib"]
|
43
|
+
s.rubygems_version = %q{1.3.5}
|
44
|
+
s.summary = %q{Encapsulates the composed of pattern for phone numbers into any easy to use library.}
|
45
|
+
s.test_files = [
|
46
|
+
"spec/phone_number/number_parsing_shared_spec.rb",
|
47
|
+
"spec/phone_number/number_spec.rb",
|
48
|
+
"spec/phone_number_spec.rb",
|
49
|
+
"spec/spec_helper.rb"
|
50
|
+
]
|
51
|
+
|
52
|
+
if s.respond_to? :specification_version then
|
53
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
54
|
+
s.specification_version = 3
|
55
|
+
|
56
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
57
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 2.3"])
|
58
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<activerecord>, [">= 2.3"])
|
61
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
62
|
+
end
|
63
|
+
else
|
64
|
+
s.add_dependency(%q<activerecord>, [">= 2.3"])
|
65
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class PhoneNumberMigrationGenerator < Rails::Generator::NamedBase
|
2
|
+
def initialize( runtime_args, runtime_options={} )
|
3
|
+
super
|
4
|
+
@stamp = DateTime.now.utc.strftime( "%Y%m%d%H%M%S" )
|
5
|
+
parse_args( args )
|
6
|
+
end
|
7
|
+
|
8
|
+
def manifest
|
9
|
+
record do |m|
|
10
|
+
m.directory "db/migrate"
|
11
|
+
m.template "migration.rb", "db/migrate/#{@stamp}_#{name}.rb", :assigns => { :table => @table, :field => @field }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def parse_args( args )
|
18
|
+
@table = args[0]
|
19
|
+
@field = args[1]
|
20
|
+
end
|
21
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r 'active_record' -r '#{File.dirname(__FILE__) + '/../lib/phone_number.rb'}'"
|
9
|
+
|
10
|
+
puts "Loading phone number gem"
|
11
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/spec/database.yml
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path( File.dirname(__FILE__) + '/../spec_helper' )
|
2
|
+
|
3
|
+
shared_examples_for "The phone number 1-234-567-8901 x 111" do
|
4
|
+
it "should agree that the country code is '01'" do
|
5
|
+
@instance.country_code.should eql( '01' )
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should agree that the area code is '234'" do
|
9
|
+
@instance.area_code.should eql( '234' )
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should agree that the subscriber number prefix is '567'" do
|
13
|
+
@instance.subscriber_number_prefix.should eql( '567' )
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should agree that the subscriber number postfix is '234'" do
|
17
|
+
@instance.subscriber_number_postfix.should eql( '8901' )
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should agree that the extension is '111'" do
|
21
|
+
@instance.extension.should eql( '111' )
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
shared_examples_for "The phone number 1-234-567-8901" do
|
26
|
+
it "should agree that the country code is '01'" do
|
27
|
+
@instance.country_code.should eql( '01' )
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should agree that the area code is '234'" do
|
31
|
+
@instance.area_code.should eql( '234' )
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should agree that the subscriber number prefix is '567'" do
|
35
|
+
@instance.subscriber_number_prefix.should eql( '567' )
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should agree that the subscriber number postfix is '234'" do
|
39
|
+
@instance.subscriber_number_postfix.should eql( '8901' )
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should agree that the extension is ''" do
|
43
|
+
@instance.extension.should eql( '' )
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require File.expand_path( File.dirname(__FILE__) + '/../spec_helper' )
|
2
|
+
require File.expand_path( File.dirname(__FILE__) + '/number_parsing_shared_spec' )
|
3
|
+
|
4
|
+
|
5
|
+
describe "PhoneNumber::Number" do
|
6
|
+
describe "when initializing from a raw string with an extension" do
|
7
|
+
before :each do
|
8
|
+
@instance = PhoneNumber::Number.new( '012345678901x111' )
|
9
|
+
end
|
10
|
+
|
11
|
+
it_should_behave_like "The phone number 1-234-567-8901 x 111"
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "when initializing from a raw string without an extension" do
|
15
|
+
before :each do
|
16
|
+
@instance = PhoneNumber::Number.new( '012345678901' )
|
17
|
+
end
|
18
|
+
|
19
|
+
it_should_behave_like "The phone number 1-234-567-8901"
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "when being set equal to a string if correctly parsing" do
|
23
|
+
numbers = ['012345678901', '+012345678901', '12345678901', '2345678901', '01-234-567-8901', '01.234.567.8901', '01 234 567 8901', '+01-234-567-8901',
|
24
|
+
'+01.234.567.8901', '+01 234 567 8901', '1-234-567-8901', '1.234.567.8901', '1 234 567 8901', '+1-234-567-8901', '+1.234.567.8901', '+1 234 567 8901',
|
25
|
+
'1 (234) 567-8901', '(234) 567-8901', '1 (234) 567.8901', '(234) 567.8901', '(234) 567 8901', '(234) 5678901', '(234)5678901', '1(234)5678901',
|
26
|
+
'01(234)5678901', '+1(234)5678901', '+01(234)5678901' ]
|
27
|
+
|
28
|
+
numbers.each do |number|
|
29
|
+
describe "'#{number}'" do
|
30
|
+
before :each do
|
31
|
+
@instance = PhoneNumber::Number.parse( number )
|
32
|
+
end
|
33
|
+
|
34
|
+
it_should_behave_like "The phone number 1-234-567-8901"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
numbers.map{ |n| n + 'x111' }.each do |number|
|
39
|
+
describe "'#{number}'" do
|
40
|
+
before :each do
|
41
|
+
@instance = PhoneNumber::Number.parse( number )
|
42
|
+
end
|
43
|
+
|
44
|
+
it_should_behave_like "The phone number 1-234-567-8901 x 111"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
numbers.map{ |n| n + ' x 111' }.each do |number|
|
49
|
+
describe "'#{number}'" do
|
50
|
+
before :each do
|
51
|
+
@instance = PhoneNumber::Number.parse( number )
|
52
|
+
end
|
53
|
+
|
54
|
+
it_should_behave_like "The phone number 1-234-567-8901 x 111"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "when formatting output" do
|
60
|
+
before :each do
|
61
|
+
@instance = PhoneNumber::Number.new( '012345678901x111' )
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should format the number correctly with the pattern '%a-%m-%p'" do
|
65
|
+
@instance.to_s( '%a-%m-%p' ).should eql( '234-567-8901' )
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should format the number correctly with the pattern '%a.%m.%p x %x'" do
|
69
|
+
@instance.to_s( '%a.%m.%p x %x' ).should eql( '234.567.8901 x 111' )
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should define a format of us_short" do
|
73
|
+
@instance.to_s( :us_short ).should eql( '(234) 567-8901' )
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should use the us_short format for its default format" do
|
77
|
+
@instance.to_s( :us_short ).should eql( @instance.to_s )
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should agree that a newly instantiated phone number is empty" do
|
82
|
+
PhoneNumber::Number.new( nil ).empty?.should be_true
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "PhoneNumber" do
|
4
|
+
describe "having ActiveRecord extensions" do
|
5
|
+
before :each do
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should respond to phone_number" do
|
10
|
+
ActiveRecord::Base.respond_to?( :phone_number ).should be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should respond to phone_number" do
|
14
|
+
ActiveRecord::Base.respond_to?( :phone_numbers ).should be_true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "having models descending from ActiveRecord" do
|
19
|
+
before(:each) do
|
20
|
+
@user = User.new( :home_phone => '(234) 567-8901 x 111', :mobile_phone => '(111) 111-1111 x 111' )
|
21
|
+
@user.save!
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should respond to raw_{attribute_nam}" do
|
25
|
+
@user.respond_to?( :raw_home_phone ).should be_true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should save the phone number in raw format" do
|
29
|
+
@user.raw_home_phone.should eql( '012345678901x111' )
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should have a composed of attribute of type PhoneNumber::Number" do
|
33
|
+
@user.home_phone.is_a?( PhoneNumber::Number ).should be_true
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should have the correct country code" do
|
37
|
+
@user.home_phone.country_code.should eql( '01' )
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should have the area country code" do
|
41
|
+
@user.home_phone.area_code.should eql( '234' )
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should have the subscriber number prefix code" do
|
45
|
+
@user.home_phone.subscriber_number_prefix.should eql( '567' )
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should have the subscriber number postfix code" do
|
49
|
+
@user.home_phone.subscriber_number_postfix.should eql( '8901' )
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should have the correct extension" do
|
53
|
+
@user.home_phone.extension.should eql( '111' )
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "having a second phone number attribute" do
|
57
|
+
it "should respond to raw_{attribute_nam}" do
|
58
|
+
@user.respond_to?( :raw_mobile_phone ).should be_true
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should save the phone number in raw format" do
|
62
|
+
@user.raw_mobile_phone.should eql( '011111111111x111' )
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should have a composed of attribute of type PhoneNumber::Number" do
|
66
|
+
@user.mobile_phone.is_a?( PhoneNumber::Number ).should be_true
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should have the correct country code" do
|
70
|
+
@user.mobile_phone.country_code.should eql( '01' )
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should have the area country code" do
|
74
|
+
@user.mobile_phone.area_code.should eql( '111' )
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should have the subscriber number prefix code" do
|
78
|
+
@user.mobile_phone.subscriber_number_prefix.should eql( '111' )
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should have the subscriber number postfix code" do
|
82
|
+
@user.mobile_phone.subscriber_number_postfix.should eql( '1111' )
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should have the correct extension" do
|
86
|
+
@user.mobile_phone.extension.should eql( '111' )
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rubygems'
|
4
|
+
require 'active_record'
|
5
|
+
require 'phone_number'
|
6
|
+
require 'spec'
|
7
|
+
require 'spec/autorun'
|
8
|
+
|
9
|
+
Spec::Runner.configure do |config|
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::Base.configurations = YAML::load( IO.read( File.dirname(__FILE__) + '/database.yml' ) )
|
14
|
+
ActiveRecord::Base.establish_connection( 'test' )
|
15
|
+
|
16
|
+
ActiveRecord::Schema.define :version => 1 do
|
17
|
+
create_table "users", :force => true do |t|
|
18
|
+
t.string "name", :limit => 50
|
19
|
+
t.string "raw_home_phone", :limit => 35
|
20
|
+
t.string "raw_mobile_phone", :limit => 35
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class User < ActiveRecord::Base
|
25
|
+
phone_number :home_phone, :mobile_phone
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: phone_number
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- C. Jason Harrelson (midas)
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-25 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "2.3"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.9
|
34
|
+
version:
|
35
|
+
description: Encapsulates the composed of pattern for phone numbers into any easy to use library.
|
36
|
+
email: jason@lookforwardenterprises.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/phone_number.rb
|
52
|
+
- lib/phone_number/active_record_extensions.rb
|
53
|
+
- lib/phone_number/number.rb
|
54
|
+
- phone_number.gemspec
|
55
|
+
- rails_generators/phone_number_migration/phone_number_migration_generator.rb
|
56
|
+
- rails_generators/phone_number_migration/templates/migration.rb
|
57
|
+
- script/console
|
58
|
+
- spec/database.yml
|
59
|
+
- spec/phone_number/number_parsing_shared_spec.rb
|
60
|
+
- spec/phone_number/number_spec.rb
|
61
|
+
- spec/phone_number_spec.rb
|
62
|
+
- spec/spec.opts
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
has_rdoc: true
|
65
|
+
homepage: http://github.com/midas/phone_number
|
66
|
+
licenses: []
|
67
|
+
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options:
|
70
|
+
- --charset=UTF-8
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
version:
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.3.5
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Encapsulates the composed of pattern for phone numbers into any easy to use library.
|
92
|
+
test_files:
|
93
|
+
- spec/phone_number/number_parsing_shared_spec.rb
|
94
|
+
- spec/phone_number/number_spec.rb
|
95
|
+
- spec/phone_number_spec.rb
|
96
|
+
- spec/spec_helper.rb
|