Class: GetYourRep::Representative
- Inherits:
-
Object
- Object
- GetYourRep::Representative
- Includes:
- Errors
- Defined in:
- lib/get_your_rep/representative.rb
Overview
Stores rep info in key/value pairs, and makes values accessible by instance method.
Instance Attribute Summary collapse
-
#committees ⇒ Object
Rep personal attributes.
-
#delegation ⇒ Object
The Delegation object that the instance belongs to.
-
#email ⇒ Object
Rep personal attributes.
-
#facebook ⇒ Object
Rep personal attributes.
-
#googleplus ⇒ Object
Rep personal attributes.
-
#middle_name ⇒ Object
Rep personal attributes.
-
#name ⇒ Object
Rep personal attributes.
-
#office ⇒ Object
Rep personal attributes.
-
#party ⇒ Object
Rep personal attributes.
-
#phones ⇒ Object
Rep personal attributes.
-
#photo ⇒ Object
Rep personal attributes.
-
#twitter ⇒ Object
Rep personal attributes.
-
#url ⇒ Object
Rep personal attributes.
-
#youtube ⇒ Object
Rep personal attributes.
Instance Method Summary collapse
-
#add_office_location(other) ⇒ Object
Creates a new OfficeLocation association.
-
#capitol_offices ⇒ Object
Maps the offices with the :type attribute equal to ‘capitol’.
-
#clear_office_locations ⇒ Object
Empties the office_locations array.
-
#cli_basic_info ⇒ Object
Display basic info.
-
#cli_display ⇒ Object
Displays self for the CLI.
-
#cli_display_committees ⇒ Object
Display committee info.
-
#district_offices ⇒ Object
Maps the offices with the :type attribute equal to ‘district’.
-
#first_name ⇒ Object
Parse the first name from the :name.
-
#initialize(rep_hash = {}) ⇒ Representative
constructor
Set office_locations, phones, and email as empty arrays.
-
#last_name ⇒ Object
Parse the last name from the :name.
-
#name_array ⇒ Object
Splits the name into an array.
-
#name_count ⇒ Object
Counts the elements in the name array.
-
#office_locations ⇒ Object
Returns a frozen duplicate of the office_locations array.
-
#office_locations=(other) ⇒ Object
Assign an individual OfficeLocation, or an array of them.
Methods included from Errors
#address_type_error, #command_not_found_error, #not_a_del_error, #not_a_rep_error, #not_an_office_error, #reps_not_found_error
Constructor Details
#initialize(rep_hash = {}) ⇒ Representative
Set office_locations, phones, and email as empty arrays. Set the rest of the attributes from the options hash.
15 16 17 18 19 20 21 22 |
# File 'lib/get_your_rep/representative.rb', line 15 def initialize(rep_hash = {}) @office_locations = [] @phones = [] @email = [] rep_hash.each do |key, val| send("#{key}=", val) end end |
Instance Attribute Details
#committees ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def committees @committees end |
#delegation ⇒ Object
The Delegation object that the instance belongs to.
8 9 10 |
# File 'lib/get_your_rep/representative.rb', line 8 def delegation @delegation end |
#email ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def email @email end |
#facebook ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def facebook @facebook end |
#googleplus ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def googleplus @googleplus end |
#middle_name ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def middle_name @middle_name end |
#name ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def name @name end |
#office ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def office @office end |
#party ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def party @party end |
#phones ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def phones @phones end |
#photo ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def photo @photo end |
#twitter ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def twitter @twitter end |
#url ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def url @url end |
#youtube ⇒ Object
Rep personal attributes.
10 11 12 |
# File 'lib/get_your_rep/representative.rb', line 10 def youtube @youtube end |
Instance Method Details
#add_office_location(other) ⇒ Object
Creates a new OfficeLocation association. Sets self as the other’s rep if not done so already.
46 47 48 49 50 51 52 53 |
# File 'lib/get_your_rep/representative.rb', line 46 def add_office_location(other) if other.is_a?(OfficeLocation) @office_locations << other other.rep = self unless other.rep == self else not_an_office_error end end |
#capitol_offices ⇒ Object
Maps the offices with the :type attribute equal to ‘capitol’.
102 103 104 |
# File 'lib/get_your_rep/representative.rb', line 102 def capitol_offices @district_offices ||= office_locations.select { |office| office.type == 'capitol' } end |
#clear_office_locations ⇒ Object
Empties the office_locations array.
41 42 43 |
# File 'lib/get_your_rep/representative.rb', line 41 def clear_office_locations @office_locations.clear end |
#cli_basic_info ⇒ Object
Display basic info.
114 115 116 117 118 119 |
# File 'lib/get_your_rep/representative.rb', line 114 def cli_basic_info puts name.bold.blue puts " #{office}".red puts " #{party}".red phones.each { |phone| puts " #{phone}".red } end |
#cli_display ⇒ Object
Displays self for the CLI.
107 108 109 110 111 |
# File 'lib/get_your_rep/representative.rb', line 107 def cli_display cli_basic_info office_locations.each(&:cli_display) cli_display_committees end |
#cli_display_committees ⇒ Object
Display committee info.
122 123 124 125 126 |
# File 'lib/get_your_rep/representative.rb', line 122 def cli_display_committees return unless committees && !committees.empty? puts ' Committees'.bold.blue committees.each { |comm| puts " #{comm}".red } end |
#district_offices ⇒ Object
Maps the offices with the :type attribute equal to ‘district’.
97 98 99 |
# File 'lib/get_your_rep/representative.rb', line 97 def district_offices @district_offices ||= office_locations.select { |office| office.type == 'district' } end |
#first_name ⇒ Object
Parse the first name from the :name.
65 66 67 68 69 70 71 72 73 |
# File 'lib/get_your_rep/representative.rb', line 65 def first_name @first_name ||= if name_count == 1 nil elsif (name_count > 3) || (name_array[-2].downcase == name_array[-2]) name_array[0..-3].join(' ') else name_array[0..-2].join(' ') end end |
#last_name ⇒ Object
Parse the last name from the :name.
76 77 78 79 80 81 82 83 84 |
# File 'lib/get_your_rep/representative.rb', line 76 def last_name @last_name ||= if name_count == 1 name elsif (name_count > 3) || (name_array[-2].downcase == name_array[-2]) name_array[-2..-1].join(' ') else name_array.last end end |
#name_array ⇒ Object
Splits the name into an array.
87 88 89 |
# File 'lib/get_your_rep/representative.rb', line 87 def name_array @name_array ||= name.split end |
#name_count ⇒ Object
Counts the elements in the name array.
92 93 94 |
# File 'lib/get_your_rep/representative.rb', line 92 def name_count @name_count ||= name_array.size end |
#office_locations ⇒ Object
Returns a frozen duplicate of the office_locations array.
36 37 38 |
# File 'lib/get_your_rep/representative.rb', line 36 def office_locations @office_locations.dup.freeze end |
#office_locations=(other) ⇒ Object
Assign an individual OfficeLocation, or an array of them.
56 57 58 59 60 61 62 |
# File 'lib/get_your_rep/representative.rb', line 56 def office_locations=(other) if other.is_a?(Array) other.each { |val| add_office_location(val) } else add_office_location(other) end end |