addressable_record 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ == 1.2.0 2010-09-21
2
+
3
+ * Make able to parse a string of address elements.
4
+
5
+
1
6
  == 1.1.0 2010-07-01
2
7
 
3
8
  * Tweak address formatting to use standard abbreviations and comma placement
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{addressable_record}
8
- s.version = "1.1.0"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["C. Jason Harrelson (midas)"]
12
- s.date = %q{2010-07-01}
12
+ s.date = %q{2010-09-21}
13
13
  s.description = %q{Encapsulates the composed of pattern for addresses into any easy to use library. Provides convenience methods for formatting, parsing, etc.}
14
14
  s.email = %q{jason@lookforwardenterprises.com}
15
15
  s.extra_rdoc_files = [
@@ -44,7 +44,7 @@ Gem::Specification.new do |s|
44
44
  s.homepage = %q{http://github.com/midas/addressable_record}
45
45
  s.rdoc_options = ["--charset=UTF-8"]
46
46
  s.require_paths = ["lib"]
47
- s.rubygems_version = %q{1.3.6}
47
+ s.rubygems_version = %q{1.3.7}
48
48
  s.summary = %q{Encapsulates the composed of pattern for addresses into any easy to use library.}
49
49
  s.test_files = [
50
50
  "spec/addressable_record/address_parsing_shared_spec.rb",
@@ -57,7 +57,7 @@ Gem::Specification.new do |s|
57
57
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
58
58
  s.specification_version = 3
59
59
 
60
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
60
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
61
61
  s.add_runtime_dependency(%q<geographer>, [">= 1.1.1"])
62
62
  s.add_runtime_dependency(%q<activerecord>, [">= 2.3"])
63
63
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
@@ -4,7 +4,7 @@ require 'addressable_record/address'
4
4
  require 'addressable_record/active_record_extesions'
5
5
 
6
6
  module AddressableRecord
7
- VERSION = '1.0.4'
7
+ VERSION = '1.2.0'
8
8
  end
9
9
 
10
10
  ActiveRecord::Base.send( :include, AddressableRecord::ActiveRecordExtensions ) if defined?( ActiveRecord::Base )
@@ -48,11 +48,13 @@ module AddressableRecord
48
48
  end
49
49
 
50
50
  def street( delimiter=', ' )
51
- return @streets.nil? ? '' : @streets.join( delimiter )
51
+ return @streets.nil? ? '' : @streets.join( delimiter )
52
52
  end
53
53
 
54
54
  def self.parse( address )
55
- raise "Cannot convert #{address.class.to_s.downcase} to an AddressableRecord::Address" unless [Array, Hash].include?( address.class )
55
+ unless [Array, Hash, String].include?( address.class )
56
+ raise "Cannot convert #{address.class.to_s.downcase} to an AddressableRecord::Address"
57
+ end
56
58
  self.send( :"parse_#{address.class.to_s.downcase}", address )
57
59
  end
58
60
 
@@ -79,7 +81,7 @@ module AddressableRecord
79
81
  @pattern_map.each { |pat, replacement| to_return = to_return.gsub( pat, replacement ) }
80
82
  to_return.strip
81
83
  end
82
-
84
+
83
85
  # Outputs the parts of teh address delimited by specified delimiter(s).
84
86
  #
85
87
  # *parameters*
@@ -99,7 +101,7 @@ module AddressableRecord
99
101
  options[:street_delimiter] = options[:delimiter] = opts
100
102
  options[:country] = false
101
103
  end
102
-
104
+
103
105
  to_return = "#{self.street( options[:street_delimiter] )}#{options[:delimiter]}#{self.city}, #{self.state_or_province} #{self.zip_code}"
104
106
  return options[:country] ? to_return + "#{options[:delimiter]}#{self.country}" : to_return
105
107
  end
@@ -130,11 +132,17 @@ module AddressableRecord
130
132
  street = streets.join( @@street_delimiter )
131
133
  return AddressableRecord::Address.new( :raw_street => street, :city => city, :state_or_province => state, :raw_zip_code => zip_code, :country => country )
132
134
  end
133
-
135
+
134
136
  def parse_hash( address )
135
137
  return AddressableRecord::Address.new( address )
136
138
  end
137
139
 
140
+ def parse_string( address )
141
+ parts = address.split( ',' )
142
+ parts = parts.map { |p| p.strip }
143
+ parse_array( parts )
144
+ end
145
+
138
146
  def find_state_position( address_elements )
139
147
  # Look for state abbreviation
140
148
  possible_abbreviation_positions = find_possible_state_abbrev_positions( address_elements )
@@ -40,4 +40,4 @@ class ContactAddress < ActiveRecord::Base
40
40
  end
41
41
 
42
42
  ADDRESS_ATTRIBUTES = {:raw_street => '123 Jones Street###Suite 540', :city => 'Atlanta', :state_or_province => 'GA',
43
- :raw_zip_code => '312347890', :country => 'U.S.A.' }
43
+ :raw_zip_code => '333331111', :country => 'U.S.A.' }
@@ -7,51 +7,51 @@ describe "AddressableRecord::Address" do
7
7
  @klass = AddressableRecord::Address
8
8
  @instance = AddressableRecord::Address.new( @address_attributes )
9
9
  end
10
-
10
+
11
11
  it "should agree that the raw_street is correct" do
12
12
  @instance.send( :raw_street ).should eql( @address_attributes[:raw_street] )
13
13
  end
14
-
14
+
15
15
  it "should agree that the city is correct" do
16
16
  @instance.city.should eql( @address_attributes[:city] )
17
17
  end
18
-
18
+
19
19
  it "should agree that the state_or_province is correct" do
20
20
  @instance.state_or_province.should eql( @address_attributes[:state_or_province] )
21
21
  end
22
-
22
+
23
23
  it "should agree that the raw_zip_code is correct" do
24
24
  @instance.send( :raw_zip_code ).should eql( @address_attributes[:raw_zip_code] )
25
25
  end
26
-
26
+
27
27
  it "should agree that the country is correct" do
28
28
  @instance.country.should eql( @address_attributes[:country] )
29
29
  end
30
-
30
+
31
31
  it "should agree that the streets array is the correct size" do
32
32
  @instance.streets.size.should eql( 2 )
33
33
  end
34
-
34
+
35
35
  it "should agree that the default call to \#street is correct" do
36
36
  @instance.street.should eql( @address_attributes[:raw_street].split( AddressableRecord::Address.street_delimiter ).join( ', ' ) )
37
37
  end
38
-
38
+
39
39
  it "should agree that the call to \#street with a pattern of '\\n' is correct" do
40
40
  @instance.street( '\n' ).should eql( @address_attributes[:raw_street].split( AddressableRecord::Address.street_delimiter ).join( '\n' ) )
41
41
  end
42
-
42
+
43
43
  it "should agree that the zip code is correct" do
44
44
  @instance.zip_code.should eql( "#{@address_attributes[:raw_zip_code][0..4]}-#{@address_attributes[:raw_zip_code][5..9]}")
45
45
  end
46
-
46
+
47
47
  it "should agree that the state is the state_or_province" do
48
48
  @instance.state.should eql( @instance.state_or_province )
49
49
  end
50
-
50
+
51
51
  it "should agree that the province is the state_or_province" do
52
52
  @instance.province.should eql( @instance.state_or_province )
53
53
  end
54
-
54
+
55
55
  it "should be able to find the position of a state abbreviation in an array" do
56
56
  @klass.send( :find_state_position, ['123 Jones St.', 'Atlanta', 'GA', '33333', ] ).should eql( 2 )
57
57
  end
@@ -64,10 +64,176 @@ describe "AddressableRecord::Address" do
64
64
  @klass.send( :find_state_position, ['123 Jones St.', 'Atlanta', '33333' ] ).should be_nil
65
65
  end
66
66
 
67
- describe "when parsing an hash of address elements" do
68
-
67
+ describe "when parsng a string of address elements" do
68
+ before :each do
69
+ @address_elements_without_country = ['123 Jones Street', 'Suite 540', 'Atlanta', 'GA', '33333-1111']
70
+ end
71
+
72
+ describe "with a country" do
73
+ before :each do
74
+ @address = @klass.parse( (@address_elements_without_country + ['U.S.A.']).join( ', ' ) )
75
+ end
76
+
77
+ it_should_behave_like "The address 123 Jones Street, Suite 540, Atlanta, GA 33333-1111 U.S.A."
78
+ end
79
+
80
+ describe "without a country" do
81
+ before :each do
82
+ @address = @klass.parse( @address_elements_without_country.join( ', ' ) )
83
+ end
84
+
85
+ it_should_behave_like "The address 123 Jones Street, Suite 540, Atlanta, GA 33333-1111 U.S.A."
86
+ end
87
+
88
+ describe "with a spelled out state name" do
89
+ before :each do
90
+ @address_elements_without_country[@address_elements_without_country.index( 'GA' )] = 'Georgia'
91
+ @address = @klass.parse( @address_elements_without_country.join( ', ' ) )
92
+ end
93
+
94
+ it_should_behave_like "The address 123 Jones Street, Suite 540, Atlanta, GA 33333-1111 U.S.A."
95
+ end
96
+
97
+ describe "when printing out a formatted string" do
98
+ before :each do
99
+ @address = @klass.parse( @address_elements_without_country.join( ', ' ) )
100
+ end
101
+
102
+ it "should obey the :us format correctly" do
103
+ @address.to_s( :us ).should eql( '123 Jones Street, Suite 540, Atlanta, GA 33333-1111' )
104
+ end
105
+
106
+ it "should obey the :us_long format correctly" do
107
+ @address.to_s( :us_long ).should eql( '123 Jones Street, Suite 540, Atlanta, GA 33333-1111 U.S.A.' )
108
+ end
109
+
110
+ it "should obey the %s format correctly" do
111
+ @address.to_s( '%s' ).should eql( '123 Jones Street, Suite 540' )
112
+ end
113
+
114
+ it "should obey the %1 format correctly" do
115
+ @address.to_s( '%1' ).should eql( '123 Jones Street' )
116
+ end
117
+
118
+ it "should obey the %2 format correctly" do
119
+ @address.to_s( '%2' ).should eql( 'Suite 540' )
120
+ end
121
+
122
+ it "should obey the %3 format correctly" do
123
+ @address.to_s( '%3' ).should eql( '' )
124
+ end
125
+
126
+ it "should obey the %4 format correctly" do
127
+ @address.to_s( '%4' ).should eql( '' )
128
+ end
129
+
130
+ it "should obey the %5 format correctly" do
131
+ @address.to_s( '%5' ).should eql( '' )
132
+ end
133
+
134
+ it "should obey the %c format correctly" do
135
+ @address.to_s( '%c' ).should eql( 'Atlanta' )
136
+ end
137
+
138
+ it "should obey the %S format correctly" do
139
+ @address.to_s( '%S' ).should eql( 'GA' )
140
+ end
141
+
142
+ it "should obey the %z format correctly" do
143
+ @address.to_s( '%z' ).should eql( '33333-1111' )
144
+ end
145
+
146
+ it "should obey the %C format correctly" do
147
+ @address.to_s( '%C' ).should eql( 'U.S.A.' )
148
+ end
149
+ end
150
+ end
151
+
152
+ describe "when parsing a hash of address elements" do
153
+ before :each do
154
+ @address_elements_without_country = @address_attributes.reject { |k,v| k.to_s.include?( 'country' ) }
155
+ end
156
+
157
+ describe "with a country" do
158
+ before :each do
159
+ @address = @klass.parse( @address_attributes )
160
+ end
161
+
162
+ it_should_behave_like "The address 123 Jones Street, Suite 540, Atlanta, GA 33333-1111 U.S.A."
163
+ end
164
+
165
+ # describe "without a country" do
166
+ # before :each do
167
+ # @address = @klass.parse( @address_attributes.reject { |k,v| k.to_s.include?( 'country' ) } )
168
+ # end
169
+ #
170
+ # it_should_behave_like "The address 123 Jones Street, Suite 540, Atlanta, GA 33333-1111 U.S.A."
171
+ # end
172
+
173
+ # describe "with a spelled out state name" do
174
+ # before :each do
175
+ # @address_elements_without_country[:state_or_province] = 'Georgia'
176
+ # @address = @klass.parse( @address_elements_without_country )
177
+ # end
178
+ #
179
+ # it_should_behave_like "The address 123 Jones Street, Suite 540, Atlanta, GA 33333-1111 U.S.A."
180
+ # end
181
+
182
+ describe "when printing out a formatted string" do
183
+ before :each do
184
+ @address = @klass.parse( @address_elements_without_country )
185
+ end
186
+
187
+ it "should obey the :us format correctly" do
188
+ @address.to_s( :us ).should eql( '123 Jones Street, Suite 540, Atlanta, GA 33333-1111' )
189
+ end
190
+
191
+ it "should obey the :us_long format correctly" # do
192
+ # @address.to_s( :us_long ).should eql( '123 Jones Street, Suite 540, Atlanta, GA 33333-1111 U.S.A.' )
193
+ # end
194
+
195
+ it "should obey the %s format correctly" do
196
+ @address.to_s( '%s' ).should eql( '123 Jones Street, Suite 540' )
197
+ end
198
+
199
+ it "should obey the %1 format correctly" do
200
+ @address.to_s( '%1' ).should eql( '123 Jones Street' )
201
+ end
202
+
203
+ it "should obey the %2 format correctly" do
204
+ @address.to_s( '%2' ).should eql( 'Suite 540' )
205
+ end
206
+
207
+ it "should obey the %3 format correctly" do
208
+ @address.to_s( '%3' ).should eql( '' )
209
+ end
210
+
211
+ it "should obey the %4 format correctly" do
212
+ @address.to_s( '%4' ).should eql( '' )
213
+ end
214
+
215
+ it "should obey the %5 format correctly" do
216
+ @address.to_s( '%5' ).should eql( '' )
217
+ end
218
+
219
+ it "should obey the %c format correctly" do
220
+ @address.to_s( '%c' ).should eql( 'Atlanta' )
221
+ end
222
+
223
+ it "should obey the %S format correctly" do
224
+ @address.to_s( '%S' ).should eql( 'GA' )
225
+ end
226
+
227
+ it "should obey the %z format correctly" do
228
+ @address.to_s( '%z' ).should eql( '33333-1111' )
229
+ end
230
+
231
+ it "should obey the %C format correctly" # do
232
+ # @address.to_s( '%C' ).should eql( 'U.S.A.' )
233
+ # end
234
+ end
69
235
  end
70
-
236
+
71
237
  describe "when parsing an array of address elements" do
72
238
  before :each do
73
239
  @address_elements_without_country = ['123 Jones Street', 'Suite 540', 'Atlanta', 'GA', '33333-1111']
@@ -102,7 +268,7 @@ describe "AddressableRecord::Address" do
102
268
  before :each do
103
269
  @address = @klass.parse( @address_elements_without_country )
104
270
  end
105
-
271
+
106
272
  it "should obey the :us format correctly" do
107
273
  @address.to_s( :us ).should eql( '123 Jones Street, Suite 540, Atlanta, GA 33333-1111' )
108
274
  end
@@ -10,18 +10,18 @@ describe "AddressableRecord" do
10
10
  ActiveRecord::Base.respond_to?( :addresses ).should be_true
11
11
  end
12
12
  end
13
-
13
+
14
14
  describe "having models descending from ActiveRecord" do
15
15
  before :each do
16
16
  @address_attributes = ADDRESS_ATTRIBUTES
17
17
  @user = User.new( :name => 'John Smith', :address => AddressableRecord::Address.new( @address_attributes ) )
18
18
  @user.save!
19
19
  end
20
-
20
+
21
21
  it "should respond to address" do
22
22
  @user.respond_to?( :address ).should be_true
23
23
  end
24
-
24
+
25
25
  it "should be an AddressableRecord::Address" do
26
26
  @user.address.is_a?( AddressableRecord::Address ).should be_true
27
27
  end
@@ -42,20 +42,34 @@ describe "AddressableRecord" do
42
42
  @user.address.state.should eql( 'GA' )
43
43
  end
44
44
 
45
- it "should agree that the zip_code is 31234-7890" do
46
- @user.address.zip_code.should eql( '31234-7890' )
45
+ it "should agree that the zip_code is 33333-1111" do
46
+ @user.address.zip_code.should eql( '33333-1111' )
47
47
  end
48
48
 
49
49
  it "should agree that the country is U.S.A." do
50
50
  @user.address.country.should eql( 'U.S.A.' )
51
51
  end
52
-
52
+
53
+ # describe "when creating with a string of address elements" do
54
+ # before :each do
55
+ # attributes = ADDRESS_ATTRIBUTES
56
+ # @address_attributes =
57
+ # [attributes[:raw_street],attributes[:city],attributes[:state_or_province],attributes[:raw_zip_code],attributes[:country]]
58
+ # @klass = AddressableRecord::Address
59
+ # @instance = AddressableRecord::Address.new( @address_attributes.join( ', ' ) )
60
+ # end
61
+ #
62
+ # it "should create an address record" do
63
+ # @instance.is_a?( @klass ).should be_true
64
+ # end
65
+ # end
66
+
53
67
  describe "when creating with a Hash of address elements" do
54
68
  before :each do
55
69
  @user = User.new( :name => 'John Smith', :address => @address_attributes )
56
70
  @user.save!
57
71
  end
58
-
72
+
59
73
  it "should respond to address" do
60
74
  @user.respond_to?( :address ).should be_true
61
75
  end
@@ -80,14 +94,14 @@ describe "AddressableRecord" do
80
94
  @user.address.state.should eql( 'GA' )
81
95
  end
82
96
 
83
- it "should agree that the zip_code is 31234-7890" do
84
- @user.address.zip_code.should eql( '31234-7890' )
97
+ it "should agree that the zip_code is 33333-1111" do
98
+ @user.address.zip_code.should eql( '33333-1111' )
85
99
  end
86
100
 
87
101
  it "should agree that the country is U.S.A." do
88
102
  @user.address.country.should eql( 'U.S.A.' )
89
103
  end
90
-
104
+
91
105
  it "should handle a 5 digit zip code" do
92
106
  @user = User.new( :name => 'John Smith', :address => @address_attributes.merge( :raw_zip_code => '31234' ) )
93
107
  @user.save!
@@ -95,14 +109,14 @@ describe "AddressableRecord" do
95
109
  end
96
110
  end
97
111
  end
98
-
112
+
99
113
  describe "when used through an association" do
100
114
  before :each do
101
115
  @address_attributes = ADDRESS_ATTRIBUTES
102
116
  @person = Person.new( :name => 'John Smith' )
103
117
  @person.save!
104
118
  end
105
-
119
+
106
120
  describe "creating through the association" do
107
121
  before :each do
108
122
  @address = AddressableRecord::Address.new( @address_attributes )
@@ -134,12 +148,12 @@ describe "AddressableRecord" do
134
148
  @person.contact_addresses.first.address.state.should eql( 'GA' )
135
149
  end
136
150
 
137
- it "should agree that the address zip code is 31234-7890" do
138
- @address.zip_code.should eql( '31234-7890' )
151
+ it "should agree that the address zip code is 33333-1111" do
152
+ @address.zip_code.should eql( '33333-1111' )
139
153
  end
140
154
 
141
- it "should agree that the associated zip code is 31234-7890" do
142
- @person.contact_addresses.first.address.zip_code.should eql( '31234-7890' )
155
+ it "should agree that the associated zip code is 33333-1111" do
156
+ @person.contact_addresses.first.address.zip_code.should eql( '33333-1111' )
143
157
  end
144
158
 
145
159
  it "should agree that the address country is U.S.A." do
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: addressable_record
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 31
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
- - 1
8
+ - 2
8
9
  - 0
9
- version: 1.1.0
10
+ version: 1.2.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - C. Jason Harrelson (midas)
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-07-01 00:00:00 -05:00
18
+ date: 2010-09-21 00:00:00 -05:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: geographer
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 17
27
30
  segments:
28
31
  - 1
29
32
  - 1
@@ -35,9 +38,11 @@ dependencies:
35
38
  name: activerecord
36
39
  prerelease: false
37
40
  requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
38
42
  requirements:
39
43
  - - ">="
40
44
  - !ruby/object:Gem::Version
45
+ hash: 5
41
46
  segments:
42
47
  - 2
43
48
  - 3
@@ -48,9 +53,11 @@ dependencies:
48
53
  name: rspec
49
54
  prerelease: false
50
55
  requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
51
57
  requirements:
52
58
  - - ">="
53
59
  - !ruby/object:Gem::Version
60
+ hash: 13
54
61
  segments:
55
62
  - 1
56
63
  - 2
@@ -101,23 +108,27 @@ rdoc_options:
101
108
  require_paths:
102
109
  - lib
103
110
  required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
104
112
  requirements:
105
113
  - - ">="
106
114
  - !ruby/object:Gem::Version
115
+ hash: 3
107
116
  segments:
108
117
  - 0
109
118
  version: "0"
110
119
  required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
111
121
  requirements:
112
122
  - - ">="
113
123
  - !ruby/object:Gem::Version
124
+ hash: 3
114
125
  segments:
115
126
  - 0
116
127
  version: "0"
117
128
  requirements: []
118
129
 
119
130
  rubyforge_project:
120
- rubygems_version: 1.3.6
131
+ rubygems_version: 1.3.7
121
132
  signing_key:
122
133
  specification_version: 3
123
134
  summary: Encapsulates the composed of pattern for addresses into any easy to use library.