Skip to content

Commit ea04f69

Browse files
committed
add solution
1 parent 6dc73b0 commit ea04f69

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
66

77
gem "rspec"
88
gem "rubocop"
9+
gem 'timecop'

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ GEM
3131
ruby-progressbar (~> 1.7)
3232
unicode-display_width (~> 1.0, >= 1.0.1)
3333
ruby-progressbar (1.10.0)
34+
timecop (0.9.1)
3435
unicode-display_width (1.4.0)
3536

3637
PLATFORMS
@@ -39,6 +40,7 @@ PLATFORMS
3940
DEPENDENCIES
4041
rspec
4142
rubocop
43+
timecop
4244

4345
BUNDLED WITH
4446
1.16.3

_solution/lib/birthday_list.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'date'
2+
3+
class BirthdayList
4+
DATE_FORMAT = '%m-%d'
5+
6+
def initialize
7+
@list = []
8+
end
9+
10+
def add(name, date)
11+
@list << { name: name, birthday: date }
12+
end
13+
14+
def show
15+
@list.each do |bday|
16+
puts "#{bday[:name]}: #{bday[:birthday]}"
17+
end
18+
end
19+
20+
def check
21+
@list.each do |bday|
22+
puts "It's #{bday[:name]}'s birthday today! They are #{age(bday)} years old!" if today?(bday)
23+
end
24+
end
25+
26+
private
27+
28+
def age(bday)
29+
Time.now.year - Date.parse(bday[:birthday]).year
30+
end
31+
32+
def today?(bday)
33+
birthday_date(bday) == today
34+
end
35+
36+
def birthday_date(bday)
37+
Date.parse(bday[:birthday]).strftime(DATE_FORMAT)
38+
end
39+
40+
def today
41+
Time.now.strftime(DATE_FORMAT)
42+
end
43+
end

_solution/spec/birthday_list_spec.rb

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'birthday_list'
2+
require 'timecop'
3+
4+
RSpec.describe BirthdayList do
5+
# add my friends’ birthdays
6+
7+
it 'adds a birthday' do
8+
birthday_list = BirthdayList.new
9+
10+
expect(birthday_list.add('Guy Fawkes', "13 April 1570")).to eq [{ name: 'Guy Fawkes', birthday: "13 April 1570" }]
11+
end
12+
13+
it 'adds two birthdays' do
14+
birthday_list = BirthdayList.new
15+
birthday_list.add('Guy Fawkes', "13 April 1570")
16+
17+
expect(birthday_list.add('Julie Andrews', "1 October 1935")).to eq [{ name: 'Guy Fawkes', birthday: "13 April 1570" }, { name: 'Julie Andrews', birthday: "1 October 1935" }]
18+
end
19+
20+
# see them all there with their names
21+
22+
it 'shows all the birthdays' do
23+
birthday_list = BirthdayList.new
24+
birthday_list.add('Guy Fawkes', "13 April 1570")
25+
birthday_list.add('Julie Andrews', "1 October 1935")
26+
27+
expect { birthday_list.show }.to output("Guy Fawkes: 13 April 1570\nJulie Andrews: 1 October 1935\n").to_stdout
28+
end
29+
30+
# Check who’s birthday it is today
31+
32+
it 'checks birthdays for a birthday today' do
33+
Timecop.freeze(Time.parse('1 October 2018')) do
34+
35+
birthday_list = BirthdayList.new
36+
birthday_list.add('Guy Fawkes', "13 April 1570")
37+
birthday_list.add('Julie Andrews', "1 October 1935")
38+
39+
expect { birthday_list.check }.to output("It's Julie Andrews's birthday today! They are 83 years old!\n").to_stdout
40+
end
41+
end
42+
43+
it 'checks birthdays for a birthday - but there are none' do
44+
birthday_list = BirthdayList.new
45+
birthday_list.add('Guy Fawkes', "13 April 1570")
46+
birthday_list.add('Julie Andrews', "1 October 1935")
47+
48+
expect { birthday_list.check }.not_to output().to_stdout
49+
end
50+
end

0 commit comments

Comments
 (0)