Class: Wagon::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/wagon/address.rb

Constant Summary collapse

CITY_STATE_ZIP =
%r/^(\D+), (\D+)?\s*(\d+(-\d+)?)?$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#cityObject (readonly)

Returns the value of attribute city.



5
6
7
# File 'lib/wagon/address.rb', line 5

def city
  @city
end

#countryObject (readonly)

Returns the value of attribute country.



5
6
7
# File 'lib/wagon/address.rb', line 5

def country
  @country
end

#stateObject (readonly)

Returns the value of attribute state.



5
6
7
# File 'lib/wagon/address.rb', line 5

def state
  @state
end

#streetObject (readonly)

Returns the value of attribute street.



5
6
7
# File 'lib/wagon/address.rb', line 5

def street
  @street
end

#zipObject (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_sObject



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