Class: Flicks::Playlist

Inherits:
Object
  • Object
show all
Defined in:
lib/flicks/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
# File 'lib/flicks/playlist.rb', line 9

def initialize(name)
  @name = name
  @movies = []
end

Instance Attribute Details

#moviesObject (readonly)

Returns the value of attribute movies.



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

def movies
  @movies
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add_movie(movie) ⇒ Object



32
33
34
# File 'lib/flicks/playlist.rb', line 32

def add_movie(movie)
  @movies << movie
end

#load(from_file) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/flicks/playlist.rb', line 14

def load(from_file)
  File.readlines(from_file, chomp: true).each do |line|
    movie = Movie.from_csv(line)
    add_movie(movie)
  end
rescue Errno::ENOENT
  puts "Whoops, #{from_file} not found!"
  exit 1
end

#play(viewings = 3) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/flicks/playlist.rb', line 59

def play(viewings = 3)
  puts "*" * 30
  puts "#{@name}'s playlist:"

  puts "\nThe snackbar has:"
  menu_items = Snackbar.menu_items
  puts menu_items

  puts "\nBefore watching:"
  puts @movies

  1.upto(viewings) do |viewing_number|
    puts "\nViewing #{viewing_number}:"

    @movies.each do |movie|
      number_rolled = roll_die

      case number_rolled
      when 1..2
        movie.thumbs_down
        puts "#{movie.title} got a 👎"
      when 3..4
        puts "#{movie.title} got skipped"
      else
        movie.thumbs_up
        puts "#{movie.title} got a 👍"
      end

      snack = Snackbar.random_snack
      movie.add_snack(snack.name, snack.price)
      puts "During #{movie.title}, #{@name} ate #{snack.name} for $#{snack.price}."
    end
  end

  puts "\nAfter watching:"
  puts @movies
end


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/flicks/playlist.rb', line 44

def print_stats
  puts "\n#{@name}'s Playlist Stats:"
  puts "-" * 30

  puts sorted_movies

  @movies.each do |movie|
    puts "\n#{movie.title} snack totals:"
    movie.snacks_eaten.each do |name, total_price|
      puts "#{name}: $#{total_price}"
    end
    puts "total: $#{movie.total_snack_price}"
  end
end

#roll_dieObject



36
37
38
# File 'lib/flicks/playlist.rb', line 36

def roll_die
  rand(1..6)
end

#save(to_file = "movie_rankings.csv") ⇒ Object



24
25
26
27
28
29
30
# File 'lib/flicks/playlist.rb', line 24

def save(to_file = "movie_rankings.csv")
  File.open(to_file, "w") do |file|
    sorted_movies.each do |movie|
      file.puts movie.to_csv
    end
  end
end

#sorted_moviesObject



40
41
42
# File 'lib/flicks/playlist.rb', line 40

def sorted_movies
  @movies.sort_by { |movie| movie.rank }.reverse
end