-
Notifications
You must be signed in to change notification settings - Fork 25
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
Movie JSON submission #26
base: master
Are you sure you want to change the base?
Changes from 9 commits
745009b
c7a7953
0c115b4
8df31e7
ccfed8f
910010d
3b54798
32fddc6
b770147
527e834
048dce5
d3a2f34
86c4e24
b977711
12f27b0
7f81c59
5b786f0
78d49fd
cb1e2c0
f9814fd
a93af62
10a00ea
2e367b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'http://rubygems.org' | ||
|
||
|
||
gem 'rspec' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
diff-lcs (1.2.5) | ||
rspec (2.14.1) | ||
rspec-core (~> 2.14.0) | ||
rspec-expectations (~> 2.14.0) | ||
rspec-mocks (~> 2.14.0) | ||
rspec-core (2.14.7) | ||
rspec-expectations (2.14.5) | ||
diff-lcs (>= 1.1.3, < 2.0) | ||
rspec-mocks (2.14.5) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
rspec |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
require_relative './movie_library' | ||
class Movie | ||
|
||
@@movies = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, I don't think using a class variable makes 100% sense. Instead, you could have the caller keep track of the MovieLibrary and help simplify this code and your tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (actually, looks like you just need to remove old code from here) |
||
attr_reader :id, :title, :year, :score | ||
def initialize(hash={}) | ||
@id = hash.fetch(:id) | ||
@title = hash.fetch(:title) | ||
@year = hash.fetch(:year) | ||
@score = hash.fetch(:score) | ||
MovieLibrary.new.register(self) | ||
end | ||
|
||
def self.build(args) | ||
movie = Movie.new(args) | ||
@@movies << movie | ||
movie | ||
end | ||
|
||
def self.average_rating | ||
all_movies.inject(0.0) { |sum, movie| sum + movie.score } / all_movies.size | ||
end | ||
|
||
def self.count | ||
@@movies.count | ||
end | ||
|
||
def self.all_movies | ||
@@movies | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class MovieLibrary | ||
|
||
attr_reader :movies | ||
def initialize | ||
@movies = [] | ||
end | ||
|
||
def average_rating | ||
all_movies.inject(0.0) { |sum, movie| sum + movie.score } / all_movies.size | ||
end | ||
|
||
def register(movie) | ||
@movies << movie | ||
end | ||
|
||
def all_movies | ||
@movies | ||
end | ||
|
||
def count | ||
movies.count | ||
end | ||
end |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'rspec' | ||
require_relative "../lib/movie" | ||
require_relative "../lib/movie_library" | ||
describe MovieLibrary do | ||
|
||
let(:movie_library) {MovieLibrary.new} | ||
let (:movie){Movie.new(id: "the-id", title: "the-title", year: 1998, score: 50)} | ||
describe "#new" do | ||
|
||
it 'should be a movie library object ' do | ||
movie_library.is_a?(MovieLibrary).should be_true | ||
end | ||
|
||
it 'should have no movies when initialized' do | ||
movie_library.count.should eq(0) | ||
end | ||
end | ||
|
||
describe "#register" do | ||
|
||
it 'should add a new movie to the library whenever a search happens' do | ||
movie_library.register(movie) | ||
expect(movie_library.all_movies).to include (movie) | ||
end | ||
it 'should increase the movie count' do | ||
movie_library.register(movie) | ||
movie_library.count.should eq(1) | ||
end | ||
end | ||
|
||
describe "#average_rating" do | ||
it "should have no average rating initially" do | ||
movie_library.average_rating.should eq(0) | ||
end | ||
|
||
xit 'should calculate the average rating' do | ||
|
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,50 @@ | ||
require 'rspec' | ||
require_relative "../lib/movie" | ||
describe Movie do | ||
|
||
it "should store the title, year, and score" do | ||
movie = Movie.new(id: "the-id", title: "the-title", year: 1998, score: 50) | ||
movie.id.should eq("the-id") | ||
movie.title.should eq("the-title") | ||
movie.year.should eq(1998) | ||
movie.score.should eq(50) | ||
describe "#new" do | ||
|
||
let (:movie){Movie.new(id: "the-id", title: "the-title", year: 1998, score: 50)} | ||
|
||
it "should store the title, year, and score" do | ||
movie.id.should eq("the-id") | ||
movie.title.should eq("the-title") | ||
movie.year.should eq(1998) | ||
movie.score.should eq(50) | ||
end | ||
|
||
it 'should a movie object' do | ||
movie.is_a?(Movie).should be_true | ||
end | ||
|
||
it 'should not change the movie count' do | ||
Movie.count.should eq(0) | ||
end | ||
end | ||
|
||
describe ".build" do | ||
|
||
it "records the movie when I build it" do | ||
movie = Movie.build(id: 10020, title: 'Pirates of the Caribbean: The Curse of the Black Pearl', year: 2003, score: 79) | ||
expect(Movie.all_movies).to include (movie) | ||
end | ||
|
||
it "should increment the movie count" do | ||
expect{Movie.build(id: 10036, title: 'Forrest Gump', year: 1994, score: 71)}.to change{Movie.count}.from(1).to(2) | ||
end | ||
end | ||
|
||
describe ".average_rating" do | ||
it "should have no average rating initially" do | ||
#how do you mock this scenario | ||
end | ||
|
||
it 'should calculate the average rating while ignoring the Movie#build calls above' do | ||
#how do you change scope for RSpec object. Or rather how do you reset the Movie object instance variables? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Movie.new will always give you a new object. |
||
end | ||
|
||
it "should calculate the average rating for Forrest Gump and Pirates of the...as 75 " do | ||
Movie.average_rating.should eq(75) | ||
end | ||
end | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really know why this method exists.