class CDOrganizer
Attributes
cd_list[RW]
table_header_stuff[RW]
Public Class Methods
new()
click to toggle source
# File lib/cd_organizer.rb, line 3 def initialize Struct.new("CD", :artist, :title, :year, :genre) @cd_list = [] @table_header_stuff = 'Artist Title Genre Year' end
Public Instance Methods
add(artist, title, year, genre)
click to toggle source
# File lib/cd_organizer.rb, line 8 def add(artist, title, year, genre) @cd_list << Struct::CD.new(artist.upcase, title.upcase, year, genre) @cd_list.sort! { |cd, other| [cd.artist, other.year.to_i] <=> [other.artist, cd.year.to_i] } end
build_cd_output(cd)
click to toggle source
# File lib/cd_organizer.rb, line 51 def build_cd_output(cd) print_space = 17 s = '' s << cd.artist (print_space-cd.artist.length).times { s << ' ' } s << cd.title (print_space-cd.title.length).times { s << ' ' } s << cd.genre.to_s (print_space-cd.genre.to_s.length).times { s << ' ' } s << cd.year.to_s end
delete(artist, title)
click to toggle source
# File lib/cd_organizer.rb, line 12 def delete(artist, title) @cd_list.delete_if { |cd| cd.artist == artist.upcase && cd.title == title.upcase } end
save(filename)
click to toggle source
# File lib/cd_organizer.rb, line 37 def save(filename) file = File.open("../cds/#{filename}",'w') @cd_list.each { |cd| file.write("#{cd.artist}_#{cd.title}_#{cd.genre}_#{cd.year}\n") } system('clear') file.close end
search(input, flag)
click to toggle source
# File lib/cd_organizer.rb, line 15 def search(input, flag) puts puts @table_header_stuff count = 0 @cd_list.each do |cd| case flag when 'artist' if cd.artist =~ /#{input}/i puts build_cd_output(cd) count += 1 end when 'genre' if cd.genre.to_i == input.to_i puts build_cd_output(cd) count += 1 end end end puts puts "#{count} record(s) found." puts end
to_s()
click to toggle source
# File lib/cd_organizer.rb, line 43 def to_s puts puts @table_header_stuff @cd_list.each do |cd| puts build_cd_output(cd) end puts end