Class: Flicks::Playlist

Inherits:
Object
  • Object
show all
Defined in:
lib/playlist.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Playlist

Returns a new instance of Playlist.



9
10
11
12
13
# File 'lib/playlist.rb', line 9

def initialize(name)
	@name = name.capitalize
	@movies = []
	puts "\n***#{@name}'s Playlist has been created***"
end

Instance Attribute Details

#moviesObject (readonly)

Returns the value of attribute movies.



7
8
9
# File 'lib/playlist.rb', line 7

def movies
  @movies
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/playlist.rb', line 7

def name
  @name
end

Instance Method Details

#add_movie(movie) ⇒ Object



38
39
40
41
# File 'lib/playlist.rb', line 38

def add_movie(movie)
	@movies << movie
	puts "::added #{movie.title}::"
end

#load_file(from_file) ⇒ Object



20
21
22
23
24
25
# File 'lib/playlist.rb', line 20

def load_file(from_file)
	puts "\n::::Loding File #{from_file.capitalize}::::"
	File.readlines(from_file).each do |line|
		add_movie(Movie.convert(line))
	end
end

#play(viewings) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/playlist.rb', line 43

def play(viewings)

	puts "\n\n~~Playing #{@name}'s playlist:~~"

	@movies.sort {|x,y| y.rank <=> x.rank}.each do |movie|
		puts "\tTrack: #{movie}"
	end

	snacks = SnackBar::SNACKS
	puts "\nThere are #{snacks.size} snacks available in the Snack Bar."
	snacks.sort {|x,y| y.carbs <=> x.carbs }.map.with_index(1) {|snack,i| puts "\t#{i}. #{snack.name.capitalize} - #{snack.carbs} carbs"}

	puts "\nStarting the Movie,Enjoy your show!!"
	1.upto(viewings) do |counter|
		puts "\nViewing: #{counter}"
		@movies.sort.each do |movie|
			Dok2AndQuiet.review(movie)
			snack = SnackBar.random
			movie.ate_snack(snack)
			puts movie
			puts "**************************************************"
		end
	end
end


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/playlist.rb', line 77

def print_stats
	puts "\n\n\nPlayList #{@name.upcase!}'s Stats: #{total_playlist_carbs} carbs - #{total_playlist_snacks} snacks"

	@movies.sort {|x,y| y.carbs_consumed <=> x.carbs_consumed }.map.with_index(1) do |movie,i| 
		puts "#{i}. #{movie.title} - #{movie.carbs_consumed} *total* grand carbs"
		movie.expose_hash {|key| puts "\t #{key.name} - #{key.carbs} carbs"}
	end

	hits, flops = @movies.partition {|m| m.hit? }

	puts "\nHits: "
	hits.sort.each do |movie| 
		puts "\t#{movie}"
	end


	puts "\nFlops: "
	flops.sort.each do |movie|
		puts "\t#{movie}"
	end
end

#save_file(to_file = "final_records") ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/playlist.rb', line 27

def save_file(to_file="final_records")
	puts "\n::::::Saving To File #{to_file.capitalize}::::::"
	File.open(to_file,"a+") do |file|
		@movies.sort.map.with_index(1) do |movie, i|
			file.puts("\t#{i}.#{movie.title},#{movie.rank}")
		end
		time = Time.new
		file.puts time.strftime("Printed on %m/%d/%Y at %I:%M%p")
	end
end

#total_playlist_carbsObject



68
69
70
# File 'lib/playlist.rb', line 68

def total_playlist_carbs
	@movies.reduce(0) {|sum,movie| sum + movie.carbs_consumed}
end

#total_playlist_snacksObject



72
73
74
# File 'lib/playlist.rb', line 72

def total_playlist_snacks 
	@movies.reduce(0) {|sum, movie| sum + movie.snack_carbs.keys.size}
end