Class: AddressableRecord::Address
- Inherits:
-
Object
- Object
- AddressableRecord::Address
- Defined in:
- lib/addressable_record/address.rb
Constant Summary collapse
- NATURAL_OR_CSV_STRING_REGEX =
/^(.+),s*(.+),s*(.+),s*(.2),?s*(d5)-?(d4)?s*$/
/^\s*?(?:([^,]+?)),\s*?(?:([^,]+?)),\s*?(?:(?:([^,]+?)),\s*?)??([A-Z]{2}),??\s*?(\d{5}(?:-\d{4})?)\s*$/
- NATURAL_OR_CSV_STRING_INVALID_MESSAGE =
"invalid format, try something like: 123 Some Street, Suite 123, Some City, AL 11111-1111"
- NATURAL_STRING_REGEX =
/^(.+),\s*(.+),\s*(.+),\s*(.{2})\s*(\d{5})-?(\d{4})?\s*$/
- CSV_STRING_REGEX =
/^(.+),\s*(.+),\s*(.+),\s*(.{2}),\s*(\d{5})-?(\d{4})?\s*$/
Instance Attribute Summary collapse
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#raw_street ⇒ Object
readonly
:nodoc:.
-
#state_or_province ⇒ Object
readonly
Returns the value of attribute state_or_province.
-
#streets ⇒ Object
readonly
Returns the value of attribute streets.
-
#zip_code ⇒ Object
readonly
Returns the value of attribute zip_code.
-
#zip_code_ext ⇒ Object
readonly
Returns the value of attribute zip_code_ext.
-
#zip_code_prefix ⇒ Object
readonly
Returns the value of attribute zip_code_prefix.
Class Method Summary collapse
-
.convert(address) ⇒ Object
:nodoc:.
- .parse(address) ⇒ Object
-
.parse_street(street) ⇒ Object
:nodoc:.
- .street_delimiter ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(attrs) ⇒ Address
constructor
A new instance of Address.
- #is_blank? ⇒ Boolean
-
#join(opts) ⇒ Object
Outputs the parts of teh address delimited by specified delimiter(s).
- #province ⇒ Object
-
#raw_zip_code ⇒ Object
:nodoc:.
- #state ⇒ Object
- #street(delimiter = ', ') ⇒ Object
-
#to_s(pattern = nil) ⇒ Object
Outputs a address based on pattern provided.
Constructor Details
#initialize(attrs) ⇒ Address
Returns a new instance of Address.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/addressable_record/address.rb', line 19 def initialize( attrs ) raise 'Initilaizer argument must be an attributes hash.' unless attrs.is_a?( Hash ) @city, @state_or_province, @country = attrs[:city], attrs[:state_or_province], attrs[:country] @streets = AddressableRecord::Address.parse_street( attrs[:raw_street] || '' ) raw_zip = (attrs[:raw_zip_code] || '') @zip_code = raw_zip.size == 5 ? raw_zip : raw_zip.gsub( /(\d{5})(\d{4})/, "\\1#{@@zip_code_delimiter}\\2" ) @zip_code_prefix = raw_zip.gsub( /(\d{5})(\d{4}?)/, "\\1" ) @zip_code_ext = raw_zip.gsub( /(\d{5})(\d{4}?)/, "\\2" ) @pattern_map = { '%s' => @streets.join( ', ' ) || "", '%1' => @streets[0] || "", '%2' => @streets[1] || "", '%3' => @streets[2] || "", '%4' => @streets[3] || "", '%5' => @streets[4] || "", '%c' => @city || "", '%S' => @state_or_province || "", '%z' => @zip_code || "", '%C' => @country || "" } self.freeze end |
Instance Attribute Details
#city ⇒ Object (readonly)
Returns the value of attribute city.
10 11 12 |
# File 'lib/addressable_record/address.rb', line 10 def city @city end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
10 11 12 |
# File 'lib/addressable_record/address.rb', line 10 def country @country end |
#raw_street ⇒ Object (readonly)
:nodoc:
132 133 134 |
# File 'lib/addressable_record/address.rb', line 132 def raw_street @raw_street end |
#state_or_province ⇒ Object (readonly)
Returns the value of attribute state_or_province.
10 11 12 |
# File 'lib/addressable_record/address.rb', line 10 def state_or_province @state_or_province end |
#streets ⇒ Object (readonly)
Returns the value of attribute streets.
10 11 12 |
# File 'lib/addressable_record/address.rb', line 10 def streets @streets end |
#zip_code ⇒ Object (readonly)
Returns the value of attribute zip_code.
10 11 12 |
# File 'lib/addressable_record/address.rb', line 10 def zip_code @zip_code end |
#zip_code_ext ⇒ Object (readonly)
Returns the value of attribute zip_code_ext.
10 11 12 |
# File 'lib/addressable_record/address.rb', line 10 def zip_code_ext @zip_code_ext end |
#zip_code_prefix ⇒ Object (readonly)
Returns the value of attribute zip_code_prefix.
10 11 12 |
# File 'lib/addressable_record/address.rb', line 10 def zip_code_prefix @zip_code_prefix end |
Class Method Details
.convert(address) ⇒ Object
:nodoc:
68 69 70 |
# File 'lib/addressable_record/address.rb', line 68 def self.convert( address ) #:nodoc: parse( address ) end |
.parse(address) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/addressable_record/address.rb', line 61 def self.parse( address ) unless [Array, Hash, String].include?( address.class ) raise "Cannot convert #{address.class.to_s.downcase} to an AddressableRecord::Address" end self.send( :"parse_#{address.class.to_s.downcase}", address ) end |
.parse_street(street) ⇒ Object
:nodoc:
72 73 74 |
# File 'lib/addressable_record/address.rb', line 72 def self.parse_street( street ) #:nodoc: return street.split( @@street_delimiter ) end |
.street_delimiter ⇒ Object
45 46 47 |
# File 'lib/addressable_record/address.rb', line 45 def self.street_delimiter @@street_delimiter end |
Instance Method Details
#==(other) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/addressable_record/address.rb', line 97 def ==( other ) return false unless other.instance_of?( self.class ) return false unless other.raw_street.upcase == raw_street.upcase return false unless other.city.upcase == city.upcase return false unless other.state_or_province.upcase == state_or_province.upcase return false unless other.raw_zip_code == raw_zip_code return false unless other.country.upcase == country.upcase true end |
#is_blank? ⇒ Boolean
93 94 95 |
# File 'lib/addressable_record/address.rb', line 93 def is_blank? raw_street.blank? && city.blank? && state_or_province.blank? && raw_zip_code.blank? && country.blank? end |
#join(opts) ⇒ Object
Outputs the parts of teh address delimited by specified delimiter(s).
parameters
- opts
-
Can be a string that is the delimiter or an an options hash.
options
- delimiter
-
The string to delimit the address with.
- street_delimiter
-
An additional delimiter to use only on the street fields.
- country
-
Outputs the country when true, otherwise no country is output (defaults to false).
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/addressable_record/address.rb', line 118 def join( opts ) if opts.is_a?( Hash ) = opts [:street_delimiter] ||= [:delimiter] elsif opts.is_a?( String ) = {} [:street_delimiter] = [:delimiter] = opts [:country] = false end to_return = "#{self.street( [:street_delimiter] )}#{[:delimiter]}#{self.city}, #{self.state_or_province} #{self.zip_code}" return [:country] ? to_return + "#{[:delimiter]}#{self.country}" : to_return end |
#province ⇒ Object
53 54 55 |
# File 'lib/addressable_record/address.rb', line 53 def province @state_or_province end |
#raw_zip_code ⇒ Object
:nodoc:
136 137 138 |
# File 'lib/addressable_record/address.rb', line 136 def raw_zip_code #:nodoc: return @zip_code.nil? ? '' : @zip_code.gsub( /#{@@zip_code_delimiter}/, '' ) end |
#state ⇒ Object
49 50 51 |
# File 'lib/addressable_record/address.rb', line 49 def state @state_or_province end |
#street(delimiter = ', ') ⇒ Object
57 58 59 |
# File 'lib/addressable_record/address.rb', line 57 def street( delimiter=', ' ) return @streets.nil? ? '' : @streets.join( delimiter ) end |
#to_s(pattern = nil) ⇒ Object
Outputs a address based on pattern provided.
Symbols:
%s - street
%c - city
%S - state
%z - zip code
%C - country
85 86 87 88 89 90 91 |
# File 'lib/addressable_record/address.rb', line 85 def to_s( pattern=nil ) return '' if is_blank? to_return = pattern.is_a?( Symbol ) ? @@patterns[pattern] : pattern to_return = @@patterns[:us] if to_return.nil? || to_return.empty? @pattern_map.each { |pat, replacement| to_return = to_return.gsub( pat, replacement ) } to_return.strip end |