Class: DigitalOpera::States

Inherits:
Object show all
Defined in:
lib/digital_opera/states.rb

Constant Summary collapse

US_STATES =
[
  ['Alabama', 'AL'],
  ['Alaska', 'AK'],
  ['Arizona', 'AZ'],
  ['Arkansas', 'AR'],
  ['California', 'CA'],
  ['Colorado', 'CO'],
  ['Connecticut', 'CT'],
  ['Delaware', 'DE'],
  ['District of Columbia', 'DC'],
  ['Florida', 'FL'],
  ['Georgia', 'GA'],
  ['Hawaii', 'HI'],
  ['Idaho', 'ID'],
  ['Illinois', 'IL'],
  ['Indiana', 'IN'],
  ['Iowa', 'IA'],
  ['Kansas', 'KS'],
  ['Kentucky', 'KY'],
  ['Louisiana', 'LA'],
  ['Maine', 'ME'],
  ['Maryland', 'MD'],
  ['Massachusetts', 'MA'],
  ['Michigan', 'MI'],
  ['Minnesota', 'MN'],
  ['Mississippi', 'MS'],
  ['Missouri', 'MO'],
  ['Montana', 'MT'],
  ['Nebraska', 'NE'],
  ['Nevada', 'NV'],
  ['New Hampshire', 'NH'],
  ['New Jersey', 'NJ'],
  ['New Mexico', 'NM'],
  ['New York', 'NY'],
  ['North Carolina', 'NC'],
  ['North Dakota', 'ND'],
  ['Ohio', 'OH'],
  ['Oklahoma', 'OK'],
  ['Oregon', 'OR'],
  ['Pennsylvania', 'PA'],
  ['Rhode Island', 'RI'],
  ['South Carolina', 'SC'],
  ['South Dakota', 'SD'],
  ['Tennessee', 'TN'],
  ['Texas', 'TX'],
  ['Utah', 'UT'],
  ['Vermont', 'VT'],
  ['Virginia', 'VA'],
  ['Washington', 'WA'],
  ['West Virginia', 'WV'],
  ['Wisconsin', 'WI'],
  ['Wyoming', 'WY']
]

Class Method Summary collapse

Class Method Details

.abbreviationsObject



87
88
89
# File 'lib/digital_opera/states.rb', line 87

def self.abbreviations
  to_hash.keys.sort
end

.find_abbreviation_by_name(name) ⇒ Object



99
100
101
# File 'lib/digital_opera/states.rb', line 99

def self.find_abbreviation_by_name(name)
  to_hash.detect{ |k, v| v == name.to_s.capitalize }.first
end

.find_name_by_abbreviation(abbr) ⇒ Object



95
96
97
# File 'lib/digital_opera/states.rb', line 95

def self.find_name_by_abbreviation(abbr)
  to_hash[abbr.to_s.upcase]
end

.namesObject



91
92
93
# File 'lib/digital_opera/states.rb', line 91

def self.names
  to_hash.values.sort
end

.to_collection(mapping = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/digital_opera/states.rb', line 57

def self.to_collection(mapping={})
  @collection = (
    states = US_STATES

    if mapping[:key].present? || mapping[:value].present?
      states =US_STATES.map do |state|
        key = state.last
        value = state.first

        if mapping[:value] == :abbr
          value = state.last
        elsif mapping[:value] == :name
          value = state.first
        end

        if mapping[:key] == :name
          key = state.first
        elsif mapping[:key] == :abbr
          key = state.last
        end

        [value, key]

      end
    end

    states.sort{|a, b| a.first <=> b.first }
  )
end

.to_hashObject



103
104
105
106
107
108
109
# File 'lib/digital_opera/states.rb', line 103

def self.to_hash
  @hash ||= (
    h = {}
    US_STATES.map{ |state| h[state.last] = state.first }
    h
  )
end