Class: GetYourRep::Representative

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

Instance Method Summary collapse

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

#committeesObject

Rep personal attributes.



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

def committees
  @committees
end

#delegationObject

The Delegation object that the instance belongs to.



8
9
10
# File 'lib/get_your_rep/representative.rb', line 8

def delegation
  @delegation
end

#emailObject

Rep personal attributes.



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

def email
  @email
end

#facebookObject

Rep personal attributes.



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

def facebook
  @facebook
end

#googleplusObject

Rep personal attributes.



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

def googleplus
  @googleplus
end

#middle_nameObject

Rep personal attributes.



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

def middle_name
  @middle_name
end

#nameObject

Rep personal attributes.



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

def name
  @name
end

#officeObject

Rep personal attributes.



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

def office
  @office
end

#partyObject

Rep personal attributes.



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

def party
  @party
end

#phonesObject

Rep personal attributes.



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

def phones
  @phones
end

#photoObject

Rep personal attributes.



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

def photo
  @photo
end

#twitterObject

Rep personal attributes.



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

def twitter
  @twitter
end

#urlObject

Rep personal attributes.



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

def url
  @url
end

#youtubeObject

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_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
  @district_offices ||= office_locations.select { |office| office.type == 'capitol' }
end

#clear_office_locationsObject

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_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.



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_nameObject

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_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.



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