Class: PeoplePlacesThings::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/people_places_things/location.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Location) initialize(str)



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/people_places_things/location.rb', line 5

def initialize(str)
  self.raw = str

  tokens = str.split(/\s|,/).collect {|t| t.strip}

  # try to parse last token as zip
  #
  self.zip = ZipCode.new(tokens.last) rescue nil
  tokens = tokens.slice(0..-2) if self.zip

  # try to parse last token as state
  #
  self.state = State.new(tokens.last) rescue nil
  tokens = tokens.slice(0..-2) if self.state

  # remainder must be city
  #
  self.city = tokens.join(' ').strip
  self.city = nil if self.city.empty?
end

Instance Attribute Details

- (Object) city

Returns the value of attribute city



3
4
5
# File 'lib/people_places_things/location.rb', line 3

def city
  @city
end

- (Object) raw

Returns the value of attribute raw



3
4
5
# File 'lib/people_places_things/location.rb', line 3

def raw
  @raw
end

- (Object) state

Returns the value of attribute state



3
4
5
# File 'lib/people_places_things/location.rb', line 3

def state
  @state
end

- (Object) zip

Returns the value of attribute zip



3
4
5
# File 'lib/people_places_things/location.rb', line 3

def zip
  @zip
end

Instance Method Details

- (Object) to_s



26
27
28
# File 'lib/people_places_things/location.rb', line 26

def to_s
  [[self.city, (self.state.to_s(:abbr) rescue nil)].compact.join(','), self.zip.to_s].compact.join(' ')
end