Class: Geocoder::US::NumberMap

Inherits:
Hash
  • Object
show all
Defined in:
lib/geocoder/us/numbers.rb

Overview

The NumberMap class provides a means for mapping ordinal and cardinal number words to digits and back.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ NumberMap

Returns a new instance of NumberMap.



15
16
17
# File 'lib/geocoder/us/numbers.rb', line 15

def initialize (array)
  @count = 0
end

Instance Attribute Details

#regexpObject

Returns the value of attribute regexp.



8
9
10
# File 'lib/geocoder/us/numbers.rb', line 8

def regexp
  @regexp
end

Class Method Details

.[](array) ⇒ Object



9
10
11
12
13
14
# File 'lib/geocoder/us/numbers.rb', line 9

def self.[] (array)
  nmap = self.new({})
  array.each {|item| nmap << item } 
  nmap.build_match
  nmap
end

Instance Method Details

#<<(item) ⇒ Object



26
27
28
29
30
# File 'lib/geocoder/us/numbers.rb', line 26

def <<(item)
  store clean(item), @count
  store @count, item
  @count += 1
end

#[](key) ⇒ Object



31
32
33
# File 'lib/geocoder/us/numbers.rb', line 31

def [] (key)
  super(clean(key))
end

#build_matchObject



18
19
20
21
22
# File 'lib/geocoder/us/numbers.rb', line 18

def build_match
  @regexp = Regexp.new(
    '\b(' + keys.flatten.join("|") + ')\b',
    Regexp::IGNORECASE)
end

#clean(key) ⇒ Object



23
24
25
# File 'lib/geocoder/us/numbers.rb', line 23

def clean (key)
  key.is_a?(String) ? key.downcase.gsub(/\W/o, "") : key
end