Module: SunDawg::USAStateTranslater

Defined in:
lib/usa_state_translater.rb

Defined Under Namespace

Classes: NoStateError

Constant Summary collapse

FILE =

allows client application to override YAML hash

File.expand_path(File.join(File.dirname(__FILE__), 'usa_states.yml'))
USA_STATES =
YAML.load_file(FILE)

Class Method Summary collapse

Class Method Details

.translate_code_to_name(code) ⇒ Object

O(1) translation of 2-digit code to name

Raises:



18
19
20
21
22
# File 'lib/usa_state_translater.rb', line 18

def self.translate_code_to_name(code)
  state = USA_STATES[code]
  raise NoStateError.new("[#{code}] IS NOT VALID") if state.nil?
  state["name"]
end

.translate_name_to_code(name) ⇒ Object

O(N) translation from state name to 2-digit code

Raises:



10
11
12
13
14
15
# File 'lib/usa_state_translater.rb', line 10

def self.translate_name_to_code(name)
  USA_STATES.each_pair do |key, value| 
    return key if value["name"] == name 
  end
  raise NoStateError.new("[#{name}] IS NOT VALID")
end