Class: Lite::Address::Regexp

Inherits:
Object
  • Object
show all
Defined in:
lib/lite/address/regexp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ Regexp

Returns a new instance of Regexp.



9
10
11
# File 'lib/lite/address/regexp.rb', line 9

def initialize(list)
  @list = list
end

Instance Attribute Details

#listObject (readonly)

Returns the value of attribute list.



7
8
9
# File 'lib/lite/address/regexp.rb', line 7

def list
  @list
end

Instance Method Details

#avoid_unitObject



13
14
15
# File 'lib/lite/address/regexp.rb', line 13

def avoid_unit
  @avoid_unit ||= /(?:[^\#\w]+|\Z)/ix
end

#cardinal_codeObject



17
18
19
20
21
22
# File 'lib/lite/address/regexp.rb', line 17

def cardinal_code
  @cardinal_code ||= begin
    values = list.cardinal_codes.keys
    ::Regexp.new(values.join("|"), ::Regexp::IGNORECASE)
  end
end

#cardinal_typeObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/lite/address/regexp.rb', line 24

def cardinal_type
  @cardinal_type ||= begin
    values = list.cardinal_types.each_with_object([]) do |(key, val), array|
      array << key
      array << [::Regexp.quote(val.gsub(/(\w)/, '\1.')), ::Regexp.quote(val)]
    end

    ::Regexp.new(values.join("|"), ::Regexp::IGNORECASE)
  end
end

#city_stateObject



35
36
37
# File 'lib/lite/address/regexp.rb', line 35

def city_state
  @city_state ||= /(?:(?<city> [^\d,]+?)\W+(?<state> #{subdivision}))/ix
end

#cornerObject



39
40
41
# File 'lib/lite/address/regexp.rb', line 39

def corner
  @corner ||= /(?:\band\b|\bat\b|&|@)/ix
end

#formal_addressObject



43
44
45
46
47
48
49
50
# File 'lib/lite/address/regexp.rb', line 43

def formal_address
  @formal_address ||= /\A[^\w\x23]*
    #{number} \W*
    #{street}\W+
    (?:#{unit}\W+)?
    #{place}\W*\z
  /ix
end

#informal_addressObject



52
53
54
55
56
57
58
59
60
# File 'lib/lite/address/regexp.rb', line 52

def informal_address
  @informal_address ||= /\A\s*
    (?:#{unit} #{separator} #{place})?
    (?:#{number})? \W*
    #{street} #{avoid_unit}
    (?:#{unit} #{separator})?
    (?:#{place})?
  /ix
end

#intersectional_addressObject



62
63
64
65
66
67
68
69
# File 'lib/lite/address/regexp.rb', line 62

def intersectional_address
  @intersectional_address ||= /\A\W*
    #{street}\W*?
    \s+#{corner}\s+
    #{street}\W+
    #{place}\W*\z
  /ix
end

#numberObject

rubocop:disable Lint/MixedRegexpCaptureTypes



72
73
74
75
76
# File 'lib/lite/address/regexp.rb', line 72

def number
  # Utah and Wisconsin have a more elaborate system of block numbering
  # http://en.wikipedia.org/wiki/House_number#Block_numbers
  @number ||= /(?<number>(n|s|e|w)?\d+[.-]?\d*)(?=\D)/ix
end

#placeObject

rubocop:enable Lint/MixedRegexpCaptureTypes



79
80
81
# File 'lib/lite/address/regexp.rb', line 79

def place
  @place ||= /(?:#{city_state}\W*)? (?:#{postal_code})?/ix
end

#postal_codeObject



83
84
85
# File 'lib/lite/address/regexp.rb', line 83

def postal_code
  @postal_code ||= /(?:(?<postal_code>\d{5})(?:-?(?<postal_code_ext>\d{4}))?)/ix
end

#separatorObject



87
88
89
# File 'lib/lite/address/regexp.rb', line 87

def separator
  @separator ||= /(?:\W+|\Z)/ix
end

#streetObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/lite/address/regexp.rb', line 91

def street
  @street ||= /(?:
    (?:
      (?<street> #{cardinal_type})\W+
      (?<street_type> #{street_type})\b
    )
    | (?:(?<prefix> #{cardinal_type})\W+)?
    (?:
      (?<street> [^,]*\d)
      (?:[^\w,]* (?<suffix> #{cardinal_type})\b)
      |
      (?<street> [^,]+)
      (?:[^\w,]+(?<street_type> #{street_type})\b)
      (?:[^\w,]+(?<suffix> #{cardinal_type})\b)?
      |
      (?<street> [^,]+?)
      (?:[^\w,]+(?<street_type> #{street_type})\b)?
      (?:[^\w,]+(?<suffix> #{cardinal_type})\b)?
    )
  )/ix
end

#street_typeObject



113
114
115
116
117
118
# File 'lib/lite/address/regexp.rb', line 113

def street_type
  @street_type ||= begin
    values = (list.street_types.keys + list.street_types.values).uniq
    ::Regexp.new(values.join("|"), ::Regexp::IGNORECASE)
  end
end

#subdivisionObject



120
121
122
123
124
125
# File 'lib/lite/address/regexp.rb', line 120

def subdivision
  @subdivision ||= begin
    values = list.subdivision_codes.flatten.map { |code| ::Regexp.quote(code) }
    ::Regexp.new("\b#{values.join('|')}\b", ::Regexp::IGNORECASE)
  end
end

#unitObject



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/lite/address/regexp.rb', line 127

def unit
  @unit ||= %r{
    (?:
      (?:
        (?:#{unit_numbered} \W*)
        | (?<unit_prefix> \#)\W*
      )
      (?<unit> [\w/-]+)
    ) | #{unit_unnumbered}
  }ix
end

#unit_numberedObject



139
140
141
142
143
144
# File 'lib/lite/address/regexp.rb', line 139

def unit_numbered
  @unit_numbered ||= begin
    values = list.unit_abbr_numbered_regexps.values
    /(?<unit_prefix>#{values.join('|')})(?![a-z])/ix
  end
end

#unit_unnumberedObject



146
147
148
149
150
151
# File 'lib/lite/address/regexp.rb', line 146

def unit_unnumbered
  @unit_unnumbered ||= begin
    values = list.unit_abbr_unnumbered_regexps.values
    /(?<unit_prefix>#{values.join('|')})\b/ix
  end
end