Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- entry.rb
- ========
- class Entry
- attr_accessor :name, :phone_number, :email
- def initialize(name, phone_number, email)
- @name = name
- @phone_number = phone_number
- @email = email
- end
- def to_s
- "Name: #{@name}\nPhone Number: #{@phone_number}\nEmail: #{@email}"
- end
- end
- address_book.rb
- ================
- require_relative 'entry'
- require "csv"
- class AddressBook
- attr_accessor :entries
- def initialize
- @entries = []
- end
- def add_entry(name, phone_number, email)
- # #9
- index = 0
- @entries.each do |entry|
- # #10
- if name < entry.name
- break
- end
- index += 1
- end
- # #11
- @entries.insert(index, Entry.new(name, phone_number, email))
- end
- def remove_entry(name, phone_number, email)
- # #9
- @entries.each_with_index do |entry, index|
- # #10
- if name == entry.name
- @entries.delete(index)
- end
- end
- end
- def import_from_csv(file_name)
- csv_text = File.read(file_name)
- csv = CSV.parse(csv_text, headers: true, skip_blanks: true)
- # #8
- csv.each do |row|
- row_hash = row.to_hash
- add_entry(row_hash["name"], row_hash["phone_number"], row_hash["email"])
- end
- # Implementation goes here
- end
- end
- entry_spec.rb
- ==============
- require_relative '../models/entry'
- ## 1
- RSpec.describe Entry do
- ## 2
- describe "attributes" do
- ## 3
- it "should respond to name" do
- entry = Entry.new('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
- ## 4
- expect(entry).to respond_to(:name)
- end
- it "should respond to phone_num" do
- entry = Entry.new('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
- expect(entry).to respond_to(:phone_num)
- end
- it "should respond to email" do
- entry = Entry.new('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
- expect(entry).to respond_to(:email)
- end
- end
- describe "#to_s" do
- it "prints an entry a string" do
- entry = Entry.new('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
- expected_string = "Name: Ada Lovelace\nPhone Number: 010.012.1815\nEmail: augusta.king@lovelace.com"
- expect(entry.to_s).to eq(expected_string)
- end
- end
- end
- address-bloc.rb
- ================
- require_relative '../controllers/menu_controller'
- # #4
- menu = MenuController.new
- # #5
- system "clear"
- puts "Welcome to AddressBloc!"
- # #6
- menu.main_menu
- menu_controller.rb
- ====================
- require_relative '../models/address_book'
- class MenuController
- attr_accessor :address_book
- def initialize
- @address_book = AddressBook.new
- end
- def main_menu
- # #2
- puts "Main Menu - #{@address_book.entries.count} entries"
- puts "1 - View all entries"
- puts "2 - Create an entry"
- puts "3 - Search for an entry"
- puts "4 - Import entries from a CSV"
- puts "5 - View Entry Number n"
- puts "6 - Exit"
- print "Enter your selection: "
- # #3
- selection = gets.to_i
- puts "You picked #{selection}"
- case selection
- when 1
- system "clear"
- view_all_entries
- main_menu
- when 2
- system "clear"
- create_entry
- main_menu
- when 3
- system "clear"
- search_entries
- main_menu
- when 4
- system "clear"
- read_csv
- main_menu
- when 5
- system "clear"
- puts "Give the Entry Number:"
- entry_num = gets.to_i - 1
- puts entry_num
- @address_book.entries.each_with_index { |entry, index|
- if entry_num == index
- puts entry.to_s
- main_menu
- end
- }
- puts "Invalid entry"
- main_menu
- when 6
- puts "Good-bye!"
- # #8
- exit(0)
- # #9
- else
- system "clear"
- puts "Sorry, that is not a valid input"
- main_menu
- end
- end
- # #10
- def entry_submenu(entry)
- # #16
- puts "n - next entry"
- puts "d - delete entry"
- puts "e - edit this entry"
- puts "m - return to main menu"
- # #17
- selection = gets.chomp
- case selection
- # #18
- when "n"
- # #19
- when "d"
- when "e"
- # #20
- when "m"
- system "clear"
- main_menu
- else
- system "clear"
- puts "#{selection} is not a valid input"
- entries_submenu(entry)
- end
- end
- def view_all_entries
- # #14
- @address_book.entries.each do |entry|
- system "clear"
- puts entry.to_s
- # #15
- entry_submenu(entry)
- end
- system "clear"
- puts "End of entries"
- end
- def create_entry
- # #11
- system "clear"
- puts "New AddressBloc Entry"
- # #12
- print "Name: "
- name = gets.chomp
- print "Phone number: "
- phone = gets.chomp
- print "Email: "
- email = gets.chomp
- # #13
- @address_book.add_entry(name, phone, email)
- system "clear"
- puts "New entry created"
- end
- def search_entriescheck_point_20_assignment.git
- end
- def read_csv
- end
- end
- address_book_spec.rb
- ====================
- require_relative '../models/address_book'
- RSpec.describe AddressBook do
- let(:book) {AddressBook.new}
- def check_entry(entry, expected_name, expected_number, expected_email)
- expect(entry.name).to eql expected_name
- expect(entry.phone_number).to eql expected_number
- expect(entry.email).to eql expected_email
- end
- describe "#import_from_csv" do
- it "tests the csv import process" do
- book.import_from_csv("./address-bloc/entries_2.csv")
- book_size = book.entries.size
- # Check the size of the AddressBook.entries
- expect(book_size).to eql 3
- end
- it "imports the 1st entry" do
- book.import_from_csv("./address-bloc/entries_2.csv")
- # Check the first entry
- entry_one = book.entries[0]
- check_entry(entry_one, "Babu", "655-555-4854", "babu@blocmail.com")
- expect(entry_one.name).to eql "Babu"
- expect(entry_one.phone_number).to eql "655-555-4854"
- expect(entry_one.email).to eql "babu@blocmail.com"
- end
- it "imports the 2nd entry" do
- book.import_from_csv("./address-bloc/entries_2.csv")
- # Check the second entry
- entry_two = book.entries[1]
- check_entry(entry_two, "Jaya", "755-555-3660", "jaya@blocmail.com")
- expect(entry_two.name).to eql "Jaya"
- expect(entry_two.phone_number).to eql "755-555-3660"
- expect(entry_two.email).to eql "jaya@blocmail.com"
- end
- it "imports the 3rd entry" do
- book.import_from_csv("./address-bloc/entries_2.csv")
- # Check the third entry
- entry_three = book.entries[2]
- check_entry(entry_three, "Yasha", "716-555-5415", "yasha@blocmail.com")
- expect(entry_three.name).to eql "Yasha"
- expect(entry_three.phone_number).to eql "716-555-5415"
- expect(entry_three.email).to eql "yasha@blocmail.com"
- end
- end
- describe "#import_from_csv" do
- it "tests the csv import process" do
- book.import_from_csv("./address-bloc/entries.csv")
- book_size = book.entries.size
- # Check the size of the AddressBook.entries
- expect(book_size).to eql 5
- end
- it "imports the 1st entry" do
- book.import_from_csv("./address-bloc/entries.csv")
- # Check the first entry
- entry_one = book.entries[0]
- check_entry(entry_one, "Bill", "555-555-4854", "bill@blocmail.com")
- expect(entry_one.name).to eql "Bill"
- expect(entry_one.phone_number).to eql "555-555-4854"
- expect(entry_one.email).to eql "bill@blocmail.com"
- end
- it "imports the 2nd entry" do
- book.import_from_csv("./address-bloc/entries.csv")
- # Check the second entry
- entry_two = book.entries[1]
- check_entry(entry_two, "Bob", "555-555-5415", "bob@blocmail.com")
- expect(entry_two.name).to eql "Bob"
- expect(entry_two.phone_number).to eql "555-555-5415"
- expect(entry_two.email).to eql "bob@blocmail.com"
- end
- it "imports the 3rd entry" do
- book.import_from_csv("./address-bloc/entries.csv")
- # Check the third entry
- entry_three = book.entries[2]
- check_entry(entry_three, "Joe", "555-555-3660", "joe@blocmail.com")
- expect(entry_three.name).to eql "Joe"
- expect(entry_three.phone_number).to eql "555-555-3660"
- expect(entry_three.email).to eql "joe@blocmail.com"
- end
- it "imports the 4th entry" do
- book.import_from_csv("./address-bloc/entries.csv")
- # Check the fourth entry
- entry_four = book.entries[3]
- check_entry(entry_four, "Sally", "555-555-4646", "sally@blocmail.com")
- expect(entry_four.name).to eql "Sally"
- expect(entry_four.phone_number).to eql "555-555-4646"
- expect(entry_four.email).to eql "sally@blocmail.com"
- end
- it "imports the 5th entry" do
- book.import_from_csv("./address-bloc/entries.csv")
- # Check the fifth entry
- entry_five = book.entries[4]
- check_entry(entry_five, "Sussie", "555-555-2036", "sussie@blocmail.com")
- expect(entry_five.name).to eql "Sussie"
- expect(entry_five.phone_number).to eql "555-555-2036"
- expect(entry_five.email).to eql "sussie@blocmail.com"
- end
- end
- describe "attributes" do
- it "should respond to entries" do
- book = AddressBook.new
- expect(book).to respond_to(:entries)
- end
- it "should initialize entries as an array" do
- book = AddressBook.new
- expect(book.entries).to be_a(Array)
- end
- it "should initialize entries as empty" do
- book = AddressBook.new
- expect(book.entries.size).to eq(0)
- end
- end
- describe "#add_entry" do
- it "adds only one entry to the address book" do
- book = AddressBook.new
- book.add_entry('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
- expect(book.entries.size).to eq(1)
- end
- it "adds the correct information to entries" do
- book = AddressBook.new
- book.add_entry('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
- new_entry = book.entries[0]
- expect(new_entry.name).to eq('Ada Lovelace')
- expect(new_entry.phone_number).to eq '010.012.1815'
- expect(new_entry.email).to eq 'augusta.king@lovelace.com'
- end
- end
- describe "#remove_entry" do
- it "removes only one entry from the address book" do
- book = AddressBook.new
- book.remove_entry('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
- expect(book.entries.size).to eq(0)
- end
- end
- # Test that AddressBook's .import_from_csv() method is working as expected
- end
- entries.csv
- ===========
- name,phone_number,email
- Bob,555-555-5415,bob@blocmail.com
- Bill,555-555-4854,bill@blocmail.com
- Joe,555-555-3660,joe@blocmail.com
- Sally,555-555-4646,sally@blocmail.com
- Sussie,555-555-2036,sussie@blocmail.com
- entries_2.csv
- ==============
- name,phone_number,email
- Yasha,716-555-5415,yasha@blocmail.com
- Babu,655-555-4854,babu@blocmail.com
- Jaya,755-555-3660,jaya@blocmail.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement