Class: PeoplePlacesThings::StreetAddress

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

Constant Summary

DIRECTIONS =
{
  :north => %w(north n n.),
  :northeast => %w(northeast ne ne. n.e.),
  :east => %w(east e e.),
  :southeast => %w(southeast se se. s.e.),
  :south => %w(south s s.),
  :southwest => %w(southwest sw sw. s.w.),
  :west => %w(west w w.),
  :northwest => %w(northwest nw nw. n.w.)
}
SUFFIXES =
{
	:alley => %w(alley al al.),
	:avenue => %w(avenue ave ave. av av.),
	:beach => %w(beach bch bch.),
	:bend => %w(bend),
	:boulevard => %w(boulevard blvd blvd. blv blv.),
	:center => %w(center ctr ctr.),
	:circle => %w(circle cir cir.),
	:cliff => %w(cliff clf clf.),
	:club => %w(club),
	:condo => %w(condo con con.),
	:court => %w(court ct ct. cor cor.),
	:cove => %w(cove),
	:creek => %w(creek crk crk.),
	:crossing => %w(crossing xing xing. crs crs.),
	:drive => %w(drive dr dr.),
	:extension => %w(extension ext ext.),
	:freeway => %w(freeway fwy fwy.),
	:gardens => %w(gardens gdns gdns.),
	:glen => %w(glen gl gl.),
	:green => %w(green grn grn.),
	:heights => %w(heights hts hts.),
	:highway => %w(highway hwy hwy. hgwy hgwy.),
	:hill => %w(hill),
	:knoll => %w(knoll knl knl.),
	:lake => %w(lake),
	:lane => %w(lane ln ln.),
	:landing => %w(landing lndg lndg.),
	:loop => %w(loop),
	:meadows => %w(meadows mdws mdws.),
	:manor => %w(manor mnr mnr.),
	:mountain => %w(mountain mtn mtn. mnt mnt.),
	:oaks => %w(oaks),
	:oval => %w(oval),
	:park => %w(park pk pk. prk prk.),
	:parkway => %w(parkway pkwy pkwy. pky pky.),
	:pier => %w(pier),
	:place => %w(place pl pl.),
	:plaza => %w(plaza plz plz.),
	:point => %w(point pt pt. pnt pnt.),
	:ridge => %w(ridge ri ri.),
	:road => %w(road rd rd.),
	:row => %w(row),
	:run => %w(run),
	:springs => %w(springs spgs spgs.),
	:square => %w(square sq sq.),
	:street => %w(street st st.),
	:station => %w(station sta sta.),
	:terrace => %w(terrace ter ter. te te.),
	:turnpike => %w(turnpike tpke tpke.),
	:trace => %w(trace trc trc.),
	:trail => %w(trail trl trl. tl tl.),
	:valley => %w(valley vly vly.),
	:walk => %w(walk),
	:way => %w(way)
}
UNIT_TYPES =
{
  :suite => %w(suite ste ste.),
  :number => %w(number # nbr nbr.),
  :apartment => %w(apartment apt apt.)
}
SUPPORTED_FORMS =
[:long, :short]

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (StreetAddress) initialize(str)

Returns a new instance of StreetAddress



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/people_places_things/street_address.rb', line 5

def initialize(str)
  self.raw = str
  tokens = str.split(/[\s,]/).select {|s| !s.empty?}

  # Check the first token for leading numericality.  If so, set number to the first token, and delete it
  #
  if tokens.first =~ /(^\d+.*)/
    self.number = $1
    tokens.shift
  end

  # If at least two tokens remain, check next-to-last token as unit type.  If so, set unit_type and unit, and delete the tokens
  #
  if tokens.size > 1 
    self.unit_type = StreetAddress.find_token(tokens[-2], UNIT_TYPES)
    if self.unit_type
      self.unit = tokens[-1]
      tokens.slice!(tokens.size - 2, 2)
    end
  end

  # If at least one token remains, check last token for directionality.  If so, set post_direction and delete the token
  #
  if tokens.size > 0
    self.post_direction = StreetAddress.find_token(tokens[-1], DIRECTIONS)
    if self.post_direction
      post_direction_token = tokens[-1]
      tokens.slice!(tokens.size - 1)
    end
  end

  # If at least one token remains, check last token for suffix.  If so, self set.suffix and delete the token
  #
  if tokens.size > 0
    self.suffix = StreetAddress.find_token(tokens[-1], SUFFIXES)
    tokens.slice!(tokens.size - 1) if self.suffix
  end

  # If at least two tokens remain, check first for directionality. If so, set pre_direction and delete token
  #
  if tokens.size > 1
    self.pre_direction = StreetAddress.find_token(tokens.first, DIRECTIONS)
    tokens.shift if self.pre_direction
  end

  # if any tokens remain, set joined remaining tokens as name, otherwise, set name to post_direction, if set, and set post_direction to nil
  #
  if tokens.size > 0
    self.name = tokens.join(' ')
  else
    self.name = post_direction_token
    self.post_direction = nil
  end

  validate_parts
end

Instance Attribute Details

- (Object) name

Returns the value of attribute name



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

def name
  @name
end

- (Object) number

Returns the value of attribute number



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

def number
  @number
end

- (Object) post_direction

Returns the value of attribute post_direction



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

def post_direction
  @post_direction
end

- (Object) pre_direction

Returns the value of attribute pre_direction



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

def pre_direction
  @pre_direction
end

- (Object) raw

Returns the value of attribute raw



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

def raw
  @raw
end

- (Object) suffix

Returns the value of attribute suffix



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

def suffix
  @suffix
end

- (Object) unit

Returns the value of attribute unit



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

def unit
  @unit
end

- (Object) unit_type

Returns the value of attribute unit_type



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

def unit_type
  @unit_type
end

Class Method Details

+ (Object) string_for(symbol, form)



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/people_places_things/street_address.rb', line 74

def self.string_for(symbol, form)
  raise "Requested unknown form \"#{type}\" for :#{symbol}" if !SUPPORTED_FORMS.include?(form)

  val = DIRECTIONS[symbol] || SUFFIXES[symbol] || UNIT_TYPES[symbol]

  if val
    val = ((val[SUPPORTED_FORMS.index(form)] rescue nil) || (val.first rescue val))
  end

  val
end

Instance Method Details

- (Object) to_s



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/people_places_things/street_address.rb', line 62

def to_s
  parts = []
  parts << self.number if self.number
  parts << DIRECTIONS[self.pre_direction].first if self.pre_direction
  parts << self.name if self.name
  parts << SUFFIXES[self.suffix].first if self.suffix
  parts << DIRECTIONS[self.post_direction].first if self.post_direction
  parts << UNIT_TYPES[self.unit_type].first if self.unit_type
  parts << self.unit if self.unit
  parts.join(' ')
end