Created
November 9, 2012 18:12
-
-
Save dyoung522/4047254 to your computer and use it in GitHub Desktop.
Ruby on Rails 3 model to serve US States without a database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class State | |
include ActiveModel::Conversion | |
extend ActiveModel::Naming | |
attr_reader :all | |
US_STATES = { | |
AL: "Alabama", | |
AK: "Alaska", | |
AZ: "Arizona", | |
AR: "Arkansas", | |
CA: "California", | |
CO: "Colorado", | |
CT: "Connecticut", | |
DE: "Delaware", | |
DC: "District of Columbia", | |
FL: "Florida", | |
GA: "Georgia", | |
HI: "Hawaii", | |
ID: "Idaho", | |
IL: "Illinois", | |
IN: "Indiana", | |
IA: "Iowa", | |
KS: "Kansas", | |
KY: "Kentucky", | |
LA: "Louisiana", | |
ME: "Maine", | |
MD: "Maryland", | |
MA: "Massachusetts", | |
MI: "Michigan", | |
MN: "Minnesota", | |
MS: "Mississippi", | |
MO: "Missouri", | |
MT: "Montana", | |
NE: "Nebraska", | |
NV: "Nevada", | |
NH: "New Hampshire", | |
NJ: "New Jersey", | |
NM: "New Mexico", | |
NY: "New York", | |
NC: "North Carolina", | |
ND: "North Dakota", | |
OH: "Ohio", | |
OK: "Oklahoma", | |
OR: "Oregon", | |
PA: "Pennsylvania", | |
RI: "Rhode Island", | |
SC: "South Carolina", | |
SD: "South Dakota", | |
TN: "Tennessee", | |
TX: "Texas", | |
UT: "Utah", | |
VT: "Vermont", | |
VA: "Virginia", | |
WV: "Washington", | |
WV: "West Virginia", | |
WI: "Wisconsin", | |
WY: "Wyoming" | |
} | |
SE_STATES = [ :AL, :FL, :GA, :KY, :MS, :NC, :SC, :TN, :VA ] | |
# If we initialize the class, return a hash with all the states | |
def initialize | |
@all = US_STATES | |
end | |
def to_hash | |
@all | |
end | |
def to_a | |
@all.keys.sort | |
end | |
def to_s | |
@all.values.sort.to_sentence | |
end | |
def persisted? | |
false | |
end | |
# Class Methods | |
class << self | |
def all | |
US_STATES | |
end | |
def southeast | |
US_STATES.slice( *SE_STATES ) | |
end | |
def method_missing(method_id, *arguments, &block) | |
state = method_id.to_s.titleize | |
state_sym = method_id.to_s.upcase.to_sym | |
if US_STATES.has_key?(state_sym) | |
{ state_sym => US_STATES[state_sym] } | |
elsif US_STATES.has_value?(state) | |
{ US_STATES.key(state) => state } | |
else | |
super | |
end | |
end | |
def respond_to?(method_id, include_private = false) | |
if US_STATES.has_key?(method_id.to_s.upcase.to_sym) or US_STATES.has_value?(method_id.to_s.titleize) | |
true | |
else | |
super | |
end | |
end | |
alias_method :se, :southeast | |
alias_method :SE, :southeast | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can call the class methods directly to get hash slices, e.g:
State.all => Simply returns the entire US_STATES hash
State.southeast => Returns a hash of just the SouthEastern states (also aliased to State.se)
State.abbr or State.name => Returns a hash slice containing just the state requested ( i.e. State.nc or State.north_carolina => {:NC => "North Carolina"} )