Class: AnsiCodes::State
- Inherits:
-
Object
- Object
- AnsiCodes::State
- 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
-
#abbreviation ⇒ String
readonly
The two-letter state abbreviation in caps.
-
#ansi_code ⇒ String
readonly
The two-digit ANSI code string.
-
#counties ⇒ Array<County>
readonly
All of this state’s counties.
-
#name ⇒ String
readonly
The state name in title case.
Class Method Summary collapse
-
.all ⇒ Array<State>
An array of all states.
-
.find(value) ⇒ State
Look up a state by ANSI code, abbreviation, or name.
Instance Method Summary collapse
-
#initialize(ansi_code, name, abbreviation) ⇒ State
constructor
private
Create a new State instance.
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.
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
#abbreviation ⇒ String (readonly)
Returns the two-letter state abbreviation in caps.
15 16 17 |
# File 'lib/ansi_codes/state.rb', line 15 def abbreviation @abbreviation end |
#ansi_code ⇒ String (readonly)
Returns the two-digit ANSI code string.
9 10 11 |
# File 'lib/ansi_codes/state.rb', line 9 def ansi_code @ansi_code end |
#counties ⇒ Array<County> (readonly)
Returns all of this state’s counties.
43 44 45 |
# File 'lib/ansi_codes/state.rb', line 43 def counties County.all(self) end |
#name ⇒ String (readonly)
Returns the state name in title case.
12 13 14 |
# File 'lib/ansi_codes/state.rb', line 12 def name @name end |
Class Method Details
.all ⇒ Array<State>
Returns 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
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 |