diff --git a/movie.rb b/movie.rb index 3311d4b..d590c9f 100644 --- a/movie.rb +++ b/movie.rb @@ -1,2 +1,85 @@ class Movie + def initialize(title, params = {}) + @title = title + @year = params[:year] + @vhs = params[:vhs] + @dvd = params[:dvd] + @bluray = params[:bluray] + end + + def 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? + @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 + last = f.pop + formats_str = "" + formats_str << "#{f.join(', ')} and " unless f.empty? + formats_str << last + "Available in: #{formats_str}" + end + end + + def released_years_ago + return unless @year + + Time.now.year - @year + end end diff --git a/movie_printer.rb b/movie_printer.rb new file mode 100644 index 0000000..b93e6be --- /dev/null +++ b/movie_printer.rb @@ -0,0 +1,44 @@ +class MoviePrinter + def initialize(movie, width = 40) + @movie = movie + @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(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 diff --git a/movie_spec.rb b/movie_spec.rb index f1bad35..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 @@ -6,31 +7,111 @@ 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.info.should == "Life Aquatic. Available in: VHS, DVD, Blu-ray" + movie = Movie.new("Life Aquatic", {vhs: true, dvd: true, bluray: true}) + 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" 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 + + 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