Class: AnsiCodes::County
- Inherits:
-
Object
- Object
- AnsiCodes::County
- Defined in:
- lib/ansi_codes/county.rb
Overview
A representation of US counties and equivalent census areas. County instances are created at class load time and are immutable.
Constant Summary collapse
- Designations =
Suffixes for counties (parishes, etc.) to help split names into their parts.
/(.*) (city|Borough|County|City and Borough|Census Area|Municipality|Parish|Islands?|District|Municipio)$/
Instance Attribute Summary collapse
-
#ansi_code ⇒ String
readonly
The three digit ANSI code for this county.
-
#designation ⇒ String
readonly
The census designation (County, Parish, Municipio, etc.).
-
#name ⇒ String
readonly
The county’s full name in title case.
-
#short_name ⇒ String
readonly
The county name, minus the designation.
-
#state ⇒ State
readonly
The State that this county belongs to.
Class Method Summary collapse
-
.all(state = nil) ⇒ Array<County>
All counties or all of a State‘s counties if one is provided.
-
.find(state, county) ⇒ County
Look up a county by state and county ANSI code or name.
Instance Method Summary collapse
-
#initialize(state_ansi, county_ansi, name) ⇒ County
constructor
private
Create a new County instance.
Constructor Details
#initialize(state_ansi, county_ansi, name) ⇒ County (private)
This is only meant to be called internally during class loading. You cannot call #new directly.
Create a new County instance.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ansi_codes/county.rb', line 29 def initialize(state_ansi, county_ansi, name) @state = State.find(state_ansi) @ansi_code = county_ansi @name = name match = name.match(Designations) @short_name = match && match[1] || name @designation = match && match[2] || '' freeze self.class.instance_variable_get(:@counties)[@state].tap do |counties| counties[:ansi_code][@ansi_code.downcase] = counties[:name][@name.downcase] = self end end |
Instance Attribute Details
#ansi_code ⇒ String (readonly)
Returns the three digit ANSI code for this county.
8 9 10 |
# File 'lib/ansi_codes/county.rb', line 8 def ansi_code @ansi_code end |
#designation ⇒ String (readonly)
some cities have no designation, in which case this will return the empty string
Returns the census designation (County, Parish, Municipio, etc.).
16 17 18 |
# File 'lib/ansi_codes/county.rb', line 16 def designation @designation end |
#name ⇒ String (readonly)
Returns the county’s full name in title case.
10 11 12 |
# File 'lib/ansi_codes/county.rb', line 10 def name @name end |
#short_name ⇒ String (readonly)
some cities have no designation, so this attribute will be the same as #name
Returns the county name, minus the designation.
13 14 15 |
# File 'lib/ansi_codes/county.rb', line 13 def short_name @short_name end |
Class Method Details
.all(state = nil) ⇒ Array<County>
Returns all counties or all of a State‘s counties if one is provided.
47 48 49 50 |
# File 'lib/ansi_codes/county.rb', line 47 def self.all(state = nil) state ? @counties[state][:ansi_code].values : @counties.values.flat_map {|values| values[:ansi_code]}.flat_map(&:values) end |
.find(state, county) ⇒ County
Look up a county by state and county ANSI code or name
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ansi_codes/county.rb', line 60 def self.find(state, county) state = state.is_a?(State) ? state : State.find(state) case county when Fixnum county, selector = '%03d' % county, :ansi_code when String selector = county =~ /^[0-9]{3}$/ ? :ansi_code : :name else raise(ArgumentError, 'Argument must be an integer or a string.') end @counties[state][selector][county.downcase].tap do |result| raise(RuntimeError, "No county found for lookup '#{county}' in state #{state.name}") unless result yield result if block_given? end end |