Class: GetYourRep::Representative

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Associations

#add_child

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

#committeesObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def committees
  @committees
end

#delegationObject (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

#emailObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def email
  @email
end

#facebookObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def facebook
  @facebook
end

#googleplusObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def googleplus
  @googleplus
end

#middle_nameObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def middle_name
  @middle_name
end

#nameObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def name
  @name
end

#officeObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def office
  @office
end

#partyObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def party
  @party
end

#phonesObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def phones
  @phones
end

#photoObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def photo
  @photo
end

#twitterObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def twitter
  @twitter
end

#urlObject

Rep personal attributes.



11
12
13
# File 'lib/get_your_rep/representative.rb', line 11

def url
  @url
end

#youtubeObject

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_officesObject

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_locationsObject

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_infoObject

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_displayObject

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_committeesObject

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_officesObject

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_nameObject

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_nameObject



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_nameObject

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_arrayObject

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_countObject

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_locationsObject

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_nameObject



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_nameObject



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