Class: AnsiCodes::State

Inherits:
Object
  • Object
show all
Defined in:
lib/ansi_codes/state.rb

Overview

A representation of US states and equivalent census areas. State instances are created at class load time and are immutable.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ansi_code, name, abbreviation) ⇒ State (private)

Note:

This is only meant to be called internally during class loading. You cannot call #new directly.

Create a new State instance.

Parameters:

  • ansi_code (String)

    the two-digit state ANSI code

  • name (String)

    the state name

  • abbreviation (String)

    the two-letter state abbreviation



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ansi_codes/state.rb', line 25

def initialize(ansi_code, name, abbreviation)
  @ansi_code = ansi_code
  @name = name
  @abbreviation = abbreviation
  freeze
  self.class.instance_variable_get(:@states).tap do |states|
    states[:ansi_code][@ansi_code.downcase] =
      states[:name][@name.downcase] =
      states[:abbreviation][@abbreviation.downcase] = self
  end
end

Instance Attribute Details

#abbreviationString (readonly)

Returns the two-letter state abbreviation in caps.

Returns:

  • (String)

    the two-letter state abbreviation in caps



15
16
17
# File 'lib/ansi_codes/state.rb', line 15

def abbreviation
  @abbreviation
end

#ansi_codeString (readonly)

Returns the two-digit ANSI code string.

Returns:

  • (String)

    the two-digit ANSI code string



9
10
11
# File 'lib/ansi_codes/state.rb', line 9

def ansi_code
  @ansi_code
end

#countiesArray<County> (readonly)

Returns all of this state’s counties.

Returns:

  • (Array<County>)

    all of this state’s counties



43
44
45
# File 'lib/ansi_codes/state.rb', line 43

def counties
  County.all(self)
end

#nameString (readonly)

Returns the state name in title case.

Returns:

  • (String)

    the state name in title case



12
13
14
# File 'lib/ansi_codes/state.rb', line 12

def name
  @name
end

Class Method Details

.allArray<State>

Returns an array of all states.

Returns:

  • (Array<State>)

    an array of all states



73
74
75
# File 'lib/ansi_codes/state.rb', line 73

def self.all
  @states[:ansi_code].values
end

.find(value) ⇒ State

Look up a state by ANSI code, abbreviation, or name

Parameters:

  • value (Fixnum, String)

    the lookup query

Returns:

Raises:

  • (ArgumentError)

    if the argument is not a Fixnum or String

  • (RuntimeError)

    if no associated AnsiCodes::State is found



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ansi_codes/state.rb', line 52

def self.find(value)
  case value
  when Fixnum
    value = '%02d' % value
    selector = :ansi_code
  when String
    begin
      Integer(value, 10)
      selector = :ansi_code
    rescue ArgumentError
      selector = value.size == 2 ? :abbreviation : :name
    end
  else raise(ArgumentError, 'Argument must be an integer or a string.')
  end
  @states[selector][value.downcase].tap do |result|
    raise(RuntimeError, "No state found for lookup '#{value}'") unless result
    yield result if block_given?
  end
end