Class: Geocoder::US::Map

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#regexpObject

The Map class provides a two-way mapping between postal abbreviations and their fully written equivalents. attr_accessor :partial



13
14
15
# File 'lib/geocoder/us/constants.rb', line 13

def regexp
  @regexp
end

Class Method Details

.[](*items) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/geocoder/us/constants.rb', line 14

def self.[] (*items)
  hash = super(*items)
  #hash.build_partial
  hash.build_match
  hash.keys.each {|k| hash[k.downcase] = hash.fetch(k)}
  hash.values.each {|v| hash[v.downcase] = v}
  hash.freeze
end

Instance Method Details

#[](key) ⇒ Object



44
45
46
# File 'lib/geocoder/us/constants.rb', line 44

def [] (key)
  super(key.downcase)
end

#build_matchObject



31
32
33
34
35
# File 'lib/geocoder/us/constants.rb', line 31

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

#build_partialObject

The build_partial method constructs a hash of case-insensitive, whitespace-delimited prefixes to keys and values in the two-way Map.



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

def build_partial
  @partial = Set.new()
  [keys, values].flatten.each {|item|
    @partial << item.downcase
    item.downcase.split.each {|token| @partial << token}
  }
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/geocoder/us/constants.rb', line 41

def key? (key)
  super(key.downcase)
end

#partial?(key) ⇒ Boolean

The partial? method returns true if the key is a prefix of some key in the Map.

Returns:

  • (Boolean)


38
39
40
# File 'lib/geocoder/us/constants.rb', line 38

def partial? (key)
  @partial.member? key.downcase
end