From 5e9e74d7611409b80050908b85a1b1dd61c420bf Mon Sep 17 00:00:00 2001 From: Nicolas Gaivironsky Date: Sun, 13 Nov 2011 00:27:16 -0300 Subject: [PATCH 1/5] Solved: Spec 1 --- movie.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/movie.rb b/movie.rb index 3311d4b..815fdd5 100644 --- a/movie.rb +++ b/movie.rb @@ -1,2 +1,45 @@ class Movie + def initialize(title, vhs = false, dvd = false, bluray = false) + @title = title + @vhs = vhs + @dvd = dvd + @bluray = bluray + end + + def title + @title + end + + def vhs? + @vhs + end + + def dvd? + @dvd + end + + def bluray? + @bluray + end + + def info + "#{self.title}. #{self.availability}" + end + + def formats + f = [] + f << "VHS" if self.vhs? + f << "DVD" if self.dvd? + f << "Blu-ray" if self.bluray? + f + end + + def availability + f = self.formats + if f.empty? + "Not available" + else + "Available in: #{f.join(', ')}" + end + end end From 396c1ef1dd4b2ad21900d8e22eae7e521dedf101 Mon Sep 17 00:00:00 2001 From: Nicolas Gaivironsky Date: Sun, 13 Nov 2011 00:45:11 -0300 Subject: [PATCH 2/5] Spec 2: Add optional movie year and support rating --- movie_spec.rb | 69 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/movie_spec.rb b/movie_spec.rb index f1bad35..07a3d45 100644 --- a/movie_spec.rb +++ b/movie_spec.rb @@ -6,31 +6,88 @@ movie.title.should == "Life Aquatic" end - it "accepts args for available formats" do + it "accepts an optional year" do + movie = Movie.new("Groundhog Day", {year: 1993}) + movie.title.should == "Groundhog Day (1993)" + end + + it "returns the number of years since it was released" do + Time.stub_chain(:now, :year).and_return(2011) # Time.now.year => 2011 + movie = Movie.new("Ghostbusters", {year: 1984}) + movie.released_years_ago.should == 27 + + Time.stub_chain(:now, :year).and_return(2020) # Time.now.year => 2020 + movie = Movie.new("Ghostbusters", {year: 1984}) + movie.released_years_ago.should == 36 + + movie = Movie.new("Ghostbusters") + movie.released_years_ago.should be_nil + end + + it "accepts hash for available formats" do movie = Movie.new("Life Aquatic") movie.vhs?.should be_false movie.dvd?.should be_false movie.bluray?.should be_false - movie = Movie.new("Life Aquatic", true, false, false) + movie = Movie.new("Life Aquatic", {vhs: true}) movie.vhs?.should be_true movie.dvd?.should be_false movie.bluray?.should be_false - - movie = Movie.new("Life Aquatic", false, true, true) + + movie = Movie.new("Life Aquatic", {dvd: true, bluray: true, year: 2004}) movie.vhs?.should be_false movie.dvd?.should be_true movie.bluray?.should be_true end it "returns title and formats info" do - movie = Movie.new("Life Aquatic", false, true, false) + movie = Movie.new("Life Aquatic", {dvd: true}) movie.info.should == "Life Aquatic. Available in: DVD" - movie = Movie.new("Life Aquatic", true, true, true) + movie = Movie.new("Life Aquatic", {vhs: true, dvd: true, bluray: true}) movie.info.should == "Life Aquatic. Available in: VHS, DVD, Blu-ray" movie = Movie.new("Life Aquatic") movie.info.should == "Life Aquatic. Not available" end + + it "allows rating from 1 to 5" do + movie = Movie.new("Life Aquatic") + + movie.rating.should be_nil + + movie.rate(4) + movie.rating.should == 4 + + movie.rate(10) + movie.rating.should == 5 + + movie.rate(-2) + movie.rating.should == 1 + end + + it "shows rating as: Bad, Good, Excelent" do + movie = Movie.new("Life Aquatic") + + movie.in_one_word.should == "Unknown" + + movie.rate(1) + movie.in_one_word.should == "Bad" + + movie.rate(2) + movie.in_one_word.should == "Bad" + + movie.rate(3) + movie.in_one_word.should == "Good" + + movie.rate(4) + movie.in_one_word.should == "Good" + + movie.rate(5) + movie.in_one_word.should == "Excelent" + + # Tip: Use a case statement + end + end From 1b68804ca6367817fcf8a37089554d52f6476d3b Mon Sep 17 00:00:00 2001 From: Nicolas Gaivironsky Date: Sun, 13 Nov 2011 01:40:11 -0300 Subject: [PATCH 3/5] Solved: Spec 2 --- movie.rb | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/movie.rb b/movie.rb index 815fdd5..6e79b62 100644 --- a/movie.rb +++ b/movie.rb @@ -1,13 +1,43 @@ class Movie - def initialize(title, vhs = false, dvd = false, bluray = false) + def initialize(title, params = {}) @title = title - @vhs = vhs - @dvd = dvd - @bluray = bluray + @year = params[:year] + @vhs = params[:vhs] + @dvd = params[:dvd] + @bluray = params[:bluray] end def title - @title + str = @title + str << " (#{@year})" if @year + str + end + + def rate(stars) + @rating = if stars < 1 + 1 + elsif (1..5).member?(stars) + stars + else + 5 + end + end + + def rating + @rating + end + + def in_one_word + case @rating + when 1..2 + "Bad" + when 2..4 + "Good" + when 5 + "Excelent" + else + "Unknown" + end end def vhs? @@ -40,6 +70,12 @@ def availability "Not available" else "Available in: #{f.join(', ')}" - end + end + end + + def released_years_ago + return unless @year + + Time.now.year - @year end end From 0665c56be6536e3eddb7e03bef0702d304c571e9 Mon Sep 17 00:00:00 2001 From: Nicolas Gaivironsky Date: Sun, 13 Nov 2011 01:56:54 -0300 Subject: [PATCH 4/5] Spec 3: Pretty printing --- movie_printer.rb | 13 +++++++++++++ movie_spec.rb | 26 +++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 movie_printer.rb diff --git a/movie_printer.rb b/movie_printer.rb new file mode 100644 index 0000000..e90c018 --- /dev/null +++ b/movie_printer.rb @@ -0,0 +1,13 @@ +class MoviePrinter + def initialize(movie, width = 40) + @movie = movie + @width = width + end + + def print_box + "" + end + + def line(str, center = false) + end +end diff --git a/movie_spec.rb b/movie_spec.rb index 07a3d45..a0c09f4 100644 --- a/movie_spec.rb +++ b/movie_spec.rb @@ -1,4 +1,5 @@ require './movie.rb' +require './movie_printer.rb' describe Movie do it "returns its title" do @@ -46,7 +47,10 @@ movie.info.should == "Life Aquatic. Available in: DVD" movie = Movie.new("Life Aquatic", {vhs: true, dvd: true, bluray: true}) - movie.info.should == "Life Aquatic. Available in: VHS, DVD, Blu-ray" + movie.info.should == "Life Aquatic. Available in: VHS, DVD and Blu-ray" + + movie = Movie.new("Life Aquatic", {dvd: true, vhs: true}) + movie.info.should == "Life Aquatic. Available in: VHS and DVD" movie = Movie.new("Life Aquatic") movie.info.should == "Life Aquatic. Not available" @@ -90,4 +94,24 @@ # Tip: Use a case statement end + context "Pretty printing" do + it "prints a nice box" do + movie = Movie.new("Rushmore", {year: 1998, vhs: true}) + movie.rate(4) + + movie_printer = MoviePrinter.new(movie) + + box = movie_printer.print_box + + lines = box.split("\n") + + lines[0].should == '----------------------------------------' + lines[1].should == '| Rushmore (1998) |' + lines[2].should == '| |' + lines[3].should == '| Review: Good |' + lines[4].should == '| Available in: VHS |' + lines[5].should == '----------------------------------------' + end + end + end From 000a030813123c4ef9fa0b4412f6876169c6bc33 Mon Sep 17 00:00:00 2001 From: Nicolas Gaivironsky Date: Sun, 13 Nov 2011 02:28:52 -0300 Subject: [PATCH 5/5] Solved: Spec 3 --- movie.rb | 6 +++++- movie_printer.rb | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/movie.rb b/movie.rb index 6e79b62..d590c9f 100644 --- a/movie.rb +++ b/movie.rb @@ -69,7 +69,11 @@ def availability if f.empty? "Not available" else - "Available in: #{f.join(', ')}" + last = f.pop + formats_str = "" + formats_str << "#{f.join(', ')} and " unless f.empty? + formats_str << last + "Available in: #{formats_str}" end end diff --git a/movie_printer.rb b/movie_printer.rb index e90c018..b93e6be 100644 --- a/movie_printer.rb +++ b/movie_printer.rb @@ -1,13 +1,44 @@ class MoviePrinter def initialize(movie, width = 40) @movie = movie - @width = width + @width = 40 end def print_box - "" + str = "" + + str << self.border + str << self.line(@movie.title, true) + str << self.line("") + str << self.line("Review: #{@movie.in_one_word}") + str << self.line(@movie.availability) + str << self.border + + str + end + + def border + str = "-" * @width + str << "\n" + str end - def line(str, center = false) + def line(text, center = false) + str = "|" + extra = @width - 2 - text.length + + if center + extra = @width - 2 - text.length + left = extra / 2 + str << " " * left + str << text + else + left = 2 + str << " " * left + str << text + end + + str << " " * (extra - left) + str << "|\n" end end