github.com/midas/addressable_record
Encapsulates the composed of pattern for addresses into any easy to use library.
-
Store an address in multiple database fields, but load it as a AddressableRecord::Address object.
-
Parse virtually any formatting an address can be represented with.
-
Use 1 or more street address parts with no limit.
-
Overrides #to_s with format strings to control output.
-
Pre-defined named format strings to control output.
-
Migration generator to generate a migration file containing the correct fields to hold the address data.
-
geographer >= 1.1.1
-
active_record >= 2.3
gem sources -a http://gemcutter.org sudo gem install addressable_record
Add to environment file:
config.gem "addressable_record", :version => '~>1.0.0', :source => 'http://gemcutter.org'
Run:
sudo rake:gems:install
Generate a migration to add the necessary field to the database:
script/generate addressable_record_migration {file_name} {table_name} {field_name} script/generate addressable_record_migration users_have_home_address users home_address
Will yield:
class UsersHaveHomeAddress < ActiveRecord::Migration def self.up add_column :users, :home_address_raw_street, :string, :limit => 255 add_column :users, :home_address_city, :string, :limit => 50 add_column :users, :home_address_state_or_province, :string, :limit => 50 add_column :users, :home_address_raw_zip_code, :string, :limit => 9 add_column :users, :home_address_country, :string, :limit => 75 end def self.down remove_column :users, :home_address_raw_street remove_column :users, :home_address_city remove_column :users, :home_address_state_or_province remove_column :users, :home_address_raw_zip_code remove_column :users, :home_address_country end end
Call the macro in an ActiveRecord descendant:
class User < ActiveRecord::Base address :home_address end
Set the field equal to something:
@user.home_address = AddressableRecord::Address.new( :raw_street => '123 Jones Street###Suite 540', :city => 'Atlanta', :state_or_province => 'GA', :raw_zip_code => '333331111', :country => 'U.S.A.' )
or (with identical results):
@user.home_address = ['123 Jones Street', 'Suite 540', 'Atlanta', 'GA', '33333-1111']
Reference the raw database fields:
@user.home_address_raw_street # => 123 Jones Street###Suite 540 @user.home_address_city # => Atlanta @user.home_address_state_or_province # => GA @user.home_address_raw_zip_code # => 333331111 @user.home_address_country # => U.S.A.
Reference the sub parts of the field:
@user.home_address.streets.first # => 123 Jones Street @user.home_address.streets.join( ', ' ) # => 123 Jones Street, Suite 540 @user.home_address.streets[1] # => Suite 540 @user.home_address.street # => 123 Jones Street, Suite 540 @user.home_address.city # => Atlanta @user.home_address.state_or_province # => GA @user.home_address.state # => GA @user.home_address.province # => GA @user.home_address.zip_code # => 33333-1111 @user.home_address.zip_code_prefix # => 33333 @user.home_address.zip_code_ext # => 1111 @user.home_address.country # => U.S.A.
Use formats to control the output:
@user.home_address.to_s( '%s' ) # => 123 Jones Street, Suite 540 @user.home_address.to_s( '%c' ) # => Atlanta @user.home_address.to_s( '%S' ) # => GA @user.home_address.to_s( '%z' ) # => 33333-1111 @user.home_address.to_s( '%C' ) # => U.S.A. @user.home_address.to_s( '%c %S' ) # => Atlanta GA
Use pre-defined named formats to control the output:
@user.home_address.to_s( :us_long ) # => 123 Jones Street, Suite 540, Atlanta, GA 33333-1111 U.S.A. @user.home_address.to_s( :us ) # => 123 Jones Street, Suite 540, Atlanta, GA 33333-1111
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. (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)
-
Send me a pull request. Bonus points for topic branches.
Copyright © 2009 C. Jason Harrelson (midas)
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.