Class: PeoplePlacesThings::ZipCode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ ZipCode



5
6
7
8
9
10
11
12
# File 'lib/people_places_things/zip_code.rb', line 5

def initialize(str)
  self.raw = str
  tokens = str.strip.match(/^(\d{5})(-\d{4})?$/)[0].split('-') rescue nil
  raise "Unsupported Format" if !tokens

  self.base = tokens.first
  self.plus_four = tokens[1] rescue nil
end

Instance Attribute Details

#baseObject

Returns the value of attribute base



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

def base
  @base
end

#plus_fourObject

Returns the value of attribute plus_four



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

def plus_four
  @plus_four
end

#rawObject

Returns the value of attribute raw



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

def raw
  @raw
end

Instance Method Details

#to_sObject



14
15
16
# File 'lib/people_places_things/zip_code.rb', line 14

def to_s
  [self.base, self.plus_four].compact.join('-')
end