Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.85 KB | None | 0 0
  1.  
  2. entry.rb
  3. ========
  4. class Entry
  5. attr_accessor :name, :phone_number, :email
  6.  
  7. def initialize(name, phone_number, email)
  8. @name = name
  9. @phone_number = phone_number
  10. @email = email
  11. end
  12. def to_s
  13. "Name: #{@name}\nPhone Number: #{@phone_number}\nEmail: #{@email}"
  14. end
  15.  
  16. end
  17.  
  18.  
  19. address_book.rb
  20. ================
  21. require_relative 'entry'
  22. require "csv"
  23. class AddressBook
  24. attr_accessor :entries
  25. def initialize
  26. @entries = []
  27. end
  28. def add_entry(name, phone_number, email)
  29. # #9
  30. index = 0
  31. @entries.each do |entry|
  32. # #10
  33. if name < entry.name
  34. break
  35. end
  36. index += 1
  37. end
  38. # #11
  39. @entries.insert(index, Entry.new(name, phone_number, email))
  40. end
  41.  
  42. def remove_entry(name, phone_number, email)
  43. # #9
  44. @entries.each_with_index do |entry, index|
  45. # #10
  46. if name == entry.name
  47. @entries.delete(index)
  48. end
  49. end
  50. end
  51.  
  52. def import_from_csv(file_name)
  53. csv_text = File.read(file_name)
  54. csv = CSV.parse(csv_text, headers: true, skip_blanks: true)
  55. # #8
  56. csv.each do |row|
  57. row_hash = row.to_hash
  58. add_entry(row_hash["name"], row_hash["phone_number"], row_hash["email"])
  59. end
  60.  
  61. # Implementation goes here
  62.  
  63.  
  64. end
  65. end
  66.  
  67. entry_spec.rb
  68. ==============
  69. require_relative '../models/entry'
  70.  
  71. ## 1
  72. RSpec.describe Entry do
  73. ## 2
  74. describe "attributes" do
  75. ## 3
  76. it "should respond to name" do
  77. entry = Entry.new('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
  78. ## 4
  79. expect(entry).to respond_to(:name)
  80. end
  81.  
  82. it "should respond to phone_num" do
  83. entry = Entry.new('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
  84. expect(entry).to respond_to(:phone_num)
  85. end
  86.  
  87.  
  88. it "should respond to email" do
  89. entry = Entry.new('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
  90. expect(entry).to respond_to(:email)
  91. end
  92. end
  93. describe "#to_s" do
  94. it "prints an entry a string" do
  95. entry = Entry.new('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
  96. expected_string = "Name: Ada Lovelace\nPhone Number: 010.012.1815\nEmail: augusta.king@lovelace.com"
  97. expect(entry.to_s).to eq(expected_string)
  98. end
  99. end
  100. end
  101.  
  102. address-bloc.rb
  103. ================
  104. require_relative '../controllers/menu_controller'
  105.  
  106. # #4
  107. menu = MenuController.new
  108. # #5
  109. system "clear"
  110. puts "Welcome to AddressBloc!"
  111. # #6
  112. menu.main_menu
  113.  
  114. menu_controller.rb
  115. ====================
  116. require_relative '../models/address_book'
  117.  
  118. class MenuController
  119. attr_accessor :address_book
  120.  
  121. def initialize
  122. @address_book = AddressBook.new
  123. end
  124.  
  125. def main_menu
  126. # #2
  127. puts "Main Menu - #{@address_book.entries.count} entries"
  128. puts "1 - View all entries"
  129. puts "2 - Create an entry"
  130. puts "3 - Search for an entry"
  131. puts "4 - Import entries from a CSV"
  132. puts "5 - View Entry Number n"
  133. puts "6 - Exit"
  134. print "Enter your selection: "
  135.  
  136. # #3
  137. selection = gets.to_i
  138. puts "You picked #{selection}"
  139.  
  140. case selection
  141. when 1
  142. system "clear"
  143. view_all_entries
  144. main_menu
  145. when 2
  146. system "clear"
  147. create_entry
  148. main_menu
  149. when 3
  150. system "clear"
  151. search_entries
  152. main_menu
  153. when 4
  154. system "clear"
  155. read_csv
  156. main_menu
  157. when 5
  158. system "clear"
  159. puts "Give the Entry Number:"
  160. entry_num = gets.to_i - 1
  161. puts entry_num
  162. @address_book.entries.each_with_index { |entry, index|
  163. if entry_num == index
  164. puts entry.to_s
  165. main_menu
  166. end
  167. }
  168. puts "Invalid entry"
  169. main_menu
  170. when 6
  171. puts "Good-bye!"
  172. # #8
  173. exit(0)
  174. # #9
  175. else
  176. system "clear"
  177. puts "Sorry, that is not a valid input"
  178. main_menu
  179. end
  180. end
  181.  
  182. # #10
  183. def entry_submenu(entry)
  184. # #16
  185. puts "n - next entry"
  186. puts "d - delete entry"
  187. puts "e - edit this entry"
  188. puts "m - return to main menu"
  189.  
  190. # #17
  191. selection = gets.chomp
  192.  
  193. case selection
  194. # #18
  195. when "n"
  196. # #19
  197. when "d"
  198. when "e"
  199. # #20
  200. when "m"
  201. system "clear"
  202. main_menu
  203. else
  204. system "clear"
  205. puts "#{selection} is not a valid input"
  206. entries_submenu(entry)
  207. end
  208. end
  209. def view_all_entries
  210. # #14
  211. @address_book.entries.each do |entry|
  212. system "clear"
  213. puts entry.to_s
  214. # #15
  215. entry_submenu(entry)
  216. end
  217.  
  218. system "clear"
  219. puts "End of entries"
  220. end
  221.  
  222. def create_entry
  223. # #11
  224. system "clear"
  225. puts "New AddressBloc Entry"
  226. # #12
  227. print "Name: "
  228. name = gets.chomp
  229. print "Phone number: "
  230. phone = gets.chomp
  231. print "Email: "
  232. email = gets.chomp
  233.  
  234. # #13
  235. @address_book.add_entry(name, phone, email)
  236.  
  237. system "clear"
  238. puts "New entry created"
  239. end
  240.  
  241. def search_entriescheck_point_20_assignment.git
  242. end
  243.  
  244. def read_csv
  245. end
  246. end
  247.  
  248. address_book_spec.rb
  249. ====================
  250. require_relative '../models/address_book'
  251.  
  252. RSpec.describe AddressBook do
  253. let(:book) {AddressBook.new}
  254. def check_entry(entry, expected_name, expected_number, expected_email)
  255. expect(entry.name).to eql expected_name
  256. expect(entry.phone_number).to eql expected_number
  257. expect(entry.email).to eql expected_email
  258. end
  259.  
  260. describe "#import_from_csv" do
  261. it "tests the csv import process" do
  262. book.import_from_csv("./address-bloc/entries_2.csv")
  263. book_size = book.entries.size
  264.  
  265. # Check the size of the AddressBook.entries
  266. expect(book_size).to eql 3
  267. end
  268.  
  269. it "imports the 1st entry" do
  270. book.import_from_csv("./address-bloc/entries_2.csv")
  271. # Check the first entry
  272. entry_one = book.entries[0]
  273. check_entry(entry_one, "Babu", "655-555-4854", "babu@blocmail.com")
  274. expect(entry_one.name).to eql "Babu"
  275. expect(entry_one.phone_number).to eql "655-555-4854"
  276. expect(entry_one.email).to eql "babu@blocmail.com"
  277. end
  278.  
  279. it "imports the 2nd entry" do
  280. book.import_from_csv("./address-bloc/entries_2.csv")
  281. # Check the second entry
  282. entry_two = book.entries[1]
  283. check_entry(entry_two, "Jaya", "755-555-3660", "jaya@blocmail.com")
  284. expect(entry_two.name).to eql "Jaya"
  285. expect(entry_two.phone_number).to eql "755-555-3660"
  286. expect(entry_two.email).to eql "jaya@blocmail.com"
  287. end
  288.  
  289. it "imports the 3rd entry" do
  290. book.import_from_csv("./address-bloc/entries_2.csv")
  291. # Check the third entry
  292. entry_three = book.entries[2]
  293. check_entry(entry_three, "Yasha", "716-555-5415", "yasha@blocmail.com")
  294. expect(entry_three.name).to eql "Yasha"
  295. expect(entry_three.phone_number).to eql "716-555-5415"
  296. expect(entry_three.email).to eql "yasha@blocmail.com"
  297. end
  298. end
  299.  
  300.  
  301. describe "#import_from_csv" do
  302. it "tests the csv import process" do
  303. book.import_from_csv("./address-bloc/entries.csv")
  304. book_size = book.entries.size
  305.  
  306. # Check the size of the AddressBook.entries
  307. expect(book_size).to eql 5
  308. end
  309.  
  310. it "imports the 1st entry" do
  311. book.import_from_csv("./address-bloc/entries.csv")
  312. # Check the first entry
  313. entry_one = book.entries[0]
  314. check_entry(entry_one, "Bill", "555-555-4854", "bill@blocmail.com")
  315. expect(entry_one.name).to eql "Bill"
  316. expect(entry_one.phone_number).to eql "555-555-4854"
  317. expect(entry_one.email).to eql "bill@blocmail.com"
  318. end
  319.  
  320. it "imports the 2nd entry" do
  321. book.import_from_csv("./address-bloc/entries.csv")
  322. # Check the second entry
  323. entry_two = book.entries[1]
  324. check_entry(entry_two, "Bob", "555-555-5415", "bob@blocmail.com")
  325. expect(entry_two.name).to eql "Bob"
  326. expect(entry_two.phone_number).to eql "555-555-5415"
  327. expect(entry_two.email).to eql "bob@blocmail.com"
  328. end
  329.  
  330. it "imports the 3rd entry" do
  331. book.import_from_csv("./address-bloc/entries.csv")
  332. # Check the third entry
  333. entry_three = book.entries[2]
  334. check_entry(entry_three, "Joe", "555-555-3660", "joe@blocmail.com")
  335. expect(entry_three.name).to eql "Joe"
  336. expect(entry_three.phone_number).to eql "555-555-3660"
  337. expect(entry_three.email).to eql "joe@blocmail.com"
  338. end
  339.  
  340. it "imports the 4th entry" do
  341. book.import_from_csv("./address-bloc/entries.csv")
  342. # Check the fourth entry
  343. entry_four = book.entries[3]
  344. check_entry(entry_four, "Sally", "555-555-4646", "sally@blocmail.com")
  345. expect(entry_four.name).to eql "Sally"
  346. expect(entry_four.phone_number).to eql "555-555-4646"
  347. expect(entry_four.email).to eql "sally@blocmail.com"
  348. end
  349.  
  350. it "imports the 5th entry" do
  351. book.import_from_csv("./address-bloc/entries.csv")
  352. # Check the fifth entry
  353. entry_five = book.entries[4]
  354. check_entry(entry_five, "Sussie", "555-555-2036", "sussie@blocmail.com")
  355. expect(entry_five.name).to eql "Sussie"
  356. expect(entry_five.phone_number).to eql "555-555-2036"
  357. expect(entry_five.email).to eql "sussie@blocmail.com"
  358. end
  359. end
  360.  
  361. describe "attributes" do
  362. it "should respond to entries" do
  363. book = AddressBook.new
  364. expect(book).to respond_to(:entries)
  365. end
  366.  
  367. it "should initialize entries as an array" do
  368. book = AddressBook.new
  369. expect(book.entries).to be_a(Array)
  370. end
  371.  
  372. it "should initialize entries as empty" do
  373. book = AddressBook.new
  374. expect(book.entries.size).to eq(0)
  375. end
  376. end
  377. describe "#add_entry" do
  378. it "adds only one entry to the address book" do
  379. book = AddressBook.new
  380. book.add_entry('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
  381. expect(book.entries.size).to eq(1)
  382. end
  383.  
  384. it "adds the correct information to entries" do
  385. book = AddressBook.new
  386. book.add_entry('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
  387. new_entry = book.entries[0]
  388.  
  389. expect(new_entry.name).to eq('Ada Lovelace')
  390. expect(new_entry.phone_number).to eq '010.012.1815'
  391. expect(new_entry.email).to eq 'augusta.king@lovelace.com'
  392. end
  393. end
  394. describe "#remove_entry" do
  395. it "removes only one entry from the address book" do
  396. book = AddressBook.new
  397. book.remove_entry('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
  398. expect(book.entries.size).to eq(0)
  399. end
  400. end
  401. # Test that AddressBook's .import_from_csv() method is working as expected
  402. end
  403.  
  404. entries.csv
  405. ===========
  406. name,phone_number,email
  407. Bob,555-555-5415,bob@blocmail.com
  408. Bill,555-555-4854,bill@blocmail.com
  409. Joe,555-555-3660,joe@blocmail.com
  410. Sally,555-555-4646,sally@blocmail.com
  411. Sussie,555-555-2036,sussie@blocmail.com
  412.  
  413. entries_2.csv
  414. ==============
  415. name,phone_number,email
  416. Yasha,716-555-5415,yasha@blocmail.com
  417. Babu,655-555-4854,babu@blocmail.com
  418. Jaya,755-555-3660,jaya@blocmail.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement