class AddressTitlecase::Titleizer

AddressTitlecase::Titleizer .titleize

Will titlecase a given US address string.
Words that should be overridden can be passed as an overrides hash option.

Ex:
  AddressTitlecase::Titleizer.titleize('123 sesame st', overrides: { 'st' => 'ST' }) => '123 Sesame ST'

Constants

CA_PROVINCES
CA_TERRITORIES
COUNTRIES
DIRECTIONS
MILITARY
MISC
REMAIN_UPCASE
US_STATES
US_TERRITORIES

Public Class Methods

titleize(address, opts = {}) click to toggle source
# File lib/address_titlecase/titleizer.rb, line 28
def titleize(address, opts = {})
  overrides = opts['overrides'] || opts[:overrides] || {}
  address.gsub(/[[:alpha:]]+['’]?[[:alpha:]]*/) do |word|
    overriden_word = overrides[word]
    if overriden_word
      overriden_word
    elsif REMAIN_UPCASE.include?(word.upcase) || contains_digits?(word)
      word.upcase
    else
      word.capitalize
    end
  end
end

Private Class Methods

contains_digits?(word) click to toggle source
# File lib/address_titlecase/titleizer.rb, line 44
def contains_digits?(word)
  !word.count('0-9').zero?
end