Class: Daywalker::TypeConverter
- Inherits:
-
Object
- Object
- Daywalker::TypeConverter
- Defined in:
- lib/daywalker/type_converter.rb
Overview
:nodoc:
Class Method Summary collapse
- .blank_to_nil(str) ⇒ Object
- .district_to_sym_or_i(district) ⇒ Object
- .gender_letter_to_sym(letter) ⇒ Object
- .normalize_conditions(conditions) ⇒ Object
- .party_letter_to_sym(letter) ⇒ Object
- .sym_or_i_to_district(sym) ⇒ Object
- .sym_to_gender_letter(sym) ⇒ Object
- .sym_to_party_letter(sym) ⇒ Object
- .sym_to_title_abbr(sym) ⇒ Object
- .title_abbr_to_sym(abbr) ⇒ Object
Class Method Details
.blank_to_nil(str) ⇒ Object
71 72 73 |
# File 'lib/daywalker/type_converter.rb', line 71 def self.blank_to_nil(str) str == '' ? nil : str end |
.district_to_sym_or_i(district) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/daywalker/type_converter.rb', line 28 def self.district_to_sym_or_i(district) case district when 'Junior Seat' then :junior_seat when 'Senior Seat' then :senior_seat when /^(\d+)$/ then $1.to_i else raise ArgumentError, "Unknown district #{district.inspect}. Only Junior Seat, Senior Seat, and numbers allowed." end end |
.gender_letter_to_sym(letter) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/daywalker/type_converter.rb', line 3 def self.gender_letter_to_sym(letter) case letter when 'M' then :male when 'F' then :female else raise ArgumentError, "Unknown gender #{letter.inspect}. Only M and F allowed." end end |
.normalize_conditions(conditions) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/daywalker/type_converter.rb', line 75 def self.normalize_conditions(conditions) if conditions[:district].kind_of?(Symbol) || conditions[:district].kind_of?(Fixnum) conditions[:district] = sym_or_i_to_district(conditions[:district]) end if conditions[:title].kind_of? Symbol conditions[:title] = sym_to_title_abbr(conditions[:title]) end move_value_in_hash(conditions, :official_rss_url, :official_rss) if conditions[:party].kind_of? Symbol conditions[:party] = sym_to_party_letter(conditions[:party]) end move_value_in_hash(conditions, :website_url, :website) move_value_in_hash(conditions, :fax_number, :fax) move_value_in_hash(conditions, :first_name, :firstname) move_value_in_hash(conditions, :middle_name, :middlename) move_value_in_hash(conditions, :last_name, :lastname) move_value_in_hash(conditions, :webform_url, :webform) if conditions[:gender].kind_of? Symbol conditions[:gender] = sym_to_gender_letter(conditions[:gender]) end conditions end |
.party_letter_to_sym(letter) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/daywalker/type_converter.rb', line 11 def self.party_letter_to_sym(letter) case letter when 'D' then :democrat when 'R' then :republican when 'I' then :independent else raise ArgumentError, "Unknown party #{letter.inspect}. Only D, R, and I allowed." end end |
.sym_or_i_to_district(sym) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/daywalker/type_converter.rb', line 37 def self.sym_or_i_to_district(sym) case sym when :junior_seat then 'Junior Seat' when :senior_seat then 'Senior Seat' when Fixnum then sym.to_s else raise ArgumentError, "Unknown district #{sym.inspect}. Only :junior_seat, :senior_seat, and Fixnum are allowed." end end |
.sym_to_gender_letter(sym) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/daywalker/type_converter.rb', line 63 def self.sym_to_gender_letter(sym) case sym when :male then 'M' when :female then 'F' else raise ArgumentError, "Unknown gender #{sym.inspect}" end end |
.sym_to_party_letter(sym) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/daywalker/type_converter.rb', line 54 def self.sym_to_party_letter(sym) case sym when :democrat then 'D' when :republican then 'R' when :independent then 'I' else raise ArgumentError, "Unknown party #{sym.inspect}" end end |
.sym_to_title_abbr(sym) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/daywalker/type_converter.rb', line 46 def self.sym_to_title_abbr(sym) case sym when :senator then 'Sen' when :representative then 'Rep' else raise ArgumentError, "Unknown title #{sym.inspect}" end end |
.title_abbr_to_sym(abbr) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/daywalker/type_converter.rb', line 20 def self.title_abbr_to_sym(abbr) case abbr when 'Sen' then :senator when 'Rep' then :representative else raise ArgumentError, "Unknown title #{abbr.inspect}. Only Sen and Rep allowed." end end |