diff --git a/README.md b/README.md new file mode 100644 index 0000000..0c6f4f3 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Ruby Course - Day 2 + +## Getting Started + + cd tdd_ruby_tutorial + git fetch + git checkout day2 + +## Exercise + +Implement the methods inside the class `MovieCatalog` in `movie_catalog.rb` according to the spec until it passes + + rspec movie_catalog_spec.rb + +Commit the results + + git commit -am "Implementation for day 2" + +*For help on how to handle collections check: * diff --git a/movie.rb b/movie.rb new file mode 100644 index 0000000..ed32675 --- /dev/null +++ b/movie.rb @@ -0,0 +1,28 @@ +class Movie + def initialize(title, params = {}) + @title = title + @year = params[:year] + @rating = params[:rating] + end + + def title + str = @title + str << " (#{self.year})" if @year + str + end + + def year + @year + end + + def rating + @rating + end + + def released_years_ago + return unless @year + + Time.now.year - @year + end +end + diff --git a/movie_catalog.rb b/movie_catalog.rb new file mode 100644 index 0000000..ea349c5 --- /dev/null +++ b/movie_catalog.rb @@ -0,0 +1,48 @@ +require './movie' + +class MovieCatalog + def initialize + load_movies + end + + def all + @movies + end + + def by_year(year) + self.all.select{|movie| movie.year == year} + end + + def top10 + self.all.sort_by{|movie| -movie.rating}[0..9] + end + + def counts_by_year + counts = {} + self.all.each do |movie| + if counts[movie.year] + counts[movie.year] += 1 + else + counts[movie.year] = 1 + end + end + counts + end + + protected + + def load_movies + @movies = [] + + File.open("./movies.csv", 'r') do |f| + f.each_line do |line| + cols = line.split(',') + year = cols[0].to_i + title = cols[1].gsub('"', '') + rating = cols[2].to_f + + @movies << Movie.new(title, {year: year, rating: rating}) + end + end + end +end diff --git a/movie_catalog_spec.rb b/movie_catalog_spec.rb new file mode 100644 index 0000000..fe63288 --- /dev/null +++ b/movie_catalog_spec.rb @@ -0,0 +1,41 @@ +require './movie_catalog.rb' + +describe MovieCatalog do + before(:each) do + @catalog = MovieCatalog.new + end + + it "has many movies" do + @catalog.all.size.should == 250 + end + + it "returns movies by year" do + movies_from_1990 = @catalog.by_year(1990) + movies_from_1990.should_not be_empty + movies_from_1990.all?{|movie| movie.year == 1990}.should be_true + + movies_from_1991 = @catalog.by_year(1991) + movies_from_1991.should_not be_empty + movies_from_1991.all?{|movie| movie.year == 1991}.should be_true + end + + it "returns top10 movies" do + top10 = @catalog.top10 + top10.size.should == 10 + + top10.first.title.should == "The Shawshank Redemption (1994)" + top10.last.title.should == "Star Wars: Episode V - The Empire Strikes Back (1980)" + end + + it "counts movies by year" do + counts = @catalog.counts_by_year + + counts.is_a?(Hash).should be_true + counts.keys.size.should == 80 + + counts[1995].should == 7 + counts[2005].should == 3 + counts[2010].should == 8 + counts[2012].should be_nil + end +end diff --git a/movie_spec.rb b/movie_spec.rb new file mode 100644 index 0000000..c6a102f --- /dev/null +++ b/movie_spec.rb @@ -0,0 +1,31 @@ +require './movie.rb' + +describe Movie do + it "returns its title" do + movie = Movie.new("Life Aquatic") + movie.title.should == "Life Aquatic" + end + + it "accepts an optional year" do + movie = Movie.new("Groundhog Day", {year: 1993}) + movie.title.should == "Groundhog Day (1993)" + end + + it "accepts an optional rating" do + movie = Movie.new("Groundhog Day", {rating: 9.9}) + movie.rating.should == 9.9 + 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 +end diff --git a/movies.csv b/movies.csv new file mode 100644 index 0000000..a2d4b36 --- /dev/null +++ b/movies.csv @@ -0,0 +1,250 @@ +1994,The Shawshank Redemption,9.2 +1972,The Godfather,9.2 +1974,The Godfather: Part II,9 +1994,Pulp Fiction,8.9 +1957,12 Angry Men,8.9 +1993,Schindler's List,8.9 +1975,One Flew Over the Cuckoo's Nest,8.8 +2008,The Dark Knight,8.8 +2003,The Lord of the Rings: The Return of the King,8.8 +1980,Star Wars: Episode V - The Empire Strikes Back,8.8 +2010,Inception,8.8 +1999,Fight Club,8.8 +1954,Seven Samurai,8.8 +1990,Goodfellas,8.7 +2001,The Lord of the Rings: The Fellowship of the Ring,8.7 +1977,Star Wars,8.7 +2002,City of God,8.7 +1942,Casablanca,8.7 +1968,Once Upon a Time in the West,8.7 +1999,The Matrix,8.7 +1954,Rear Window,8.7 +1981,Raiders of the Lost Ark,8.7 +1991,The Silence of the Lambs,8.7 +1995,The Usual Suspects,8.7 +1960,Psycho,8.6 +1995,Se7en,8.6 +1994,Forrest Gump,8.6 +2002,The Lord of the Rings: The Two Towers,8.6 +1946,It's a Wonderful Life,8.6 +2000,Memento,8.6 +1950,Sunset Blvd.,8.6 +1994,Leon: The Professional,8.6 +1964,Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb,8.6 +1979,Apocalypse Now,8.6 +1998,American History X,8.5 +1959,North by Northwest,8.5 +1941,Citizen Kane,8.5 +2010,Toy Story 3,8.5 +1999,American Beauty,8.5 +1991,Terminator 2: Judgment Day,8.5 +1998,Saving Private Ryan,8.5 +1976,Taxi Driver,8.5 +1979,Alien,8.5 +2001,Spirited Away,8.5 +1958,Vertigo,8.5 +1931,City Lights,8.5 +1980,The Shining,8.5 +1957,Paths of Glory,8.5 +2001,Amélie,8.5 +2002,The Pianist,8.5 +1931,M,8.5 +2008,WALL·E,8.5 +1944,Double Indemnity,8.4 +2006,The Departed,8.4 +2006,The Lives of Others,8.4 +1971,A Clockwork Orange,8.4 +1962,Lawrence of Arabia,8.4 +1986,Aliens,8.4 +1962,To Kill a Mockingbird,8.4 +1936,Modern Times,8.4 +2000,Requiem for a Dream,8.4 +1997,Life Is Beautiful,8.4 +1981,Das Boot,8.4 +2004,Eternal Sunshine of the Spotless Mind,8.4 +1985,Back to the Future,8.4 +1992,Reservoir Dogs,8.4 +1949,The Third Man,8.4 +1997,L.A. Confidential,8.4 +2006,The Prestige,8.4 +1974,Chinatown,8.4 +1948,The Treasure of the Sierra Madre,8.4 +1988,Cinema Paradiso,8.4 +1999,The Green Mile,8.4 +2005,The Life Aquatic with Steve Zissou,8.4 +1974,Monty Python and the Holy Grail,8.3 +1984,Once Upon a Time in America,8.3 +1950,Rashomon,8.3 +1940,The Great Dictator,8.3 +1980,Raging Bull,8.3 +1984,Amadeus,8.3 +1987,Full Metal Jacket,8.3 +1959,Some Like It Hot,8.3 +1952,Singin' in the Rain,8.3 +2000,Gladiator,8.3 +1950,All About Eve,8.3 +1948,Bicycle Thieves,8.3 +1957,The Bridge on the River Kwai,8.3 +2006,Pan's Labyrinth,8.3 +1995,Braveheart,8.3 +1968,2001: A Space Odyssey,8.3 +1927,Metropolis,8.3 +2003,Oldboy,8.3 +1960,The Apartment,8.3 +1992,Unforgiven,8.3 +2004,Downfall,8.3 +1973,The Sting,8.3 +2009,Inglourious Basterds,8.3 +1983,Star Wars: Episode VI - Return of the Jedi,8.3 +1997,Princess Mononoke,8.3 +1989,Indiana Jones and the Last Crusade,8.3 +1939,Mr. Smith Goes to Washington,8.3 +1980,The Elephant Man,8.3 +2009,Up,8.3 +2008,Gran Torino,8.3 +1954,On the Waterfront,8.3 +1941,The Maltese Falcon,8.3 +1988,Die Hard,8.3 +1940,Rebecca,8.3 +1963,The Great Escape,8.3 +1957,The Seventh Seal,8.3 +1988,Grave of the Fireflies,8.2 +2005,Batman Begins,8.2 +1994,The Lion King,8.2 +2005,Sin City,8.2 +1961,Yojimbo,8.2 +1995,Heat,8.2 +1965,For a Few Dollars More,8.2 +1926,The General,8.2 +1982,Blade Runner,8.2 +1996,Fargo,8.2 +1957,Witness for the Prosecution,8.2 +2010,Black Swan,8.2 +2000,Snatch.,8.2 +1985,Ran,8.2 +1957,Wild Strawberries,8.2 +1975,Jaws,8.2 +2010,The King's Speech,8.2 +2004,Hotel Rwanda,8.2 +1958,Touch of Evil,8.2 +2007,No Country for Old Men,8.2 +1998,The Big Lebowski,8.2 +1939,The Wizard of Oz,8.2 +1995,Toy Story,8.2 +1978,The Deer Hunter,8.2 +1999,The Sixth Sense,8.2 +1967,Cool Hand Luke,8.2 +1934,It Happened One Night,8.2 +1977,Annie Hall,8.2 +1951,Strangers on a Train,8.2 +2003,Kill Bill: Vol. 1,8.2 +1983,Scarface,8.2 +1986,Platoon,8.2 +2008,Slumdog Millionaire,8.2 +1952,High Noon,8.2 +2011,Drive,8.1 +1952,Ikiru,8.1 +2001,Donnie Darko,8.1 +2007,Into the Wild,8.1 +2011,A Separation,8.1 +1996,Trainspotting,8.1 +1925,The Gold Rush,8.1 +1969,Butch Cassidy and the Sundance Kid,8.1 +1953,The Wages of Fear,8.1 +1921,The Kid,8.1 +2004,Million Dollar Baby,8.1 +1946,Notorious,8.1 +1940,The Grapes of Wrath,8.1 +1939,Gone with the Wind,8.1 +2007,There Will Be Blood,8.1 +1998,Two Smoking Barrels,8.1 +2009,The Secret in Their Eyes,8.1 +1927,Sunrise: A Song of Two Humans,8.1 +2000,Amores Perros,8.1 +1982,The Thing,8.1 +1993,Groundhog Day,8.1 +1979,Life of Brian,8.1 +1995,Casino,8.1 +1959,Ben-Hur,8.1 +2003,Finding Nemo,8.1 +1967,The Graduate,8.1 +1984,The Terminator,8.1 +1946,The Big Sleep,8.1 +2009,District 9,8.1 +1946,The Best Years of Our Lives,8.1 +1955,Diabolique,8.1 +1986,Stand by Me,8.1 +2010,How to Train Your Dragon,8.1 +1962,The Manchurian Candidate,8.1 +1988,My Neighbor Totoro,8.1 +1995,Twelve Monkeys,8.1 +2006,V for Vendetta,8.1 +1975,Dog Day Afternoon,8.1 +2007,The Bourne Ultimatum,8.1 +2011,Harry Potter and the Deathly Hallows: Part 2,8.1 +1997,Good Will Hunting,8.1 +1961,Judgment at Nuremberg,8.1 +1982,Gandhi,8.1 +1976,Network,8.1 +2008,The Wrestler,8.1 +1955,The Night of the Hunter,8.1 +1959,The 400 Blows,8.1 +2000,High Fidelity,8.1 +2009,Mary and Max,8 +1966,The Battle of Algiers,8 +1963,8½,8 +1987,The Princess Bride,8 +1956,The Killing,8 +2007,Ratatouille,8 +1954,Dial M for Murder,8 +1966,Persona,8 +1961,The Hustler,8 +1954,La Strada,8 +2009,Avatar,8 +1966,Who's Afraid of Virginia Woolf?,8 +1973,The Exorcist,8 +2007,The Diving Bell and the Butterfly,8 +2009,Star Trek,8 +1969,The Wild Bunch,8 +1951,A Streetcar Named Desire,8 +1953,Stalag 17,8 +1982,Fanny and Alexander,8 +1928,The Passion of Joan of Arc,8 +2004,Howl's Moving Castle,8 +1975,Barry Lyndon,8 +1976,Rocky,8 +1949,Kind Hearts and Coronets,8 +1998,The Truman Show,8 +1930,All Quiet on the Western Front,8 +2010,Elite Squad: The Enemy Within,8 +1924,Sherlock Jr.,8 +2003,Mystic River,8 +2004,The Incredibles,8 +1968,Rosemary's Baby,8 +2008,Let the Right One In,8 +2008,In Bruges,8 +1957,Nights of Cabiria,8 +2002,Infernal Affairs,8 +1953,Roman Holiday,8 +1999,Magnolia,8 +1998,The Celebration,8 +2003,Big Fish,8 +2003,Pirates of the Caribbean: The Curse of the Black Pearl,8 +1948,Rope,8 +1979,Manhattan,8 +1994,Ed Wood,8 +2010,The Social Network,8 +1962,The Man Who Shot Liberty Valance,8 +2001,A Beautiful Mind,8 +2008,Ip Man,8 +2004,Kill Bill: Vol. 2,8 +2010,Shutter Island,8 +1953,Tokyo Story,8 +1979,Stalker,8 +2001,Monsters Inc.,8 +2006,Children of Men,8 +1970,Patton,8 +1972,Sleuth,8 +1984,Nausicaä of the Valley of the Wind,8 +1991,Beauty and the Beast,8 +1988,Rain Man,7.9