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 #10

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
### Quick Start
# Birthdays

Fork this repository to your github account and clone it to your machine. Then install the dependencies:
```bash
> git clone https://github.com/makersacademy/birthdays.git
> cd birthdays
> bundle
```
This repository contains the project I created working through the Birthdays TDD exercise during week 1 at Makers Academy. It was initially forked from a repository that contains a lot of scaffolding, a quick-start and instructions.

## Exercise instructions

### Instructions
- Test-drive an implementation of the given requirements
- Lint your code
- Open a Pull Request when you're finished

- Test-drive an implementation of the requirements
- Make sure your code is [linted](https://github.com/rubocop-hq/rubocop)
- [Open a PR](https://services.github.com/on-demand/github-cli/open-pull-request-github) when you've finished
## Requirements

### Requirements
I want a program that I can load in IRB that allows me to:

I want a program that I can load in IRB that allows me to
- Store all of my friends’ birthdays so I can keep track of them
- See them all at once with their names and birthdays each on a line in a tidy format
- Check whose birthday it is today - the program can check through the birthdays I have stored and check each one to see if it’s someone’s birthday, and tells me something like "It's Mary Poppin's birthday today! They are 124 years old!" - otherwise it won't say anything.

More requirements:

- Test-drive extracting a birthday class
- Isolate your birthday list class using a mock for Birthday

### TDD resources
### User stories

- https://github.com/makersacademy/course/blob/master/pills/tdd.md
- https://github.com/makersacademy/course/blob/master/pills/tdd_quality_discussion.md
```
As a user
I can store my friends' birthdays so I can keep track of them
```

### Mocking
```
As a user
I can see all of my friends' names and birthdays.
- it must print each friend on one line in a tidy format
```

- https://relishapp.com/rspec/rspec-mocks/docs/basics/test-doubles
```
As a user
I can check whose birthday it is today
- it must tell me something like ""It's Mary Poppin's birthday today! They are 124 years old!"
- if there are no birthdays for that day it must print nothing
```
11 changes: 11 additions & 0 deletions lib/birthday.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Birthday
attr_reader :date

def initialize(day, month, year)
@date = Time.new(year, month, day)
end

def to_s
"#{@date.day} #{@date.month} #{@date.year}"
end
end
43 changes: 43 additions & 0 deletions lib/birthday_list.rb
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
require_relative './birthday'

class BirthdayList
COLUMN_SPACER = "\t\t\t"

def initialize
@friends_and_birthdays = {}
end

def store(name, birthday)
@friends_and_birthdays[name] = birthday
"Birthday stored!"
end

def print_stored_friends
@friends_and_birthdays.each do |friend, birthday|
print_friend_and_birthday(friend, birthday)
end
end

def print_todays_birthdays
today = Time.now
@friends_and_birthdays.each do |friend, birthday|
if is_birthday_today?(birthday, today)
friend_age = calculate_friend_age(birthday, today)
puts "It's #{friend}'s birthday today! They are #{friend_age} years old!"
end
end
end

private

def print_friend_and_birthday(friend, birthday)
puts "#{friend}#{COLUMN_SPACER}#{birthday.to_s}"
end

def is_birthday_today?(birthday, today)
birthday.date.month == today.month && birthday.date.day == today.day
end

def calculate_friend_age(birthday, today)
today.year - birthday.date.year
end
end
45 changes: 45 additions & 0 deletions spec/birthday_list_spec.rb
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
require 'birthday_list'

describe BirthdayList do
let(:first_birthday) {double(:birthday, :date => Time.new(1977, 1, 1), :to_s => "1 1 1977")}
let(:second_birthday) {double(:birthday, :date => Time.new(1958, 8, 15), :to_s => "15 8 1958")}

it 'stores friends birthdays' do
expect(subject).to respond_to(:store).with(2).argument
end

it 'confirms that a friends birthday has been stored' do
expect(subject.store("Phil", first_birthday)).to eq("Birthday stored!")
end

it 'prints all of the stored friends and their birthdays, each on a single line with a tidy format when a single friend is stored' do
subject.store("Phil", first_birthday)
expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1 1 1977\n").to_stdout
end

it 'prints all stored friends and their birthdays, each on a single line with a tidy format when multiple friends are stored' do
subject.store("Phil", first_birthday)
subject.store("Steve", second_birthday)
expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1 1 1977\nSteve#{BirthdayList::COLUMN_SPACER}15 8 1958\n").to_stdout
end

it 'when you ask it to print todays birthdays and there are none, it prints nothing' do
subject.store("Phil", first_birthday)
expect { subject.print_todays_birthdays }.to output("").to_stdout
end

it 'when you ask it to print todays birthdays and there are some, it prints them' do
current_month = Time.now.month
current_date = Time.now.day

birthday_today_1 = double(:birthday, :date => Time.new(1956, current_month, current_date), :to_s => "#{current_date} #{current_month} #{1956}")
birthday_today_2 = double(:birthday, :date => Time.new(1984, current_month, current_date), :to_s => "#{current_date} #{current_month} #{1984}")

subject.store("Phil", birthday_today_1)
subject.store("Emma", first_birthday)
subject.store("Steve", birthday_today_2)

expected_output = "It's Phil's birthday today! They are #{Time.now.year - 1956} years old!\n"
expected_output << "It's Steve's birthday today! They are #{Time.now.year - 1984} years old!\n"
expect { subject.print_todays_birthdays }.to output(expected_output).to_stdout
end
end
24 changes: 24 additions & 0 deletions spec/birthday_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'birthday'

describe Birthday do
it 'stores a date when you create it' do
expect(Birthday).to respond_to(:new).with(3).argument
end

it 'tells you its date' do
birthday = Birthday.new(1, 9, 1977)
expect(birthday.date).to eq Time.new(1977, 9, 1)
end

it 'different birthdays tell you the correct dates' do
first_birthday = Birthday.new(1, 9, 1977)
second_birthday = Birthday.new(5, 8, 1985)
expect(first_birthday.date).to eq Time.new(1977, 9, 1)
expect(second_birthday.date).to eq Time.new(1985, 8, 5)
end

it 'can be converted to a human readable string' do
birthday = Birthday.new(1, 9, 1977)
expect(birthday.to_s).to eq "1 9 1977"
end
end