Class: Wagon::Address
- Inherits:
-
Object
- Object
- Wagon::Address
- Defined in:
- lib/wagon/address.rb
Constant Summary collapse
- CITY_STATE_ZIP =
%r/^(\D+), (\D+)?\s*(\d+(-\d+)?)?$/
Instance Attribute Summary collapse
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#street ⇒ Object
readonly
Returns the value of attribute street.
-
#zip ⇒ Object
readonly
Returns the value of attribute zip.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(street, city, state, zip, country) ⇒ Address
constructor
A new instance of Address.
- #to_s ⇒ Object
Constructor Details
#initialize(street, city, state, zip, country) ⇒ Address
Returns a new instance of Address.
19 20 21 |
# File 'lib/wagon/address.rb', line 19 def initialize(street, city, state, zip, country) @street, @city, @state, @zip, @country = street, city, state, zip, country end |
Instance Attribute Details
#city ⇒ Object (readonly)
Returns the value of attribute city.
5 6 7 |
# File 'lib/wagon/address.rb', line 5 def city @city end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
5 6 7 |
# File 'lib/wagon/address.rb', line 5 def country @country end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
5 6 7 |
# File 'lib/wagon/address.rb', line 5 def state @state end |
#street ⇒ Object (readonly)
Returns the value of attribute street.
5 6 7 |
# File 'lib/wagon/address.rb', line 5 def street @street end |
#zip ⇒ Object (readonly)
Returns the value of attribute zip.
5 6 7 |
# File 'lib/wagon/address.rb', line 5 def zip @zip end |
Class Method Details
.extract_from_string(string) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/wagon/address.rb', line 7 def self.extract_from_string(string) parts = string.split("\n").collect(&:strip).delete_if(&:empty?) street = city = state = zip = country = nil parts.delete_if do |part| next unless part =~ CITY_STATE_ZIP city, state, zip = $1, ($2 || '').strip(), $3; true end self.new(parts.shift, city, state, zip, parts.shift) end |
Instance Method Details
#==(other) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/wagon/address.rb', line 27 def ==(other) street == other.street && city == other.city && state == other.state && zip == other.zip && country == other.country end |
#to_s ⇒ Object
23 24 25 |
# File 'lib/wagon/address.rb', line 23 def to_s [street, [[city, state].compact.join(", "), zip, country.to_s.empty? ? nil : "(#{country})"].compact.join(" ")].compact.join("\n") end |