Class: GetYourRep::Representative
- Inherits:
-
Object
- Object
- GetYourRep::Representative
- Includes:
- Associations, 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
readonly
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.
- #four_part_name ⇒ Object
-
#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.
- #parse_first_name ⇒ Object
- #parse_last_name ⇒ Object
Methods included from Associations
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.
16 17 18 19 20 21 22 23 |
# File 'lib/get_your_rep/representative.rb', line 16 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.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def committees @committees end |
#delegation ⇒ Object (readonly)
The Delegation object that the instance belongs to.
9 10 11 |
# File 'lib/get_your_rep/representative.rb', line 9 def delegation @delegation end |
#email ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def email @email end |
#facebook ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def facebook @facebook end |
#googleplus ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def googleplus @googleplus end |
#middle_name ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def middle_name @middle_name end |
#name ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def name @name end |
#office ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def office @office end |
#party ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def party @party end |
#phones ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def phones @phones end |
#photo ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def photo @photo end |
#twitter ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def twitter @twitter end |
#url ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 def url @url end |
#youtube ⇒ Object
Rep personal attributes.
11 12 13 |
# File 'lib/get_your_rep/representative.rb', line 11 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.
36 37 38 39 40 41 |
# File 'lib/get_your_rep/representative.rb', line 36 def add_office_location(other) add_child other: other, model: OfficeLocation, children: :office_locations, error: -> { not_an_office_error } 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 @capitol_offices ||= office_locations.select { |office| office.type == 'capitol' } end |
#clear_office_locations ⇒ Object
Empties the office_locations array.
31 32 33 |
# File 'lib/get_your_rep/representative.rb', line 31 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.
53 54 55 |
# File 'lib/get_your_rep/representative.rb', line 53 def first_name @first_name ||= parse_first_name end |
#four_part_name ⇒ Object
82 83 84 |
# File 'lib/get_your_rep/representative.rb', line 82 def four_part_name (name_count > 3) || (name_array[-2].downcase == name_array[-2]) end |
#last_name ⇒ Object
Parse the last name from the :name.
68 69 70 |
# File 'lib/get_your_rep/representative.rb', line 68 def last_name @last_name ||= parse_last_name 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.
26 27 28 |
# File 'lib/get_your_rep/representative.rb', line 26 def office_locations @office_locations.dup.freeze end |
#office_locations=(other) ⇒ Object
Assign an individual OfficeLocation, or an array of them.
44 45 46 47 48 49 50 |
# File 'lib/get_your_rep/representative.rb', line 44 def office_locations=(other) if other.is_a?(Array) other.each { |val| add_office_location(val) } else add_office_location(other) end end |
#parse_first_name ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/get_your_rep/representative.rb', line 57 def parse_first_name if name_count == 1 nil elsif four_part_name name_array[0..-3].join(' ') else name_array[0..-2].join(' ') end end |
#parse_last_name ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/get_your_rep/representative.rb', line 72 def parse_last_name if name_count == 1 name elsif four_part_name name_array[-2..-1].join(' ') else name_array.last end end |