Class: AddressableRecord::Address
- Inherits:
-
Object
- Object
- AddressableRecord::Address
- Defined in:
- lib/addressable_record/address.rb
Instance Attribute Summary collapse
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#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.
Class Method Summary collapse
-
.convert(address) ⇒ Object
:nodoc:.
- .parse(address) ⇒ Object
-
.parse_street(street) ⇒ Object
:nodoc:.
- .street_delimiter ⇒ Object
Instance Method Summary collapse
-
#initialize(attrs) ⇒ Address
constructor
A new instance of Address.
-
#join(opts) ⇒ Object
Outputs the parts of teh address delimited by specified delimiter(s).
- #province ⇒ Object
- #state ⇒ Object
- #street(delimiter = ', ') ⇒ Object
-
#to_s(pattern = nil) ⇒ Object
Outputs a address based on pattern provided.
Constructor Details
#initialize(attrs) ⇒ Address
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/addressable_record/address.rb', line 14 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" ) @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.
5 6 7 |
# File 'lib/addressable_record/address.rb', line 5 def city @city end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
5 6 7 |
# File 'lib/addressable_record/address.rb', line 5 def country @country end |
#state_or_province ⇒ Object (readonly)
Returns the value of attribute state_or_province.
5 6 7 |
# File 'lib/addressable_record/address.rb', line 5 def state_or_province @state_or_province end |
#streets ⇒ Object (readonly)
Returns the value of attribute streets.
5 6 7 |
# File 'lib/addressable_record/address.rb', line 5 def streets @streets end |
#zip_code ⇒ Object (readonly)
Returns the value of attribute zip_code.
5 6 7 |
# File 'lib/addressable_record/address.rb', line 5 def zip_code @zip_code end |
Class Method Details
.convert(address) ⇒ Object
:nodoc:
61 62 63 |
# File 'lib/addressable_record/address.rb', line 61 def self.convert( address ) #:nodoc: parse( address ) end |
.parse(address) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/addressable_record/address.rb', line 54 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:
65 66 67 |
# File 'lib/addressable_record/address.rb', line 65 def self.parse_street( street ) #:nodoc: return street.split( @@street_delimiter ) end |
.street_delimiter ⇒ Object
38 39 40 |
# File 'lib/addressable_record/address.rb', line 38 def self.street_delimiter @@street_delimiter end |
Instance Method Details
#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).
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/addressable_record/address.rb', line 95 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( options[:street_delimiter] )}#{options[:delimiter]}#{self.city}, #{self.state_or_province} #{self.zip_code}" return [:country] ? to_return + "#{options[:delimiter]}#{self.country}" : to_return end |
#province ⇒ Object
46 47 48 |
# File 'lib/addressable_record/address.rb', line 46 def province @state_or_province end |
#state ⇒ Object
42 43 44 |
# File 'lib/addressable_record/address.rb', line 42 def state @state_or_province end |
#street(delimiter = ', ') ⇒ Object
50 51 52 |
# File 'lib/addressable_record/address.rb', line 50 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
78 79 80 81 82 83 |
# File 'lib/addressable_record/address.rb', line 78 def to_s( pattern=nil ) 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 |