Class: Songfile::Playlist

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Playlist

this method converts a Playlist.new specified title to the correct format when created



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

def initialize(name)       #this method converts a Playlist.new specified title to the correct format when created
    @name = name.upcase    #upcased all playlist titles for now
    @list = []
    puts "Playlist '#{@name}' was created."
end

Instance Attribute Details

#listObject (readonly)

you can read the title (name) of the playlist



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

def list
  @list
end

#nameObject (readonly)

you can read the title (name) of the playlist



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

def name
  @name
end

Instance Method Details

#add_song(song) ⇒ Object

when you add a song, the list re-sorts itself



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

def add_song(song) #when you add a song, the list re-sorts itself
    @list << song 
    @list = @list.sort { |a, b| a.rank <=> b.rank } #this line is critical to have so that original song order by rank is displayed by the text output!
    @original_list = @list # whenever a song is added, the list is stored
    # NOTE: ^^ I disabled the list sorting itself whenever a new song is added. All the responsibility is
    # now handed over to the sort songs method which 1) sorts all songs in array by rank then 
    # normalizes the rank values based on the amount of songs in the playlist!
end

#load_songs(from_file) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/songfile/playlist.rb', line 116

def load_songs(from_file)
    CSV.foreach(from_file, 'r:bom|utf-8') do |row|
        if row[1].to_i == 0
            song = Song.new(row[0], 10000) # THIS is the code that sets nil to 10000 by default so that sorted song is ranked last in playlist
        else
            song = Song.new(row[0], row[1].to_i)# << this "index" stuff syntax allows for nil ranks to "pass/fly" (and get reassigned to default of 0) THIS is where default gets assigned.

        end
    add_song(song) 
    end
end

#multiply_ranksObject



48
49
50
51
52
# File 'lib/songfile/playlist.rb', line 48

def multiply_ranks
    @list.each do |song| #step 2 (after ALL songs are sorted by rank)
        song.rank *= 10 # multiplies each song's rank by 10. This method is used after "normalize ranks" in the play method 
    end 
end

#normalize_ranksObject



41
42
43
44
45
46
47
# File 'lib/songfile/playlist.rb', line 41

def normalize_ranks
    @list.each do |song| #step 2 (after ALL songs are sorted by rank)
        song.rank = @list.index(song) + 1 # NOW you normalize rank once all songs are sorted
        #song.rank = @list.index(song) + 1 # NOW you normalize rank once all songs are sorted
        # puts "#{song.rank}) #{song.title}"
    end 
end

#play(rounds = 1) ⇒ Object

play one round by default



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/songfile/playlist.rb', line 55

def play(rounds=1) #play one round by default
    puts "\nThere are #{@list.size} songs in this playlist:"
    sort_songs # use of SELF to call sort_songs on the applicable Playlist object from inside the play method
    normalize_ranks # is now separate from sorting songs
    @list.each do |song|
        puts "#{song.rank}) #{song.title}"
    end 

    multiply_ranks

    show_reviewers # this method is standalone, no reference to self is needed

    1.upto(rounds) do |round|
        puts "\nRound #{round}:"
    # puts "number rolled is #{number_rolled}"
        @list.each do |song|
            puts song
            PlaylistTurn.take_turn(song)
            puts song
        end
    end

    #self.print_stats #activating this code will make two tests fail because sort/normalization is 
                    #performed within the print stats method which, when enabled INSIDE the "play" method
                    #makes the math check test fail! (activating this code sorts/normalizes the new ranks
                    #before the math checker is run, aka after the "play" method is completed)
end


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/songfile/playlist.rb', line 83

def print_stats
    sort_songs # FIRST SORT FOR PREVIEW/DISPLAY
    puts "\n#{@name} Final Rankings:"
    @list.each do |song|
        puts "#{song.title} (#{song.rank})"
    end

    puts "\nOriginal list order:"
            @original_list.each do |song|
            puts "#{@original_list.index(song) + 1}) #{song.title}"
        end 

    normalize_ranks # NOW NORMALIZE RANKINGS

    top_ten_songs, average_songs = @list.partition { |song| song.top_ten? }
puts "\nNew list order:"
if @list.size > 10
    puts "Top 10 Songs:" 
    top_ten_songs.each do |song|
    puts "#{song.rank}) #{song.title}"
    end
puts "\nOther Songs:" 
    average_songs.each do |song|
    puts "#{song.rank}) #{song.title}"
    end
else
    puts "\nTop #{@list.size} Songs:" 
    @list.each do |song|
    puts "#{song.rank}) #{song.title}"
    end
end
end

#save_output(to_file = "songfile_output.txt") ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/songfile/playlist.rb', line 127

def save_output(to_file="songfile_output.txt")
    File.open(to_file, "w") do |file|
        file.puts Time.new.strftime("File updated on %m/%d/%Y at %I:%M %p")
        file.puts "\nOriginal list order:"
            @original_list.each do |song|
            file.puts "#{@original_list.index(song) + 1}) #{song.title}"
        end 
    file.puts "\nNew list order:"

    top_ten_songs, average_songs = @list.partition { |song| song.top_ten? }

        if @list.size > 10
            file.puts "Top 10 Songs:" # literally copied from print stats but with "file.puts" not "puts"
            top_ten_songs.each do |song|
            file.puts "#{song.rank}) #{song.title}"
        end
        file.puts "\nOther Songs:" 
            average_songs.each do |song|
            file.puts "#{song.rank}) #{song.title}"
            end
        else
            file.puts "\nTop #{@list.size} Songs:" 
            @list.each do |song|
            file.puts "#{song.rank}) #{song.title}"
            end
        end
        
    end     

end

#show_reviewersObject



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

def show_reviewers
    reviewers = Council::REVIEWERS
    puts "\nThere are #{reviewers.size} playlist reviewers:"
    reviewers.each do |r|
    puts "#{r.name}, #{r.influence}"
end
end

#show_titlesObject



15
16
17
# File 'lib/songfile/playlist.rb', line 15

def show_titles
    p self.list.map(&:title) # p is used within the method to print the array properly, and it is only an array of titles
end

#sort_songsObject

created this method… it looks very similar to the print_stats method



36
37
38
39
40
# File 'lib/songfile/playlist.rb', line 36

def sort_songs #created this method... it looks very similar to the print_stats method
    @list.each do |song| #sort by rank step 1
        @list = @list.sort { |a, b| a.rank <=> b.rank }
        end
end