Class: AnsiCodes::County

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_ansi, county_ansi, name) ⇒ County (private)

Note:

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

Create a new County instance.

Parameters:

  • state_ansi (String)

    the two-digit state ANSI code

  • county_ansi (String)

    the three-digit county ANSI code

  • name (String)

    the county name



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_codeString (readonly)

Returns the three digit ANSI code for this county.

Returns:

  • (String)

    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

#designationString (readonly)

Note:

some cities have no designation, in which case this will return the empty string

Returns the census designation (County, Parish, Municipio, etc.).

Returns:

  • (String)

    the census designation (County, Parish, Municipio, etc.)



16
17
18
# File 'lib/ansi_codes/county.rb', line 16

def designation
  @designation
end

#nameString (readonly)

Returns the county’s full name in title case.

Returns:

  • (String)

    the county’s full name in title case



10
11
12
# File 'lib/ansi_codes/county.rb', line 10

def name
  @name
end

#short_nameString (readonly)

Note:

some cities have no designation, so this attribute will be the same as #name

Returns the county name, minus the designation.

Returns:

  • (String)

    the county name, minus the designation



13
14
15
# File 'lib/ansi_codes/county.rb', line 13

def short_name
  @short_name
end

#stateState (readonly)

Returns the State that this county belongs to.

Returns:

  • (State)

    the State that this county belongs to



6
7
8
# File 'lib/ansi_codes/county.rb', line 6

def state
  @state
end

Class Method Details

.all(state = nil) ⇒ Array<County>

Returns all counties or all of a State‘s counties if one is provided.

Parameters:

  • state (State) (defaults to: nil)

    an optional State object to narrow the results

Returns:

  • (Array<County>)

    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

Parameters:

  • state (State, Fixnum, String)

    the state portion of the query. This method will accept a State object or anything that State.find will accept.

  • county (Fixnum, String)

    the county ANSI code or name to look up

Returns:

Raises:

  • (ArgumentError)

    if the county parameter is not a Fixnum or String, or if the state parameter is not a State, Fixnum, or String.

  • (RuntimeError)

    if no associated State or AnsiCodes::County is found



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