my_flicks 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/flicks ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/flicks/playlist'
3
+ require_relative '../lib/flicks/movie3d'
4
+
5
+ playlist1 = Flicks::Playlist.new "Kermit"
6
+ default_movie_file = File.join(File.dirname(__FILE__), 'movies.csv')
7
+ playlist1.load(ARGV.shift || default_movie_file)
8
+
9
+ playlist1.add_movie Movie3D.new 'glee',5,20
10
+
11
+ loop do
12
+ puts "\nHow many viewings? ('quit' to exit)"
13
+ answer = gets.chomp.downcase
14
+ case answer
15
+ when /^\d+$/
16
+ playlist1.play answer.to_i
17
+ when 'quit','exit'
18
+ playlist1.print_stats
19
+ break
20
+ else
21
+ puts "Please enter a number or 'quit'"
22
+ end
23
+ end
24
+
25
+ playlist1.save
data/bin/flicks.rb ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/flicks/playlist'
3
+ require_relative '../lib/flicks/movie3d'
4
+
5
+ playlist1 = Flicks::Playlist.new "Kermit"
6
+ default_movie_file = File.join(File.dirname(__FILE__), 'movies.csv'
7
+ playlist1.load(ARGV.shift || default_movie_file)
8
+
9
+ playlist1.add_movie Movie3D.new 'glee',5,20
10
+
11
+ loop do
12
+ puts "\nHow many viewings? ('quit' to exit)"
13
+ answer = gets.chomp.downcase
14
+ case answer
15
+ when /^\d+$/
16
+ playlist1.play answer.to_i
17
+ when 'quit','exit'
18
+ playlist1.print_stats
19
+ break
20
+ else
21
+ puts "Please enter a number or 'quit'"
22
+ end
23
+ end
24
+
25
+ playlist1.save
26
+
27
+
28
+
29
+ #if you want to run this like bin/flicks
30
+ # chmod +x bin/flicks
31
+ # don't forget the shebang (!) at the top of this file
32
+ # make sure the env is not messed up or you might get errors
data/bin/movies.csv ADDED
@@ -0,0 +1,3 @@
1
+ goonies,10
2
+ ghostbusters,9
3
+ goldfinger,0
@@ -0,0 +1,3 @@
1
+ batman,10
2
+ spiderman,0
3
+ superman,2
data/lib/flicks/die.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Die
2
+ def initialize
3
+ end
4
+ def roll
5
+ rand(1..6)
6
+ end
7
+ end
@@ -0,0 +1,67 @@
1
+ require_relative 'rankable'
2
+
3
+ module Flicks
4
+ class Movie
5
+ include Rankable
6
+ attr_accessor :title, :rank
7
+ def initialize title, rank=0
8
+ @title = title.capitalize
9
+ @rank = rank
10
+ @snack_carbs = Hash.new 0
11
+ #puts "Created a movie #{title} with a rank of #{rank}"
12
+ end
13
+
14
+ def self.from_csv line
15
+ title, rank = line.split(',')
16
+ Movie.new title,Integer(rank)
17
+ end
18
+
19
+ def to_csv
20
+ "#{@title},#{@rank}"
21
+ end
22
+
23
+ def to_s
24
+ listing
25
+ end
26
+
27
+ def weekday
28
+ current_time = Time.new
29
+ today = current_time.strftime("%A")
30
+ end
31
+
32
+ def listing
33
+ "#{@title} has a rank of #{@rank} (#{status})"
34
+ end
35
+
36
+ def carbs_consumed
37
+ @snack_carbs.values.reduce(0,:+)
38
+ end
39
+
40
+ def each_snack
41
+ @snack_carbs.each do |name, carbs|
42
+ snack = Snack.new name,carbs
43
+ yield snack
44
+ end
45
+ end
46
+
47
+ def ate_snack snack
48
+ @snack_carbs[snack.name] += snack.carbs
49
+ puts "#{@title} led to the consumption of #{snack.carbs} carbs from #{snack.name}"
50
+ puts "#{@title}'s snacks: #{@snack_carbs}"
51
+ end
52
+ end
53
+
54
+ #__FILE__ holds the current filename, $0 holds the current file being run
55
+ #so if this file is being run (ruby movie.rb), then this code will be executed
56
+ if __FILE__ == $0
57
+ movie = Movie.new "goonies", 10
58
+ puts movie
59
+
60
+ movie.thumbs_up
61
+ puts movie
62
+ puts movie.rank
63
+
64
+ movie.title="Tron"
65
+ puts movie
66
+ end
67
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'movie'
2
+
3
+ class Movie3D < Flicks::Movie
4
+
5
+ def initialize title,rank,wow_factor
6
+ @wow_factor = wow_factor
7
+ super title, rank
8
+ end
9
+ def show_effect
10
+ puts "WOW " * @wow_factor
11
+ end
12
+
13
+ def thumbs_up
14
+ @wow_factor.times { super }
15
+ end
16
+ end
17
+
18
+ if __FILE__ == $0
19
+ movie3d = Movie3D.new 'glee',5,20
20
+
21
+ puts movie3d
22
+
23
+ movie3d.thumbs_up
24
+ movie3d.show_effect
25
+
26
+ puts movie3d
27
+ end
@@ -0,0 +1,78 @@
1
+ require_relative 'movie'
2
+ require_relative 'reviewer'
3
+ require_relative 'snack_bar'
4
+ module Flicks
5
+ class Playlist
6
+ def initialize name
7
+ @name = name
8
+ @movies = []
9
+ end
10
+
11
+ def add_movie movie
12
+ #@movies << movie also works
13
+ @movies.push movie
14
+ end
15
+
16
+ def load from_file
17
+ File.readlines(from_file).each do |line|
18
+ add_movie Movie.from_csv(line)
19
+ end
20
+ end
21
+
22
+ def save to_file="movie_rankings.csv"
23
+ File.open(to_file,"w") do |file|
24
+ @movies.sort.each do |movie|
25
+ file.puts movie.to_csv
26
+ end
27
+ end
28
+ end
29
+
30
+ def play viewings=1
31
+ puts "\n#{@name}'s playlist"
32
+ puts @movies.sort
33
+
34
+ snacks = SnackBar::SNACKS
35
+ puts "\nThere are #{snacks.size} snacks available in the snack bar"
36
+
37
+ snacks.each { |snack| puts "#{snack.name} has #{snack.carbs} carbs."}
38
+
39
+ viewings.times do |count|
40
+ puts "\nViewing #{count+1}"
41
+ @movies.each do |movie|
42
+ Reviewer.review movie
43
+ snack = SnackBar.random
44
+ movie.ate_snack snack
45
+ puts movie
46
+ end
47
+ end
48
+ end
49
+
50
+ def total_carbs_consumed
51
+ @movies.reduce(0) { |sum, movie| sum + movie.carbs_consumed }
52
+ end
53
+
54
+ def print_stats
55
+ puts "\n#{@name}'s Stats:"
56
+
57
+ puts "#{total_carbs_consumed} total carbs consumed"
58
+
59
+ @movies.sort.each do |movie|
60
+ puts "\n#{movie.title}'s snack totals:"
61
+
62
+ movie.each_snack do |snack|
63
+ puts "#{snack.carbs} total #{snack.name} carbs"
64
+ end
65
+
66
+ puts " #{movie.carbs_consumed} grand total carbs"
67
+ end
68
+
69
+ hits, flops = @movies.partition { |movie| movie.hit? }
70
+
71
+ puts "\nHits:"
72
+ puts hits.sort
73
+
74
+ puts "\nFlops:"
75
+ puts flops.sort
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,25 @@
1
+ module Rankable
2
+ def thumbs_up
3
+ self.rank+=1
4
+ end
5
+
6
+ def thumbs_down
7
+ self.rank-=1
8
+ end
9
+
10
+ def hit?
11
+ self.rank >= 10
12
+ end
13
+
14
+ def status
15
+ hit? ? "hit" : "flop"
16
+ end
17
+
18
+ def <=> other
19
+ other.rank <=> self.rank
20
+ end
21
+
22
+ def normalized_rank
23
+ self.rank/10
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ module Reviewer
2
+
3
+ def self.review movie
4
+ number_rolled = roll_die
5
+ case number_rolled
6
+ when 5..6
7
+ movie.thumbs_up
8
+ puts "#{movie.title} got a thumbs up"
9
+ when 3..4
10
+ puts "#{movie.title} was skipped"
11
+ else
12
+ movie.thumbs_down
13
+ puts "#{movie.title} got a thumbs down"
14
+ end
15
+ end
16
+
17
+ def self.roll_die
18
+ rand(1..6)
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ Snack = Struct.new(:name, :carbs)
2
+
3
+ module SnackBar
4
+ SNACKS = [
5
+ Snack.new(:popcorn, 20),
6
+ Snack.new(:candy, 15),
7
+ Snack.new(:nachos, 40),
8
+ Snack.new(:pretzel, 10),
9
+ Snack.new(:soda, 5)
10
+ ]
11
+
12
+ def self.random
13
+ SNACKS.sample
14
+ end
15
+ end
16
+
17
+ if __FILE__ == $0
18
+ puts SnackBar::SNACKS
19
+ snack = SnackBar.random
20
+ puts "Enjoy your #{snack.name} "
21
+ end
@@ -0,0 +1,79 @@
1
+ require 'flicks/movie'
2
+ #rspec movie_spec.rb --color --format doc
3
+ #rspec auto adds lib/spec/ directory to rubys loadpath used by require
4
+
5
+ #To avoid output from method invocations being displayed to console
6
+ $stdout = StringIO.new
7
+
8
+ module Flicks
9
+ describe Movie do
10
+
11
+ before do
12
+ @initial_rank =10
13
+ @movie = Movie.new "goonies", @initial_rank
14
+ end
15
+
16
+ it "has a capitalized title" do
17
+ @movie.title.should == "Goonies"
18
+ end
19
+
20
+ it "has an initial rank" do
21
+ @movie.rank.should == 10
22
+ end
23
+
24
+ it "has a string representation" do
25
+ @movie.to_s.should == "Goonies has a rank of 10 (hit)"
26
+ end
27
+
28
+ it "increases rank by 1 when given a thumbs up" do
29
+ @movie.thumbs_up
30
+
31
+ @movie.rank.should == @initial_rank + 1
32
+ end
33
+
34
+ it "decreases rank by 1 when given a thumbs down" do
35
+ @movie.thumbs_down
36
+
37
+ @movie.rank.should == @initial_rank - 1
38
+ end
39
+
40
+ context "created with a default rank" do
41
+ before do
42
+ @movie = Movie.new "goonies"
43
+ end
44
+
45
+ it "has a rank of 0" do
46
+ @movie.rank.should == 0
47
+ end
48
+ end
49
+
50
+ context "with a rank of atleast 10" do
51
+ before do
52
+ @movie = Movie.new "goonies", 10
53
+ end
54
+
55
+ it "is a hit" do
56
+ @movie.hit?.should == true
57
+ end
58
+
59
+ it "has a hit status" do
60
+ @movie.status.should == "hit"
61
+ end
62
+ end
63
+
64
+ context "with a rank of less than 10" do
65
+ before do
66
+ @movie = Movie.new "goonies", 9
67
+ end
68
+
69
+ it "is not a hit" do
70
+ @movie.hit?.should == false
71
+ end
72
+
73
+ it "has a flop status" do
74
+ @movie.status.should == "flop"
75
+ end
76
+ end
77
+ end
78
+ end
79
+
@@ -0,0 +1,40 @@
1
+ require 'flicks/playlist'
2
+
3
+ #To avoid output from method invocations being displayed to console
4
+ $stdout = StringIO.new
5
+ module Flicks
6
+ describe Playlist do
7
+ before do
8
+ @playlist = Playlist.new "mark"
9
+ end
10
+
11
+ context "being played with one movie" do
12
+ before do
13
+ @initial_rank = 10
14
+ @movie = Movie.new "goonies", @initial_rank
15
+ @playlist.add_movie @movie
16
+ end
17
+
18
+ it "gives the movie a thumbs up if a high number is rolled" do
19
+ Reviewer.stub(:roll_die).and_return(5)
20
+ @playlist.play
21
+
22
+ @movie.rank.should == @initial_rank + 1
23
+ end
24
+
25
+ it "skips the movie if a medium number is rolled" do
26
+ Reviewer.stub(:roll_die).and_return(3)
27
+ @playlist.play
28
+
29
+ @movie.rank.should == @initial_rank
30
+ end
31
+
32
+ it "gives the movie a thumbs down if a low number is rolled" do
33
+ Reviewer.stub(:roll_die).and_return(1)
34
+ @playlist.play
35
+
36
+ @movie.rank.should == @initial_rank - 1
37
+ end
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: my_flicks
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Yanaros
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: File.read(File.join(File.dirname(__FILE__), 'README'))
31
+ email: mark.yanaros@gmail.com
32
+ executables:
33
+ - flicks
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - bin/flicks
38
+ - bin/flicks.rb
39
+ - bin/movies.csv
40
+ - bin/superhero_movies.csv
41
+ - lib/flicks/die.rb
42
+ - lib/flicks/movie.rb
43
+ - lib/flicks/movie3d.rb
44
+ - lib/flicks/playlist.rb
45
+ - lib/flicks/rankable.rb
46
+ - lib/flicks/reviewer.rb
47
+ - lib/flicks/snack_bar.rb
48
+ - spec/flicks/movie_spec.rb
49
+ - spec/flicks/playlist_spec.rb
50
+ homepage: ''
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '1.9'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.25
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Plays and reviews movies
74
+ test_files:
75
+ - spec/flicks/movie_spec.rb
76
+ - spec/flicks/playlist_spec.rb