Skip to content

Instantly share code, notes, and snippets.

@dkan
Created July 8, 2012 22:15
Show Gist options
  • Save dkan/3073149 to your computer and use it in GitHub Desktop.
Save dkan/3073149 to your computer and use it in GitHub Desktop.
ruby assessment
module AddressBook
class Person
attr_reader :first_name, :last_name, :email_addresses, :addresses, :phone_numbers
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
@email_addresses = []
@addresses = []
@phone_numbers = []
end
def add(field, contact_info)
case field
when EmailAddress
@email_addresses << field.new(contact_info)
when Address
@addresses << field.new(contact_info)
when PhoneNumber
@phone_numbers << field.new(contact_info)
end
end
def list(field)
puts field
end
end
class EmailAddress
attr_reader :email_address
def initialize(email_address)
# has an @email_address variable
end
def update
# updates @email_address variable
end
end
class Address
attr_reader :address
def initialize(address)
# has an @address variable
end
def update
# updates @address variable
end
end
class PhoneNumber
attr_reader :phone_number
def initialize(phone_number)
# has an @phone_number variable
end
def update
# updates @phone_number variable
end
end
end
require 'simplecov'
SimpleCov.start
require 'rspec'
require './address_book'
include AddressBook
describe 'AddressBook::Person' do
context '#initialize' do
before :each do
@person = Person.new('damien', 'kan')
end
it 'should create an instance of Person' do
@person.should be_an_instance_of Person
end
it 'should have a first and last name' do
@person.first_name.should eq 'damien'
@person.last_name.should eq 'kan'
end
end
context '#add' do
before :each do
@person = Person.new('damien', 'kan')
end
it 'should add email addresses to the person' do
@person.add('EmailAddress', '[email protected]')
@person.email_addresses[0].email_address.should include '[email protected]'
end
it 'should add addresses to the person' do
@person.add('Address', '123 Main street')
@person.addresses[0].address.should include '123 Main street'
end
it 'should add email addresses to the person' do
@person.add('PhoneNumber', '2066791302')
@person.phone_numbers[0].phone_number.should include '[email protected]'
end
end
context '#list' do
before :each do
@person = Person.new('damien', 'kan')
@person.add('Address', '123 Main street')
end
it 'should list specified field' do
@person.list(addresses)[0].address.should include '123 Main street'
@person.list(first_name).should include 'damien'
end
end
end
person
-------
id (primary key)
first_name
last_name
email_address
-------
id (primary key)
email_address
person_id (foreign key)
address
-------
id (primary key)
address
person_id (foreign key)
phone_number
-------
id (primary key)
phone_number
person_id (foreign key)
SELECT addresses.address FROM persons
JOIN addresses
ON addresses.person_id=persons.id
GROUP BY persons
WHERE persons.first_name = 'Barack'
AND persons.last_name = 'Obama'
SELECT * FROM persons
JOIN addresses
on addresses.person_id=persons.id
WHERE addresses.address LIKE '%California%'
INSERT INTO users (name, address) VALUES ('Barack Obama', '717 California Street, San Francisco CA, 94108')
@jfarmer
Copy link

jfarmer commented Jul 14, 2012

Make sure you run your code! The specs don't pass, and some of the tests were clearly oversights:

e.g.,

       it 'should add email addresses to the person' do
           @person.add('PhoneNumber', '2066791302')
           @person.phone_numbers[0].phone_number.should include '[email protected]'
       end

@jfarmer
Copy link

jfarmer commented Jul 14, 2012

Make sure you run your SQL, too. Your INSERT statement won't work at all -- you don't have a users table.

The GROUP BY persons line in your first query doesn't need to be there. The query is correct without it, but is syntactically invalid with it. Think carefully about what rows you want to return. I think you want one row per user with that name (regardless of whether they're the "same" person or not, e.g., "Barack H. Obama" and "Barack Q. Obama"). Grouping without an aggregate function like COUNT, SUM, etc. doesn't make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment