Skip to content

Instantly share code, notes, and snippets.

@steven-ferguson
Created September 11, 2013 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steven-ferguson/6517979 to your computer and use it in GitHub Desktop.
Save steven-ferguson/6517979 to your computer and use it in GitHub Desktop.
class Address
attr_reader :contact_id, :street, :city, :state, :zip, :type, :full
def initialize(contact_id, street, city, state, zip, type="")
@contact_id = contact_id
@street = street
@city = city
@state = state
@zip = zip
@type = type
make_full_address
end
def make_full_address
@full = "#{street} #{city}, #{state} #{zip}"
end
def save
DB.exec("INSERT INTO addresses (contact_id, street, city, state, zip, type) VALUES (#{contact_id}, '#{street}', '#{city}', '#{state}', '#{zip}', '#{type}');")
end
def self.all
addresses = []
results = DB.exec("SELECT * FROM addresses;")
results.each do |result|
contact_id = result['contact_id']
street = result['street']
city = result['city']
state = result['state']
zip = result['zip']
type = result['type']
addresses << Address.new(contact_id, street, city, state, zip, type)
end
addresses
end
def ==(another_address)
full == another_address.full
end
def delete
DB.exec("DELETE FROM addresses WHERE contact_id = #{contact_id} AND street = '#{street}' AND city = '#{city}' AND state = '#{state}' AND zip = '#{zip}';")
end
end
require 'pg'
require './lib/contact'
require './lib/phone'
require './lib/email'
require './lib/address'
DB = PG.connect(:dbname => 'address_book')
def welcome
puts "\e[H\e[2J"
puts "Welcome to your address book!"
main_menu
end
def main_menu
puts "You can [A]dd a new contact, [V]iew all of your contacts,"
puts "[D]elete a contact, or e[X]it."
user_choice = gets.chomp
if user_choice.downcase == 'a'
add_contact
elsif user_choice.downcase == 'v'
view_all_contacts
elsif user_choice.downcase == 'd'
delete_contact
elsif user_choice.downcase == 'x'
puts "Good-Bye!"
else
puts "That is not a valid option."
main_menu
end
end
def add_contact
print "\n Enter a first name for your new contact: "
first_name = gets.chomp
print "\n Enter a last name for your new contact: "
last_name = gets.chomp
new_contact = Contact.new(first_name, last_name)
new_contact.save
puts "\n #{new_contact.first_name} #{new_contact.last_name} has been saved."
main_menu
end
def view_contacts
contacts = Contact.all
contacts.each_with_index do |contact, index|
puts "#{index + 1}. #{contact.first_name} #{contact.last_name}"
end
end
def view_all_contacts
contacts = Contact.all
contacts.each_with_index do |contact, index|
puts "#{index + 1}. #{contact.first_name} #{contact.last_name}"
end
puts "Select a contact by number to edit."
puts "\n"
contact_menu(contacts[gets.to_i - 1])
end
def delete_contact
view_contacts
print "\n\nEnter the number of the contact you would like to delete "
selected_contact_index = gets.to_i - 1
Contact.all[selected_contact_index].delete
puts "Contact deleted\n"
main_menu
end
def contact_menu(contact)
puts "You can add a [P]hone number, an [E]mail address,"
puts "a [M]ailing address, [V]iew contact information, ed[I]t contact information, [D]elete contact information, or [G]o back to the last menu."
user_choice = gets.chomp.downcase
if user_choice == 'p'
add_phone(contact)
elsif user_choice == 'e'
add_email(contact)
elsif user_choice == 'm'
add_address(contact)
elsif user_choice == 'v'
display_contact_information_all(contact)
elsif user_choice == 'd'
delete_contact_information(contact)
elsif user_choice == 'g'
main_menu
elsif user_choice == 'i'
edit_menu(contact)
else
puts "That is not a valid option"
contact_menu(contact)
end
end
def edit_menu(contact)
display_contact_information(contact)
puts "You can edit the following:"
puts "[F]irst name, [L]ast name, [N]umber"
user_choice = gets.chomp.downcase
if user_choice == 'f'
change_first_name(contact)
elsif user_choice == 'l'
change_last_name(contact)
elsif user_choice == 'n'
change_number(contact)
end
end
def change_first_name(contact)
print "What do you want to change the first name to?"
new_first_name = gets.chomp
contact.edit_first_name(new_first_name)
puts "\nFirst name was changed."
puts "\n"
contact_menu(contact)
end
def change_last_name(contact)
print "What do you want to change the last name to?"
new_last_name = gets.chomp
contact.edit_last_name(new_last_name)
puts "\nLast name was changed.\n"
contact_menu(contact)
end
def change_number(contact)
display_contact_phones(contact)
print "Which number do you want to change: "
user_choice = gets.to_i
phone = contact.phones[user_choice - 1]
puts "What would you like the number to be?"
phone.edit_number(gets.chomp)
puts "What is this numbers type?"
phone.edit_type(gets.chomp)
contact_menu(contact)
end
def add_phone(contact)
print "\n Enter the number for your new phone: "
number = gets.chomp
print "\n Please enter a type for the phone (work, home, mobile...):"
type = gets.chomp
new_phone = Phone.new(number, contact.id, type)
new_phone.save
puts "Phone has been saved"
contact_menu(contact)
end
def add_email(contact)
print "\n Enter the address for your new email: "
address = gets.chomp
print "\n Please enter a type for the email(work, personal, ect.):"
type = gets.chomp
new_email = Email.new(address, contact.id, type)
new_email.save
puts "Email has been saved"
contact_menu(contact)
end
def add_address(contact)
print "\n Enter the Street of your new address: "
street = gets.chomp
print "\n Enter the City of your new address: "
city = gets.chomp
print "\n Enter the State of your new address: "
state = gets.chomp
print "\n Enter the Zip of your new address: "
zip = gets.chomp
print "\n What kind of address is this? (home, work, etc.) "
type = gets.chomp
new_address = Address.new(contact.id, street, city, state, zip, type)
new_address.save
puts "Address has been saved"
contact_menu(contact)
end
def delete_contact_information(contact)
display_contact_information(contact)
puts "You can delete a [P]hone, [E]mail, or [A]ddress"
user_choice = gets.chomp.downcase
if user_choice == "p"
delete_phone(contact)
elsif user_choice == "e"
delete_email(contact)
elsif user_choice == "a"
delete_address(contact)
else
puts "I did not understand that."
delete_contact_information(contact)
end
end
def display_contact_information_all(contact)
display_contact_information(contact)
contact_menu(contact)
end
def display_contact_information(contact)
puts "\n#{contact.first_name} #{contact.last_name}"
puts "\t Phones: "
display_contact_phones(contact)
puts "\t Emails: "
display_contact_emails(contact)
puts "\t Addresses:"
display_contact_addresses(contact)
puts "\n\n"
end
def delete_phone(contact)
display_contact_phones(contact)
puts "Choose a phone number to delete."
user_choice = gets.to_i - 1
contact.phones[user_choice].delete
puts "Phone number has been deleted."
contact_menu(contact)
end
def delete_email(contact)
display_contact_emails(contact)
puts "Choose an email to delete"
user_choice = gets.to_i - 1
contact.emails[user_choice].delete
puts "Email has been deleted."
contact_menu(contact)
end
def delete_address(contact)
display_contact_addresses(contact)
puts "Choose an address to delete"
user_choice = gets.to_i - 1
contact.addresses[user_choice].delete
puts "Address has been deleted."
contact_menu(contact)
end
def display_contact_phones(contact)
contact.get_phones
contact.phones.each_with_index do |phone, index|
puts "\t\t #{index + 1}. #{phone.number} (#{phone.type})"
end
end
def display_contact_emails(contact)
contact.get_emails
contact.emails.each_with_index do |email, index|
puts "\t\t #{index + 1}. #{email.address} (#{email.type})"
end
end
def display_contact_addresses(contact)
contact.get_addresses
contact.addresses.each_with_index do |address, index|
puts "\t\t #{index + 1}. #{address.street} #{address.city}, #{address.state} #{address.zip} (#{address.type})"
end
end
welcome
require 'spec_helper'
describe Address do
it 'is initialized with a contact_id, street, city, state and zip' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786')
address.should be_an_instance_of Address
end
it 'can be initialized with a type' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address.should be_an_instance_of Address
end
it 'can tell you its contact_id' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address.contact_id.should eq 1
end
it 'can tell you its street' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address.street.should eq '123 South St'
end
it 'can tell you its city' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address.city.should eq 'Hood River'
end
it 'can tell you its state' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address.state.should eq 'OR'
end
it 'can tell you its zip' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address.zip.should eq '91786'
end
it 'can tell you its type' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address.type.should eq 'home'
end
it 'has a full address' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address.make_full_address
address.full.should eq '123 South St Hood River, OR 91786'
end
it 'is equal if the full address is the same' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address2 = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address.should eq address2
end
it 'can be saved to the database' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address.save
Address.all.should eq [address]
end
it 'can be deleted from the database' do
address = Address.new(1, '123 South St', 'Hood River', 'OR', '91786', 'home')
address.save
Address.all.first.delete
Address.all.should eq []
end
end
class Contact
attr_reader :first_name, :last_name, :id, :phones, :emails, :addresses
def initialize(first_name, last_name = "", id = nil)
@first_name = first_name
@last_name = last_name
@id = id
end
def ==(another_contact)
first_name == another_contact.first_name && last_name == another_contact.last_name
end
def save
DB.exec("INSERT INTO contacts (first_name, last_name) VALUES ('#{@first_name}', '#{@last_name}');")
end
def self.all
contacts = []
results = DB.exec("SELECT * FROM contacts;")
results.each do |result|
first_name = result['first_name']
last_name = result['last_name']
id = result['id'].to_i
contacts << Contact.new(first_name, last_name, id)
end
contacts
end
def delete
DB.exec("DELETE FROM contacts WHERE id = #{@id}")
end
def get_phones
@phones = []
results = DB.exec("SELECT * FROM phones WHERE contact_id = #{id}")
results.each do |result|
number = result['number']
type = result['type']
@phones << Phone.new(number, id, type)
end
end
def get_emails
@emails = []
results = DB.exec("SELECT * FROM emails WHERE contact_id = #{id}")
results.each do |result|
address = result['address']
type = result['type']
@emails << Email.new(address, id, type)
end
end
def get_addresses
@addresses = []
results = DB.exec("SELECT * FROM addresses WHERE contact_id = #{id};")
results.each do |result|
contact_id = result['contact_id']
street = result['street']
city = result['city']
state = result['state']
zip = result['zip']
type = result['type']
@addresses << Address.new(contact_id, street, city, state, zip, type)
end
end
def edit_first_name(new_first_name)
@first_name = new_first_name
DB.exec("UPDATE contacts SET first_name = '#{first_name}' WHERE id = '#{id}';")
end
def edit_last_name(new_last_name)
@last_name = new_last_name
DB.exec("UPDATE contacts SET last_name = '#{last_name}' WHERE id = '#{id}';")
end
end
require 'spec_helper'
describe Contact do
it 'is initialized with a first name' do
contact = Contact.new('Sam')
contact.should be_an_instance_of Contact
end
it 'can be initialized with a last name' do
contact = Contact.new('John', 'Doe')
contact.should be_an_instance_of Contact
end
it 'can be initialized with an id' do
contact = Contact.new('John', 'Doe', 1)
contact.should be_an_instance_of Contact
end
it 'can tell you its first name' do
contact = Contact.new("John")
contact.first_name.should eq "John"
end
it 'can tell you its last name' do
contact = Contact.new("John", "Doe")
contact.last_name.should eq "Doe"
end
it 'can tell you its id' do
contact = Contact.new("John", "Doe", 1)
contact.id.should eq 1
end
it 'can be save to a database' do
contact = Contact.new("John")
contact.save
Contact.all.should eq [contact]
end
it 'can be deleted from the database' do
contact = Contact.new("John", "Doe")
contact.save
Contact.all.first.delete
Contact.all.should eq []
end
it 'is equal if it has the same first and last name' do
contact = Contact.new("John", "Doe")
contact2 = Contact.new("John", "Doe")
contact.should eq contact2
end
it 'gets all the phones that belong to the contact' do
contact = Contact.new("John", "Doe", 1)
contact.save
contact = Contact.all.first
phone = Phone.new("1112223333", contact.id)
phone.save
contact.get_phones
contact.phones.should eq [phone]
end
it 'gets all the emails that belong to a contact' do
contact = Contact.new("John", "Doe", 1)
contact.save
contact = Contact.all.first
email = Email.new("test@test.com", contact.id)
email.save
contact.get_emails
contact.emails.should eq [email]
end
it 'gets all of the address that belong to a contact' do
contact = Contact.new("John", "Doe", 1)
contact.save
contact = Contact.all.first
address = Address.new(contact.id, "123 South St", "Hood River", "OR", "91786")
address.save
contact.get_addresses
contact.addresses.should eq [address]
end
it 'lets you edit its first name' do
contact = Contact.new("Mike", "Jones", 1)
contact.edit_first_name("James")
contact.first_name.should eq "James"
end
it 'saves the first name edit to the database' do
contact = Contact.new("Mike", "Jones", 1)
contact.save
Contact.all.first.edit_first_name("James")
Contact.all.first.first_name.should eq "James"
end
it 'lets you edit its last name' do
contact = Contact.new("Mike", "Jones", 1)
contact.edit_last_name("Brown")
contact.last_name.should eq "Brown"
end
it 'saves the last name edit to the database' do
contact = Contact.new("Mike", "Jones", 1)
contact.save
Contact.all.first.edit_last_name("Brown")
Contact.all.first.last_name.should eq "Brown"
end
end
class Email
attr_reader :address, :contact_id, :type
def initialize(address, contact_id, type="")
@address = address
@contact_id = contact_id
@type = type
end
def ==(another_email)
address == another_email.address
end
def save
DB.exec("INSERT INTO emails (address, contact_id, type) VALUES ('#{address}', #{contact_id}, '#{type}');")
end
def delete
DB.exec("DELETE FROM emails WHERE address = '#{address}' AND contact_id = #{contact_id};")
end
def self.all
emails = []
results = DB.exec("SELECT * FROM emails")
results.each do |result|
address = result['address']
contact_id = result['contact_id'].to_i
type = result['type']
emails << Email.new(address, contact_id, type)
end
emails
end
def edit_address(new_address)
DB.exec("UPDATE emails SET address = '#{new_address}' WHERE contact_id = #{contact_id} AND address = '#{address}';")
@address = new_address
end
end
require 'spec_helper'
describe Email do
it "is initialized with an address and a contact_id" do
email = Email.new("this@that.com", 1)
email.should be_an_instance_of Email
end
it "can be initialized with a type" do
email = Email.new("this@that.com", 1, 'work')
email.should be_an_instance_of Email
end
it "tells you its address" do
email = Email.new("this@that.com", 1)
email.address.should eq "this@that.com"
end
it "tells you its contact_id" do
email = Email.new("this@that.com", 1)
email.contact_id.should eq 1
end
it "tells you its type" do
email = Email.new("this@that.com", 1, "work")
email.type.should eq "work"
end
it "is equal if the address is the same" do
email = Email.new('this@that.com', 1)
email2 = Email.new('this@that.com', 1)
email.should eq email2
end
it "can be saved to the database" do
email = Email.new("this@that.com", 1 , "work")
email.save
Email.all.should eq [email]
end
it "can be deleted from the database" do
email = Email.new("this@that.com", 1, "work")
email.save
Email.all.first.delete
Email.all.should eq []
end
it 'lets you edit its address' do
email = Email.new("this@that.com", 1, "Work")
email.edit_address("those@that.com")
email.address.should eq "those@that.com"
end
it 'lets you save the address edit to the database' do
email = Email.new("this@that.com", 1, "work")
email.save
Email.all.first.edit_address("those@that.com")
Email.all.first.address.should eq "those@that.com"
end
end
class Phone
attr_reader :number, :contact_id, :type, :id
def initialize(number, contact_id, type = "", id = "")
@number = number
@contact_id = contact_id
@type = type
@id = id
end
def save
results = DB.exec("INSERT INTO phones (number, contact_id, type) VALUES ('#{number}', #{contact_id}, '#{type}') RETURNING id;")
set_id(results.first['id'].to_i)
end
def set_id(id)
@id = id
end
def self.all
phones = []
results = DB.exec("SELECT * FROM phones;")
results.each do |result|
number = result['number']
contact_id = result['contact_id'].to_i
type = result['type']
id = result['id'].to_i
phones << Phone.new(number, contact_id, type, id)
end
phones
end
def self.find(search_id)
result = DB.exec("SELECT * FROM phones WHERE id = #{search_id};")
number = result.first['number']
contact_id = result.first['contact_id'].to_i
type = result.first['type']
id = result.first['id'].to_i
Phone.new(number, contact_id, type, id)
end
def ==(another_phone)
number == another_phone.number && contact_id == another_phone.contact_id
end
def delete
DB.exec("DELETE FROM phones WHERE number = '#{number}' AND contact_id = #{contact_id}")
end
def edit_number(new_number)
DB.exec("UPDATE phones SET number = '#{new_number}' WHERE contact_id = #{contact_id} AND number = '#{number}'")
@number = new_number
end
def edit_type(new_type)
DB.exec("UPDATE phones SET type = '#{new_type}' WHERE contact_id = #{contact_id} AND number = '#{number}'")
@type = new_type
end
end
require 'spec_helper'
describe Phone do
it 'is initialized with a number and contact id' do
phone = Phone.new('281-330-8004', 1)
phone.should be_an_instance_of Phone
end
it 'can be initialized with a type and id' do
phone = Phone.new('281-330-8004', 1, 'work', 2)
phone.should be_an_instance_of Phone
end
it 'tells you its number' do
phone = Phone.new('281-330-8004', 1)
phone.number.should eq '281-330-8004'
end
it 'tells you its contact id' do
phone = Phone.new('281-330-8004', 1)
phone.contact_id.should eq 1
end
it 'tells you its type' do
phone = Phone.new('281-330-8004', 1, 'work')
phone.type.should eq 'work'
end
it 'tells you its id' do
phone = Phone.new('281-330-8004', 1, 'work', 1)
phone.id.should eq 1
end
it 'can be saved to a database' do
phone = Phone.new('281-330-8004', 1)
phone.save
Phone.all.should eq [phone]
end
it 'sets the id when saved' do
phone = Phone.new('281-330-8004', 1)
phone.save
phone.id.should be_an_instance_of Fixnum
end
it 'can be deleted from a database' do
phone = Phone.new('281-330-8004', 1, 'work')
phone2 = Phone.new('111-111-1111', 1)
phone.save
phone2.save
Phone.all.first.delete
Phone.all.should eq [phone2]
end
it 'lets you edit its number' do
phone = Phone.new('281-310-20392', 1)
phone.edit_number('281-793-0064')
phone.number.should eq '281-793-0064'
end
it 'lets you edit its type' do
phone = Phone.new('281-310-2039', 1, 'mobile')
phone.edit_type('home')
phone.type.should eq 'home'
end
it 'saves the type edit to the database' do
phone = Phone.new('222-222-2222', 1, 'mobile')
phone.save
phone.edit_type('home')
Phone.find(phone.id).type.should eq 'home'
end
it 'saves the number edit to the database' do
phone = Phone.new('111-111-1111', 1)
phone2 = Phone.new('222-222-2222', 1)
phone.save
Phone.all.first.edit_number('281-793-0064')
Phone.all.first.number.should eq '281-793-0064'
end
it 'finds phone with a matching id' do
phone = Phone.new('111-111-1111', 1, 'work')
phone.save
id = phone.id
Phone.find(id).should eq phone
end
it 'only changes one number' do
phone = Phone.new('111-111-1111', 1)
phone2 = Phone.new('222-222-2222', 1)
phone.save
phone2.save
phone.edit_number('281-793-0064')
phone_reloaded = Phone.find(phone.id)
phone2_reloaded = Phone.find(phone2.id)
phone2_reloaded.number.should eq '222-222-2222'
end
it 'is equal if it has the same number and contact id' do
phone1 = Phone.new('281-330-8004', 1)
phone2 = Phone.new('281-330-8004', 1)
phone1.should eq phone2
end
end
require 'rspec'
require 'pg'
require 'contact'
require 'phone'
require 'email'
require 'address'
DB = PG.connect(:dbname => 'address_book_test')
RSpec.configure do |config|
config.after(:each) do
DB.exec("DELETE FROM contacts *;")
DB.exec("DELETE FROM phones *;")
DB.exec("DELETE FROM emails *;")
DB.exec("DELETE FROM addresses *;")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment