Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercise completed #2

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log
22 changes: 22 additions & 0 deletions lib/phonebook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,26 @@ class Phonebook
def initialize(contacts)
@contacts = contacts
end

def contacts_names
contacts.map(&:name)
end

def contacts_phone_numbers
contacts.map(&:phone_numbers).flatten
end

def contacts_info
contacts.inject({}) do |result, contact|
result.merge(contact.name => contact.phone_numbers)
end
end

def every_contact_has_phone_number?
contacts.all? { |contact| !contact.phone_numbers.nil?}
end

def contacts_without_phone_number
contacts.select { |contact| contact.phone_numbers.nil? }
end
end
23 changes: 22 additions & 1 deletion spec/lib/phonebook_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
require 'spec_helper'
require './lib/phonebook'
require './lib/contact'

describe Phonebook do
let(:contacts) { [double('first contact'), double('second contact')] }
let(:contact1) { double('first contact', name: 'first name', phone_numbers: ['3217889909', '6733']) }
let(:contact2) { double('second contact', name: 'second name', phone_numbers: ['32456563']) }
let(:contacts) { [contact1, contact2] }
let(:phonebook) { Phonebook.new(contacts) }

describe "#contacts" do
it "returns assigend contacts" do
expect(phonebook.contacts).to eq(contacts)
end
end

describe "#contact_names" do
it "returns list of names" do
expect(phonebook.contacts_names).to eq([contact1.name, contact2.name])
end
end

describe "#contact_phone_numbers" do
it "returns list of contact's numbers" do
expect(phonebook.contacts_phone_numbers).to eq([contact1.phone_numbers[0], contact1.phone_numbers[1], contact2.phone_numbers[0]])
end
end

describe "#every_contact_has_number" do
it "returns that every contact has numbers" do
expect(phonebook.every_contact_has_number?).to be_true
end
end
end