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

- (ZipCode) initialize(str)

Returns a new instance of 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

- (Object) base

Returns the value of attribute base



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

def base
  @base
end

- (Object) plus_four

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

- (Object) raw

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

- (Object) to_s



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

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