jm81-dm-address 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +7 -0
- data/Gemfile.lock +34 -0
- data/Rakefile +7 -3
- data/TODO +3 -0
- data/jm81-dm-address.gemspec +27 -15
- data/lib/dm-address.rb +2 -3
- data/lib/dm-address/phone_number.rb +5 -7
- data/lib/dm-address/us.rb +7 -7
- data/lib/dm-address/version.rb +5 -0
- data/lib/dm-address/zip_code.rb +6 -8
- data/lib/dm-types/phone_number.rb +20 -21
- data/lib/dm-types/zip_code.rb +21 -22
- data/lib/dm-validations/formats/phone_number.rb +1 -1
- data/lib/dm-validations/formats/zip_code.rb +1 -1
- data/spec/dm-address/phone_number_spec.rb +3 -3
- data/spec/dm-address/samples.rb +1 -1
- data/spec/dm-address/us_spec.rb +30 -11
- data/spec/dm-address/zip_code_spec.rb +3 -3
- data/spec/dm-types/phone_number_spec.rb +12 -24
- data/spec/dm-types/zip_code_spec.rb +18 -30
- data/spec/fixtures/types-fixture.rb +7 -0
- metadata +90 -22
- data/VERSION +0 -1
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.2.0)
|
5
|
+
dm-core (1.0.2)
|
6
|
+
addressable (~> 2.2)
|
7
|
+
extlib (~> 0.9.15)
|
8
|
+
dm-migrations (1.0.2)
|
9
|
+
dm-core (~> 1.0.2)
|
10
|
+
dm-timestamps (1.0.2)
|
11
|
+
dm-core (~> 1.0.2)
|
12
|
+
dm-types (1.0.2)
|
13
|
+
dm-core (~> 1.0.2)
|
14
|
+
fastercsv (~> 1.5.3)
|
15
|
+
json_pure (~> 1.4)
|
16
|
+
stringex (~> 1.1.0)
|
17
|
+
uuidtools (~> 2.1.1)
|
18
|
+
dm-validations (1.0.2)
|
19
|
+
dm-core (~> 1.0.2)
|
20
|
+
extlib (0.9.15)
|
21
|
+
fastercsv (1.5.3)
|
22
|
+
json_pure (1.4.6)
|
23
|
+
stringex (1.1.0)
|
24
|
+
uuidtools (2.1.1)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
dm-core (~> 1.0.2)
|
31
|
+
dm-migrations (~> 1.0.2)
|
32
|
+
dm-timestamps (~> 1.0.2)
|
33
|
+
dm-types (~> 1.0.2)
|
34
|
+
dm-validations (~> 1.0.2)
|
data/Rakefile
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
|
+
require File.join(File.dirname(__FILE__), 'lib', 'dm-address', 'version')
|
3
4
|
|
4
5
|
begin
|
5
6
|
require 'jeweler'
|
6
7
|
Jeweler::Tasks.new do |gem|
|
7
8
|
gem.name = "jm81-dm-address"
|
9
|
+
gem.version = DataMapper::Address::VERSION.dup
|
8
10
|
gem.summary = %Q{Add address related fields to a DataMapper model}
|
9
11
|
gem.description = <<EOF
|
10
12
|
Add address fields to a DataMapper model. This includes optional validations,
|
@@ -13,9 +15,11 @@ EOF
|
|
13
15
|
gem.email = "jmorgan@morgancreative.net"
|
14
16
|
gem.homepage = "http://github.com/jm81/dm-address"
|
15
17
|
gem.authors = ["Jared Morgan"]
|
16
|
-
gem.add_dependency('dm-core', '
|
17
|
-
gem.add_dependency('dm-
|
18
|
-
gem.add_dependency('dm-
|
18
|
+
gem.add_dependency('dm-core', '~> 1.0.0')
|
19
|
+
gem.add_dependency('dm-timestamps', '~> 1.0.0')
|
20
|
+
gem.add_dependency('dm-types', '~> 1.0.0')
|
21
|
+
gem.add_dependency('dm-validations', '~> 1.0.0')
|
22
|
+
gem.add_development_dependency('dm-migrations', '~> 1.0.0')
|
19
23
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
20
24
|
end
|
21
25
|
Jeweler::GemcutterTasks.new
|
data/TODO
ADDED
data/jm81-dm-address.gemspec
CHANGED
@@ -5,32 +5,36 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{jm81-dm-address}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jared Morgan"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-09-09}
|
13
13
|
s.description = %q{Add address fields to a DataMapper model. This includes optional validations,
|
14
14
|
and additional DM types for ZipCode and PhoneNumber.
|
15
15
|
}
|
16
16
|
s.email = %q{jmorgan@morgancreative.net}
|
17
17
|
s.extra_rdoc_files = [
|
18
18
|
"LICENSE",
|
19
|
-
"README.rdoc"
|
19
|
+
"README.rdoc",
|
20
|
+
"TODO"
|
20
21
|
]
|
21
22
|
s.files = [
|
22
23
|
".document",
|
23
24
|
".gitignore",
|
25
|
+
"Gemfile",
|
26
|
+
"Gemfile.lock",
|
24
27
|
"LICENSE",
|
25
28
|
"README.rdoc",
|
26
29
|
"Rakefile",
|
27
|
-
"
|
30
|
+
"TODO",
|
28
31
|
"jm81-dm-address.gemspec",
|
29
32
|
"lib/dm-address.rb",
|
30
33
|
"lib/dm-address/phone_number.rb",
|
31
34
|
"lib/dm-address/polymorphic.rb",
|
32
35
|
"lib/dm-address/preferred.rb",
|
33
36
|
"lib/dm-address/us.rb",
|
37
|
+
"lib/dm-address/version.rb",
|
34
38
|
"lib/dm-address/zip_code.rb",
|
35
39
|
"lib/dm-types/phone_number.rb",
|
36
40
|
"lib/dm-types/zip_code.rb",
|
@@ -45,18 +49,20 @@ and additional DM types for ZipCode and PhoneNumber.
|
|
45
49
|
"spec/dm-address_spec.rb",
|
46
50
|
"spec/dm-types/phone_number_spec.rb",
|
47
51
|
"spec/dm-types/zip_code_spec.rb",
|
52
|
+
"spec/fixtures/types-fixture.rb",
|
48
53
|
"spec/spec_helper.rb"
|
49
54
|
]
|
50
55
|
s.homepage = %q{http://github.com/jm81/dm-address}
|
51
56
|
s.rdoc_options = ["--charset=UTF-8"]
|
52
57
|
s.require_paths = ["lib"]
|
53
|
-
s.rubygems_version = %q{1.3.
|
58
|
+
s.rubygems_version = %q{1.3.7}
|
54
59
|
s.summary = %q{Add address related fields to a DataMapper model}
|
55
60
|
s.test_files = [
|
56
61
|
"spec/spec_helper.rb",
|
57
62
|
"spec/dm-address_spec.rb",
|
58
63
|
"spec/dm-types/phone_number_spec.rb",
|
59
64
|
"spec/dm-types/zip_code_spec.rb",
|
65
|
+
"spec/fixtures/types-fixture.rb",
|
60
66
|
"spec/dm-address/us_spec.rb",
|
61
67
|
"spec/dm-address/preferred_spec.rb",
|
62
68
|
"spec/dm-address/polymorphic_spec.rb",
|
@@ -69,19 +75,25 @@ and additional DM types for ZipCode and PhoneNumber.
|
|
69
75
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
70
76
|
s.specification_version = 3
|
71
77
|
|
72
|
-
if Gem::Version.new(Gem::
|
73
|
-
s.add_runtime_dependency(%q<dm-core>, ["
|
74
|
-
s.add_runtime_dependency(%q<dm-
|
75
|
-
s.add_runtime_dependency(%q<dm-
|
78
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
79
|
+
s.add_runtime_dependency(%q<dm-core>, ["~> 1.0.0"])
|
80
|
+
s.add_runtime_dependency(%q<dm-timestamps>, ["~> 1.0.0"])
|
81
|
+
s.add_runtime_dependency(%q<dm-types>, ["~> 1.0.0"])
|
82
|
+
s.add_runtime_dependency(%q<dm-validations>, ["~> 1.0.0"])
|
83
|
+
s.add_development_dependency(%q<dm-migrations>, ["~> 1.0.0"])
|
76
84
|
else
|
77
|
-
s.add_dependency(%q<dm-core>, ["
|
78
|
-
s.add_dependency(%q<dm-
|
79
|
-
s.add_dependency(%q<dm-
|
85
|
+
s.add_dependency(%q<dm-core>, ["~> 1.0.0"])
|
86
|
+
s.add_dependency(%q<dm-timestamps>, ["~> 1.0.0"])
|
87
|
+
s.add_dependency(%q<dm-types>, ["~> 1.0.0"])
|
88
|
+
s.add_dependency(%q<dm-validations>, ["~> 1.0.0"])
|
89
|
+
s.add_dependency(%q<dm-migrations>, ["~> 1.0.0"])
|
80
90
|
end
|
81
91
|
else
|
82
|
-
s.add_dependency(%q<dm-core>, ["
|
83
|
-
s.add_dependency(%q<dm-
|
84
|
-
s.add_dependency(%q<dm-
|
92
|
+
s.add_dependency(%q<dm-core>, ["~> 1.0.0"])
|
93
|
+
s.add_dependency(%q<dm-timestamps>, ["~> 1.0.0"])
|
94
|
+
s.add_dependency(%q<dm-types>, ["~> 1.0.0"])
|
95
|
+
s.add_dependency(%q<dm-validations>, ["~> 1.0.0"])
|
96
|
+
s.add_dependency(%q<dm-migrations>, ["~> 1.0.0"])
|
85
97
|
end
|
86
98
|
end
|
87
99
|
|
data/lib/dm-address.rb
CHANGED
@@ -1,16 +1,15 @@
|
|
1
1
|
require 'dm-core'
|
2
|
+
require 'dm-timestamps'
|
2
3
|
require 'dm-types'
|
3
4
|
require 'dm-validations'
|
4
5
|
|
5
6
|
# Require dm-address files
|
6
|
-
%w{ phone_number zip_code polymorphic preferred us }.each do |file|
|
7
|
+
%w{ phone_number zip_code polymorphic preferred us version }.each do |file|
|
7
8
|
require 'dm-address/' + file
|
8
9
|
end
|
9
10
|
|
10
11
|
module DataMapper
|
11
12
|
module Address
|
12
|
-
VERSION = '0.4.0'
|
13
|
-
|
14
13
|
DEFAULTS = {
|
15
14
|
:phone_format => PhoneNumber::DEFAULT_FORMAT.dup,
|
16
15
|
:include_country => false,
|
@@ -5,21 +5,19 @@ module DataMapper
|
|
5
5
|
|
6
6
|
# Remove all non-digits from given phone number
|
7
7
|
def initialize(s)
|
8
|
-
super((s || '').gsub(/\D+/, ''))
|
8
|
+
super((s || '').to_str.gsub(/\D+/, ''))
|
9
9
|
end
|
10
10
|
|
11
|
-
alias base to_s
|
12
|
-
|
13
11
|
# %A is area code, %P is prefix, %S is last 4 digits (suffix)
|
14
12
|
# Default is "(%A) %P-%S" -> (###) ###-####
|
15
13
|
def to_s(format = nil)
|
16
14
|
unless format
|
17
15
|
format = DataMapper::Address.config[:phone_format] || DEFAULT_FORMAT
|
18
16
|
end
|
19
|
-
return '' if
|
20
|
-
format.gsub(/\%A/,
|
21
|
-
gsub(/\%P/,
|
22
|
-
gsub(/\%S/,
|
17
|
+
return '' if to_str.empty?
|
18
|
+
format.gsub(/\%A/, to_str[0..2]).
|
19
|
+
gsub(/\%P/, to_str[3..5]).
|
20
|
+
gsub(/\%S/, to_str[6..9])
|
23
21
|
end
|
24
22
|
end # class PhoneNumber
|
25
23
|
end # module Address
|
data/lib/dm-address/us.rb
CHANGED
@@ -25,23 +25,23 @@ module DataMapper
|
|
25
25
|
DEFAULT_REQUIRED_FIELDS
|
26
26
|
|
27
27
|
[
|
28
|
-
[:id, DataMapper::
|
28
|
+
[:id, DataMapper::Property::Serial],
|
29
29
|
[:name, String, {:length => 100}],
|
30
30
|
[:company, String, {:length => 100}],
|
31
31
|
[:street, String, {:length => 100}],
|
32
32
|
[:street_2, String],
|
33
33
|
[:city, String, {:length => 100}],
|
34
34
|
[:state, String, {:length => 2}],
|
35
|
-
[:postal_code, DataMapper::
|
36
|
-
[:country, String, {:
|
37
|
-
[:phone, DataMapper::
|
35
|
+
[:postal_code, DataMapper::Property::ZipCode, {:format => :zip_code}],
|
36
|
+
[:country, String, {:required => false, :length => 50, :default => 'USA'}],
|
37
|
+
[:phone, DataMapper::Property::PhoneNumber, {:format => :phone_number}],
|
38
38
|
[:created_at, DateTime],
|
39
39
|
[:updated_at, DateTime]
|
40
40
|
].each do |args|
|
41
41
|
unless self.properties.named?(args[0])
|
42
42
|
args[0] = "#{options[:prefix]}#{args[0]}" if options[:prefix]
|
43
43
|
args[2] ||= {}
|
44
|
-
args[2][:
|
44
|
+
args[2][:required] = reqs.include?(args[0])
|
45
45
|
self.property(*args)
|
46
46
|
end
|
47
47
|
end
|
@@ -62,9 +62,9 @@ module DataMapper
|
|
62
62
|
value = attribute_get(fld)
|
63
63
|
fields << value unless value.blank?
|
64
64
|
end
|
65
|
-
fields << "#{self.city}, #{self.state} #{self.postal_code.to_s}"
|
65
|
+
fields << "#{self.city}, #{self.state} #{ZipCode.new(self.postal_code).to_s}"
|
66
66
|
fields << self.country if include_country
|
67
|
-
fields << self.phone.to_s if include_phone && !self.phone.blank?
|
67
|
+
fields << PhoneNumber.new(self.phone).to_s if include_phone && !self.phone.blank?
|
68
68
|
fields.join(newline)
|
69
69
|
end
|
70
70
|
|
data/lib/dm-address/zip_code.rb
CHANGED
@@ -1,18 +1,16 @@
|
|
1
1
|
module DataMapper
|
2
2
|
module Address
|
3
|
-
class ZipCode < String
|
3
|
+
class ZipCode < String
|
4
4
|
# Remove all non-digits from given zip code
|
5
5
|
def initialize(s)
|
6
|
-
super((s || '').gsub(/\D+/, ''))
|
6
|
+
super((s || '').to_str.gsub(/\D+/, ''))
|
7
7
|
end
|
8
|
-
|
9
|
-
alias base to_s
|
10
|
-
|
8
|
+
|
11
9
|
# ZipCode formatted as #####-#### or #####
|
12
10
|
def to_s(format = nil)
|
13
|
-
return '' if
|
14
|
-
return
|
15
|
-
"#{
|
11
|
+
return '' if to_str.empty?
|
12
|
+
return to_str if to_str.length == 5
|
13
|
+
"#{to_str[0..4]}-#{to_str[5..8]}"
|
16
14
|
end
|
17
15
|
end # class ZipCode
|
18
16
|
end # module Address
|
@@ -1,28 +1,27 @@
|
|
1
1
|
module DataMapper
|
2
|
-
|
3
|
-
class PhoneNumber <
|
4
|
-
primitive
|
5
|
-
|
6
|
-
def self.load(value, property)
|
7
|
-
if value.nil?
|
8
|
-
nil
|
9
|
-
elsif value.is_a?(String)
|
10
|
-
DataMapper::Address::PhoneNumber.new(value)
|
11
|
-
else
|
12
|
-
raise ArgumentError.new("+value+ must be nil or a String")
|
13
|
-
end
|
2
|
+
class Property
|
3
|
+
class PhoneNumber < String
|
4
|
+
def primitive?(value)
|
5
|
+
super || value.kind_of?(::DataMapper::Address::PhoneNumber)
|
14
6
|
end
|
15
|
-
|
16
|
-
def
|
17
|
-
|
18
|
-
|
7
|
+
|
8
|
+
def load(value)
|
9
|
+
case value
|
10
|
+
when ::DataMapper::Address::PhoneNumber then value
|
11
|
+
when ::String then DataMapper::Address::PhoneNumber.new(value)
|
12
|
+
else
|
13
|
+
nil
|
14
|
+
end
|
19
15
|
end
|
20
16
|
|
21
|
-
def
|
22
|
-
value
|
23
|
-
|
24
|
-
|
17
|
+
def dump(value)
|
18
|
+
case value
|
19
|
+
when ::DataMapper::Address::PhoneNumber then value.to_str
|
20
|
+
when ::String then value
|
21
|
+
else
|
22
|
+
nil
|
23
|
+
end
|
25
24
|
end
|
26
25
|
end # class PhoneNumber
|
27
|
-
end # module
|
26
|
+
end # module Property
|
28
27
|
end # module DataMapper
|
data/lib/dm-types/zip_code.rb
CHANGED
@@ -1,28 +1,27 @@
|
|
1
1
|
module DataMapper
|
2
|
-
|
3
|
-
class ZipCode <
|
4
|
-
primitive
|
5
|
-
|
6
|
-
def self.load(value, property)
|
7
|
-
if value.nil?
|
8
|
-
nil
|
9
|
-
elsif value.is_a?(String)
|
10
|
-
DataMapper::Address::ZipCode.new(value)
|
11
|
-
else
|
12
|
-
raise ArgumentError.new("+value+ must be nil or a String")
|
13
|
-
end
|
2
|
+
class Property
|
3
|
+
class ZipCode < String
|
4
|
+
def primitive?(value)
|
5
|
+
super || value.kind_of?(::DataMapper::Address::ZipCode)
|
14
6
|
end
|
15
|
-
|
16
|
-
def
|
17
|
-
|
18
|
-
|
7
|
+
|
8
|
+
def load(value)
|
9
|
+
case value
|
10
|
+
when ::DataMapper::Address::ZipCode then value
|
11
|
+
when ::String then DataMapper::Address::ZipCode.new(value)
|
12
|
+
else
|
13
|
+
nil
|
14
|
+
end
|
19
15
|
end
|
20
|
-
|
21
|
-
def
|
22
|
-
value
|
23
|
-
|
24
|
-
|
16
|
+
|
17
|
+
def dump(value)
|
18
|
+
case value
|
19
|
+
when ::DataMapper::Address::ZipCode then value.to_str
|
20
|
+
when ::String then value
|
21
|
+
else
|
22
|
+
nil
|
23
|
+
end
|
25
24
|
end
|
26
25
|
end # class ZipCode
|
27
|
-
end # module
|
26
|
+
end # module Property
|
28
27
|
end # module DataMapper
|
@@ -6,7 +6,7 @@ module DataMapper
|
|
6
6
|
def self.included(base)
|
7
7
|
DataMapper::Validate::FormatValidator::FORMATS.merge!(
|
8
8
|
:phone_number => [
|
9
|
-
Proc.new { |ph| ph.blank? || ph.length == 10 },
|
9
|
+
Proc.new { |ph| ph.blank? || ph.gsub(/\D+/, '').length == 10 },
|
10
10
|
lambda { |field, value| '%s should be 10 digits (include area code)'.t(value) }
|
11
11
|
]
|
12
12
|
)
|
@@ -6,7 +6,7 @@ module DataMapper
|
|
6
6
|
def self.included(base)
|
7
7
|
DataMapper::Validate::FormatValidator::FORMATS.merge!(
|
8
8
|
:zip_code => [
|
9
|
-
Proc.new { |zc| zc.blank? || zc.length == 5 || zc.length == 9 },
|
9
|
+
Proc.new { |zc| zc.blank? || zc.gsub(/\D+/, '').length == 5 || zc.gsub(/\D+/, '').length == 9 },
|
10
10
|
lambda { |field, value| '%s should be 5 digits or 9 digits (ZIP+4)'.t(value) }
|
11
11
|
]
|
12
12
|
)
|
@@ -12,10 +12,10 @@ describe DataMapper::Address::PhoneNumber do
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
describe '#
|
15
|
+
describe '#to_str' do
|
16
16
|
it 'should not be formatted (digits only)' do
|
17
|
-
@klass.new('405-555-5555').
|
18
|
-
@klass.new('(405) 555.5556').
|
17
|
+
@klass.new('405-555-5555').to_str.should == '4055555555'
|
18
|
+
@klass.new('(405) 555.5556').to_str.should == '4055555556'
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
data/spec/dm-address/samples.rb
CHANGED
data/spec/dm-address/us_spec.rb
CHANGED
@@ -75,27 +75,37 @@ describe DataMapper::Address::US do
|
|
75
75
|
end
|
76
76
|
|
77
77
|
describe '#postal_code=' do
|
78
|
+
before(:each) { @address.save }
|
79
|
+
|
78
80
|
it 'should strip out non-digits' do
|
79
|
-
@address.postal_code
|
81
|
+
@address.update(:postal_code => '12345-6789')
|
82
|
+
@address.reload
|
80
83
|
@address.postal_code.should == '123456789'
|
81
|
-
|
84
|
+
|
85
|
+
@address.update(:postal_code => '(12345 -67s80z')
|
86
|
+
@address.reload
|
82
87
|
@address.postal_code.should == '123456780'
|
83
88
|
end
|
84
89
|
end
|
85
90
|
|
86
91
|
describe '#postal_code.to_s' do
|
92
|
+
before(:each) { @address.save }
|
93
|
+
|
87
94
|
it 'should return empty String if #postal_code is blank' do
|
88
|
-
@address.postal_code
|
95
|
+
@address.update!(:postal_code => '')
|
96
|
+
@address.reload
|
89
97
|
@address.postal_code.to_s.should == ''
|
90
98
|
end
|
91
99
|
|
92
100
|
it 'should format 5-digit code as #####' do
|
93
|
-
@address.postal_code
|
101
|
+
@address.update(:postal_code => '12345')
|
102
|
+
@address.reload
|
94
103
|
@address.postal_code.to_s.should == '12345'
|
95
104
|
end
|
96
105
|
|
97
106
|
it 'should format 9-digit code as #####-####' do
|
98
|
-
@address.postal_code
|
107
|
+
@address.update(:postal_code => '123456789')
|
108
|
+
@address.reload
|
99
109
|
@address.postal_code.to_s.should == '12345-6789'
|
100
110
|
end
|
101
111
|
end
|
@@ -123,22 +133,31 @@ describe DataMapper::Address::US do
|
|
123
133
|
end
|
124
134
|
|
125
135
|
describe '#phone=' do
|
136
|
+
before(:each) { @address.save }
|
137
|
+
|
126
138
|
it 'should strip out non-digits' do
|
127
|
-
@address.phone
|
128
|
-
@address.
|
129
|
-
@address.phone
|
130
|
-
|
139
|
+
@address.update(:phone => '405-555-5555')
|
140
|
+
@address.reload
|
141
|
+
@address.phone.to_str.should == '4055555555'
|
142
|
+
|
143
|
+
@address.update(:phone => '(405) 555.5556')
|
144
|
+
@address.reload
|
145
|
+
@address.phone.to_str.should == '4055555556'
|
131
146
|
end
|
132
147
|
end
|
133
148
|
|
134
149
|
describe '#phone.to_s' do
|
150
|
+
before(:each) { @address.save }
|
151
|
+
|
135
152
|
it 'should return empty String if #phone is blank' do
|
136
|
-
@address.phone
|
153
|
+
@address.update(:phone => '')
|
154
|
+
@address.reload
|
137
155
|
@address.phone.to_s.should == ''
|
138
156
|
end
|
139
157
|
|
140
158
|
it 'should format as (###) ###-####' do
|
141
|
-
@address.phone
|
159
|
+
@address.update(:phone => '1234567899')
|
160
|
+
@address.reload
|
142
161
|
@address.phone.to_s.should == '(123) 456-7899'
|
143
162
|
end
|
144
163
|
end
|
@@ -12,10 +12,10 @@ describe DataMapper::Address::ZipCode do
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
describe '#
|
15
|
+
describe '#to_str' do
|
16
16
|
it 'should not be formatted (digits only)' do
|
17
|
-
@klass.new('12345-6789').
|
18
|
-
@klass.new('(12345 -67s80z').
|
17
|
+
@klass.new('12345-6789').to_str.should == '123456789'
|
18
|
+
@klass.new('(12345 -67s80z').to_str.should == '123456780'
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'fixtures/types-fixture'
|
2
3
|
|
3
|
-
describe DataMapper::
|
4
|
-
|
4
|
+
describe DataMapper::Property::PhoneNumber do
|
5
|
+
subject { TypesFixture.properties[:phone] }
|
6
|
+
|
5
7
|
before(:each) do
|
6
|
-
@klass = DataMapper::Types::PhoneNumber
|
7
8
|
@phone_str = "(405) 555-1234"
|
8
9
|
@phone_dump = "4055551234"
|
9
10
|
@phone = DataMapper::Address::PhoneNumber.new(@phone_str)
|
@@ -11,46 +12,33 @@ describe DataMapper::Types::PhoneNumber do
|
|
11
12
|
|
12
13
|
describe ".dump" do
|
13
14
|
it "should return the Phone Number as a digits-only string" do
|
14
|
-
|
15
|
+
subject.dump(@phone).should == @phone_dump
|
15
16
|
end
|
16
17
|
|
17
18
|
it "should return nil if the string is nil" do
|
18
|
-
|
19
|
+
subject.dump(nil).should be_nil
|
19
20
|
end
|
20
21
|
|
21
22
|
it "should return an empty String if the Phone Number is empty" do
|
22
|
-
|
23
|
+
subject.dump(DataMapper::Address::PhoneNumber.new('')).should == ""
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
26
27
|
describe ".load" do
|
27
28
|
it "should return the string as PhoneNumber" do
|
28
|
-
|
29
|
+
subject.load(@phone_str).should == @phone
|
29
30
|
end
|
30
31
|
|
31
32
|
it "should return nil if given nil" do
|
32
|
-
|
33
|
+
subject.load(nil).should be_nil
|
33
34
|
end
|
34
35
|
|
35
36
|
it "should return an empty Phone Number if given an empty string" do
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'should raise an ArgumentError if given something else' do
|
40
|
-
lambda {
|
41
|
-
@klass.load([], :property)
|
42
|
-
}.should raise_error(ArgumentError, '+value+ must be nil or a String')
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
describe '.typecast' do
|
47
|
-
it 'should do nothing if an PhoneNumber is provided' do
|
48
|
-
@klass.typecast(@phone, :property).should == @phone
|
37
|
+
subject.load('').should == DataMapper::Address::PhoneNumber.new('')
|
49
38
|
end
|
50
39
|
|
51
|
-
it 'should
|
52
|
-
|
53
|
-
@klass.typecast(@phone_str, :property)
|
40
|
+
it 'should return nil if given something else' do
|
41
|
+
subject.load([]).should be(nil)
|
54
42
|
end
|
55
43
|
end
|
56
44
|
end
|
@@ -1,56 +1,44 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'fixtures/types-fixture'
|
2
3
|
|
3
|
-
describe DataMapper::
|
4
|
+
describe DataMapper::Property::ZipCode do
|
5
|
+
subject { TypesFixture.properties[:zip] }
|
4
6
|
|
5
7
|
before(:each) do
|
6
|
-
@klass = DataMapper::Types::ZipCode
|
7
8
|
@zip_str = "12345-6789"
|
8
9
|
@zip_dump = "123456789"
|
9
10
|
@zip = DataMapper::Address::ZipCode.new(@zip_str)
|
10
11
|
end
|
11
|
-
|
12
|
+
|
12
13
|
describe ".dump" do
|
13
14
|
it "should return the Zip Number as a digits-only string" do
|
14
|
-
|
15
|
+
subject.dump(@zip).should == @zip_dump
|
15
16
|
end
|
16
|
-
|
17
|
+
|
17
18
|
it "should return nil if the string is nil" do
|
18
|
-
|
19
|
+
subject.dump(nil).should be_nil
|
19
20
|
end
|
20
|
-
|
21
|
+
|
21
22
|
it "should return an empty String if the Zip Number is empty" do
|
22
|
-
|
23
|
+
subject.dump(DataMapper::Address::ZipCode.new('')).should == ""
|
23
24
|
end
|
24
25
|
end
|
25
|
-
|
26
|
+
|
26
27
|
describe ".load" do
|
27
28
|
it "should return the string as ZipCode" do
|
28
|
-
|
29
|
+
subject.load(@zip_str).should == @zip
|
29
30
|
end
|
30
|
-
|
31
|
+
|
31
32
|
it "should return nil if given nil" do
|
32
|
-
|
33
|
+
subject.load(nil).should be_nil
|
33
34
|
end
|
34
|
-
|
35
|
+
|
35
36
|
it "should return an empty Zip Code if given an empty string" do
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'should raise an ArgumentError if given something else' do
|
40
|
-
lambda {
|
41
|
-
@klass.load([], :property)
|
42
|
-
}.should raise_error(ArgumentError, '+value+ must be nil or a String')
|
37
|
+
subject.load("").should == DataMapper::Address::ZipCode.new('')
|
43
38
|
end
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
it 'should do nothing if an ZipCode is provided' do
|
48
|
-
@klass.typecast(@zip, :property).should == @zip
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'should defer to .load if a string is provided' do
|
52
|
-
@klass.should_receive(:load).with(@zip_str, :property)
|
53
|
-
@klass.typecast(@zip_str, :property)
|
39
|
+
|
40
|
+
it 'should return nil if given something else' do
|
41
|
+
subject.load([]).should be(nil)
|
54
42
|
end
|
55
43
|
end
|
56
44
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jm81-dm-address
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 11
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 5
|
9
|
+
- 0
|
10
|
+
version: 0.5.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Jared Morgan
|
@@ -9,39 +15,89 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2010-09-09 00:00:00 -05:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: dm-core
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 1.0.0
|
17
35
|
type: :runtime
|
18
|
-
|
19
|
-
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: dm-timestamps
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
20
42
|
requirements:
|
21
|
-
- -
|
43
|
+
- - ~>
|
22
44
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
45
|
+
hash: 23
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 0
|
49
|
+
- 0
|
50
|
+
version: 1.0.0
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
25
53
|
- !ruby/object:Gem::Dependency
|
26
54
|
name: dm-types
|
27
|
-
|
28
|
-
|
29
|
-
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
30
58
|
requirements:
|
31
|
-
- -
|
59
|
+
- - ~>
|
32
60
|
- !ruby/object:Gem::Version
|
33
|
-
|
34
|
-
|
61
|
+
hash: 23
|
62
|
+
segments:
|
63
|
+
- 1
|
64
|
+
- 0
|
65
|
+
- 0
|
66
|
+
version: 1.0.0
|
67
|
+
type: :runtime
|
68
|
+
version_requirements: *id003
|
35
69
|
- !ruby/object:Gem::Dependency
|
36
70
|
name: dm-validations
|
71
|
+
prerelease: false
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 23
|
78
|
+
segments:
|
79
|
+
- 1
|
80
|
+
- 0
|
81
|
+
- 0
|
82
|
+
version: 1.0.0
|
37
83
|
type: :runtime
|
38
|
-
|
39
|
-
|
84
|
+
version_requirements: *id004
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: dm-migrations
|
87
|
+
prerelease: false
|
88
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
40
90
|
requirements:
|
41
|
-
- -
|
91
|
+
- - ~>
|
42
92
|
- !ruby/object:Gem::Version
|
43
|
-
|
44
|
-
|
93
|
+
hash: 23
|
94
|
+
segments:
|
95
|
+
- 1
|
96
|
+
- 0
|
97
|
+
- 0
|
98
|
+
version: 1.0.0
|
99
|
+
type: :development
|
100
|
+
version_requirements: *id005
|
45
101
|
description: |
|
46
102
|
Add address fields to a DataMapper model. This includes optional validations,
|
47
103
|
and additional DM types for ZipCode and PhoneNumber.
|
@@ -54,19 +110,23 @@ extensions: []
|
|
54
110
|
extra_rdoc_files:
|
55
111
|
- LICENSE
|
56
112
|
- README.rdoc
|
113
|
+
- TODO
|
57
114
|
files:
|
58
115
|
- .document
|
59
116
|
- .gitignore
|
117
|
+
- Gemfile
|
118
|
+
- Gemfile.lock
|
60
119
|
- LICENSE
|
61
120
|
- README.rdoc
|
62
121
|
- Rakefile
|
63
|
-
-
|
122
|
+
- TODO
|
64
123
|
- jm81-dm-address.gemspec
|
65
124
|
- lib/dm-address.rb
|
66
125
|
- lib/dm-address/phone_number.rb
|
67
126
|
- lib/dm-address/polymorphic.rb
|
68
127
|
- lib/dm-address/preferred.rb
|
69
128
|
- lib/dm-address/us.rb
|
129
|
+
- lib/dm-address/version.rb
|
70
130
|
- lib/dm-address/zip_code.rb
|
71
131
|
- lib/dm-types/phone_number.rb
|
72
132
|
- lib/dm-types/zip_code.rb
|
@@ -81,6 +141,7 @@ files:
|
|
81
141
|
- spec/dm-address_spec.rb
|
82
142
|
- spec/dm-types/phone_number_spec.rb
|
83
143
|
- spec/dm-types/zip_code_spec.rb
|
144
|
+
- spec/fixtures/types-fixture.rb
|
84
145
|
- spec/spec_helper.rb
|
85
146
|
has_rdoc: true
|
86
147
|
homepage: http://github.com/jm81/dm-address
|
@@ -92,21 +153,27 @@ rdoc_options:
|
|
92
153
|
require_paths:
|
93
154
|
- lib
|
94
155
|
required_ruby_version: !ruby/object:Gem::Requirement
|
156
|
+
none: false
|
95
157
|
requirements:
|
96
158
|
- - ">="
|
97
159
|
- !ruby/object:Gem::Version
|
160
|
+
hash: 3
|
161
|
+
segments:
|
162
|
+
- 0
|
98
163
|
version: "0"
|
99
|
-
version:
|
100
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
|
+
none: false
|
101
166
|
requirements:
|
102
167
|
- - ">="
|
103
168
|
- !ruby/object:Gem::Version
|
169
|
+
hash: 3
|
170
|
+
segments:
|
171
|
+
- 0
|
104
172
|
version: "0"
|
105
|
-
version:
|
106
173
|
requirements: []
|
107
174
|
|
108
175
|
rubyforge_project:
|
109
|
-
rubygems_version: 1.3.
|
176
|
+
rubygems_version: 1.3.7
|
110
177
|
signing_key:
|
111
178
|
specification_version: 3
|
112
179
|
summary: Add address related fields to a DataMapper model
|
@@ -115,6 +182,7 @@ test_files:
|
|
115
182
|
- spec/dm-address_spec.rb
|
116
183
|
- spec/dm-types/phone_number_spec.rb
|
117
184
|
- spec/dm-types/zip_code_spec.rb
|
185
|
+
- spec/fixtures/types-fixture.rb
|
118
186
|
- spec/dm-address/us_spec.rb
|
119
187
|
- spec/dm-address/preferred_spec.rb
|
120
188
|
- spec/dm-address/polymorphic_spec.rb
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.4.0
|