Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done #1485

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Done #1485

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ GEM
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
require_all (2.0.0)
require_all (1.5.0)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
Expand All @@ -27,7 +27,7 @@ PLATFORMS

DEPENDENCIES
pry
require_all
require_all (~> 1.0)
rspec

BUNDLED WITH
Expand Down
46 changes: 46 additions & 0 deletions lib/artist.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'pry'

class Artist
extend Concerns::Findable

attr_accessor :name

@@all = []

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

def add_song(song)
song.artist = self if song.artist.nil?
@songs << song unless @songs.include?(song)
end

def songs
@songs
end

def genres
self.songs.collect {|song| song.genre}.uniq
end

def save
@@all << self
end

def self.create(name)
artist = self.new(name)
artist.save
artist
end

def self.all
@@all
end

def self.destroy_all
@@all = []
end

end
19 changes: 19 additions & 0 deletions lib/concerns/findable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Concerns::Findable

def find_by_name(name)
item = self.all.select {|item| item.name == name}
if item.length() > 0
item[0]
end
end

def find_or_create_by_name(name)
item = self.find_by_name(name)
if !item
self.create(name)
else
item
end
end

end
44 changes: 44 additions & 0 deletions lib/genre.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Genre
extend Concerns::Findable

attr_accessor :name

@@all = []

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

def add_song(song)
song.genre = self if song.genre.nil?
@songs << song unless @songs.include?(song)
end

def songs
@songs
end

def artists
self.songs.collect {|song| song.artist}.uniq
end

def save
@@all << self
end

def self.create(name)
genre = self.new(name)
genre.save
genre
end

def self.all
@@all
end

def self.destroy_all
@@all = []
end

end
24 changes: 24 additions & 0 deletions lib/musicimporter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'pry'

class MusicImporter

attr_accessor :path, :files

def initialize(path)
@path = path
@files = []
end

def files
files = Dir[@path + "/**/*.mp3"]
files.map! {|file| file.gsub(@path + "/","")}
files.each {|file| @files << file unless @files.include?(file)}
@files
end

def import
files
@files.each {|file| Song.create_from_filename(file)}
end

end
112 changes: 112 additions & 0 deletions lib/musiclibrarycontroller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
class MusicLibraryController

attr_accessor :path

def initialize(path="./db/mp3s")
@path = path
musicimporter = MusicImporter.new(@path)
musicimporter.import
end

def call
puts("Welcome to your music library!")
puts("To list all of your songs, enter 'list songs'.")
puts("To list all of the artists in your library, enter 'list artists'.")
puts("To list all of the genres in your library, enter 'list genres'.")
puts("To list all of the songs by a particular artist, enter 'list artist'.")
puts("To list all of the songs of a particular genre, enter 'list genre'.")
puts("To play a song, enter 'play song'.")
puts("To quit, type 'exit'.")
puts("What would you like to do?")

command = ""
until command == "exit"
command = gets.chomp
case command
when "list songs"
list_songs
when "list artists"
list_artists
when "list genres"
list_genres
when 'list artist'
list_songs_by_artist
when 'list genre'
list_songs_by_genre
when 'play song'
play_song
end
end

end

def list_songs(no_msg=false)
song_list = Song.all.sort_by {|song| song.name}
song_list.each do |song|
#"1. Thundercat - For Love I Come - dance"
puts "#{song_list.index(song)+1}. #{song.artist.name} - #{song.name} - #{song.genre.name}" if no_msg == false
end
song_list
end

def list_artists
artist_list = Artist.all.sort_by {|artist| artist.name}
artist_list.each do |artist|
# "1. Action Bronson"
puts "#{artist_list.index(artist)+1}. #{artist.name}"
end
end

def list_genres
genre_list = Genre.all.sort_by {|genre| genre.name}
genre_list.each do |genre|
# "1. country"
puts "#{genre_list.index(genre)+1}. #{genre.name}"
end
end

def list_songs_by_artist
puts "Please enter the name of an artist:"
artist_name = gets.chomp
artist_selected = Artist.all.find {|artist| artist.name == artist_name}

if artist_selected
song_list = artist_selected.songs.sort_by {|song| song.name}
song_list.each do |song|
# "1. Green Aisles - country"
puts "#{song_list.index(song)+1}. #{song.name} - #{song.genre.name}"
end
end
end

def list_songs_by_genre
puts "Please enter the name of a genre:"
genre_name = gets.chomp
genre_selected = Genre.all.find {|genre| genre.name == genre_name}

if genre_selected
song_list = genre_selected.songs.sort_by {|song| song.name}
song_list.each do |song|
# "1. Real Estate - It's Real"
puts "#{song_list.index(song)+1}. #{song.artist.name} - #{song.name}"
end
end
end

def play_song
puts "Which song number would you like to play?"
song_number = gets.chomp

if song_number.to_i > 0
song_number = song_number.to_i - 1
song_list = list_songs(true)
song_selected = song_list[song_number]

if song_selected
# "Playing Larry Csonka by Action Bronson"
puts "Playing #{song_selected.name} by #{song_selected.artist.name}"
end
end
end

end
78 changes: 78 additions & 0 deletions lib/song.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'pry'

class Song

attr_accessor :name, :artist, :genre

@@all = []

def initialize(name,artist=nil,genre=nil)
@name = name
# @artist = artist
self.artist=(artist) if !artist.nil?
# @genre = genre
self.genre=(genre) if !genre.nil?
end

def save
@@all << self
end

def artist=(artist)
@artist = artist
artist.add_song(self)
end

def genre=(genre)
@genre = genre
genre.add_song(self)
end

def self.create(name)
song = self.new(name)
song.save
song
end

def self.find_by_name(name)
song = self.all.select {|song| song.name == name}
if song.length() > 0
song[0]
end
end

def self.find_or_create_by_name(name)
song = self.find_by_name(name)
if !song
self.create(name)
else
song
end
end

def self.new_from_filename(name)
song_parts = name.split(' - ')

song = self.create(song_parts[1])
song.artist=(Artist.find_or_create_by_name(song_parts[0]))
song.genre=(Genre.find_or_create_by_name(song_parts[2].gsub(".mp3","")))

song
end

def self.create_from_filename(name)
if !self.find_by_name(name.split(' - ')[1])
song = self.new_from_filename(name)
song
end
end

def self.all
@@all
end

def self.destroy_all
@@all = []
end

end