diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..397b4a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.log diff --git a/lib/phonebook.rb b/lib/phonebook.rb index 6693a1e..4fc3277 100644 --- a/lib/phonebook.rb +++ b/lib/phonebook.rb @@ -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 diff --git a/spec/lib/phonebook_spec.rb b/spec/lib/phonebook_spec.rb index b7daa32..3a0c3e0 100644 --- a/spec/lib/phonebook_spec.rb +++ b/spec/lib/phonebook_spec.rb @@ -1,8 +1,11 @@ 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 @@ -10,4 +13,22 @@ 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