Created
June 23, 2013 21:54
-
-
Save surrealdetective/5846690 to your computer and use it in GitHub Desktop.
Playlister CLI - for those downcase songs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Table of Contents: | |
#1. main.rb | |
#2. parser.rb | |
#3. artist.rb | |
#4. song.rb | |
#5. genre.rb | |
#shoutout to George Lin, whose code I leaned on heavily to learn re: parser.rb | |
#------------------------# 1. main.rb #------------------------# | |
require_relative 'lib/artist' | |
require_relative 'lib/genre' | |
require_relative 'lib/song' | |
require_relative 'lib/parser' | |
require 'debugger' | |
class Jukebox | |
attr_accessor :songs, :artists, :genres, :playlister | |
def initialize(playlist) | |
self.populate_songs(playlist) | |
@playlister = playlist | |
self.welcome | |
self.instruction | |
end | |
def populate_songs(playlist) | |
@songs ||= [] | |
playlist.each do |song_info| | |
@songs << song_info[:song] | |
end | |
end | |
def welcome | |
puts "Welcome to jukebox." | |
end | |
def instruction | |
puts "What would you like to do? Please enter list, play, help, or exit" | |
instruction = gets.downcase.chomp | |
if instruction == "exit" | |
self.exit | |
elsif instruction == "play" | |
self.play | |
elsif instruction == "list" | |
self.list | |
elsif instruction == "help" | |
self.help | |
end | |
end | |
def list(song_selection = self.songs) | |
song_selection.each do |song| | |
puts "#{song.name} by #{song.artist.name} in #{song.genre.name}" | |
end | |
instruction | |
end | |
def play | |
puts "Which song would you like to play?" | |
request=gets.chomp.strip.downcase | |
searching = true | |
while searching | |
playlister.each do |song_info| | |
song = song_info[:song].name.downcase | |
artist = song_info[:artist].name.downcase | |
genre = song_info[:genre].name.downcase | |
if song == request || artist == request || genre == request | |
puts "*Listening to #{song_info[:song].name}, by #{song_info[:artist].name} in #{song_info[:genre].name}*" | |
instruction | |
end | |
end | |
searching =false | |
end | |
puts "Sorry, I didn't understand that. Please try again." | |
instruction | |
end | |
def help | |
puts "[Help]" | |
puts "You have the following options:" | |
puts "[play] to play your song." | |
puts "[list] to list all songs on the jukebox." | |
puts "[exit] to exit the jukebox." | |
self.instruction | |
end | |
def exit | |
puts "Goodbye :)" | |
end | |
end | |
parser = Parser.new | |
playlist = parser.create_playlist('./data') | |
Jukebox.new(playlist) | |
#------------------------# 2. parser.rb #------------------------# | |
require_relative 'artist' | |
require_relative 'genre' | |
require_relative 'song' | |
require 'debugger' | |
class Parser | |
def add_or_select_class_object(klass , name, list) | |
if list.include?(name) | |
klass.all.select{ |item| item.name == name}.first | |
else | |
list << name | |
klass.new(name) | |
end | |
end | |
def create_playlist(data_relative_path) | |
artist_list = [] | |
song_list = [] | |
genre_list = [] | |
entire_playlist = [] | |
files = Dir["#{data_relative_path}/*.mp3"] | |
files.each do |song_data| | |
#parse files for song, artist, and genre names | |
loading_artist = song_data.split(' - ').first.split('/').last | |
loading_song = song_data.split(" - ").last.split(" [").first | |
loading_genre = song_data.split(" - ").last.split(" [").last.split("]").first | |
#create instances of the class using those names | |
artist = add_or_select_class_object(Artist, loading_artist, artist_list) | |
song = add_or_select_class_object(Song, loading_song, song_list) | |
genre = add_or_select_class_object(Genre, loading_genre, genre_list) | |
#create directory of songs with data stored as hashes in array | |
entire_song = {} | |
entire_song[:artist] = artist | |
entire_song[:song] = song | |
entire_song[:genre] = genre | |
entire_playlist << entire_song | |
#use instance methods to populate info of class objects | |
song.genre = genre | |
artist.add_song(song) | |
song_list << loading_song unless song_list.include?(loading_song) | |
artist_list << loading_artist unless artist_list.include?(loading_artist) | |
genre_list << loading_genre unless genre_list.include?(loading_genre) | |
end | |
# debugger | |
#return all artists, songs, and genres as array of arrays | |
entire_playlist#, [artist_list, song_list, genre_list] | |
end | |
end | |
# #TESTS | |
parser = Parser.new() | |
playlist = parser.create_playlist('../data') | |
puts playlist.inspect | |
# #check that Artist, Song, and Genre classes have objects | |
# [Artist, Song, Genre].each do |klass| | |
# puts "\n\n" | |
# puts "There are #{klass.all.size} #{klass}s:" | |
# puts | |
# puts klass.all | |
# end | |
# #check that artists have songs | |
# Artist.all.each do |artist| | |
# puts "The artist #{artist.name} has the following songs:" | |
# puts artist.songs.each {|song| puts "#{song.name}"} | |
# puts | |
# end | |
# #check that artists have genres | |
# Artist.all.each do |artist| | |
# puts "#{artist} has these genres" | |
# puts "#{artist} - *#{artist.genres.each {|genre| genre.name}}*" | |
# end | |
# #check that genres have artists | |
# Genre.all.each do |genre| | |
# puts | |
# puts "#{genre} has these artists" | |
# puts "#{genre} - #{genre.artists}" | |
# end | |
# #check that genres have songs. | |
# Genre.all.each do |genre| | |
# puts | |
# puts "#{genre.name} has the following songs:" | |
# genre.songs.each {|song| print "#{song.name}, "} | |
# end | |
#------------------------# 3. artist.rb #------------------------# | |
class Artist | |
attr_accessor :name, :songs | |
ArtistList = [] | |
def initialize(name) | |
@songs = [] | |
ArtistList << self unless ArtistList.include?(self) | |
@name = name | |
end | |
def self.reset_artists | |
ArtistList.clear | |
end | |
def self.count | |
return ArtistList.length | |
end | |
def self.all | |
ArtistList | |
end | |
def songs_count | |
@songs.length | |
end | |
def add_song(song) | |
#add the individual song to the reciever of the method | |
@songs << song | |
song.artist = self | |
song.genre.artists << self | |
end | |
def genres | |
genres =[] | |
@songs.each do |song| | |
genres << song.genre | |
end | |
genres | |
end | |
end | |
#------------------------# 4. song.rb #------------------------# | |
require 'debugger' | |
class Song | |
attr_accessor :genre, :name, :artist | |
SongList = [] | |
def initialize(name) | |
SongList << self unless SongList.include?(self) | |
@name = name | |
end | |
#add a genre to songs AND add songs to genre. | |
def genre=(newgenre) | |
@genre = newgenre | |
newgenre.songs << self | |
end | |
def self.all | |
SongList | |
end | |
end | |
#------------------------# 5. genre.rb #------------------------# | |
class Genre | |
attr_accessor :name, :songs | |
GenreList = [] | |
def initialize(name) | |
@songs = [] | |
@artists = [] | |
@name = name | |
GenreList << self unless GenreList.include?(self) | |
end | |
def artists | |
@artists | |
end | |
def self.all | |
GenreList | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment