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 #1472

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
4 changes: 3 additions & 1 deletion config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
module Concerns
end

require_all 'lib'
require_relative '../lib/concerns/findable.rb'

require_all 'lib'
45 changes: 45 additions & 0 deletions lib/artist.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Artist
attr_accessor :name, :songs, :genre
extend Concerns::Findable

@@all = []

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

def self.all
@@all
end

def self.destroy_all
@@all.clear
end

def save
@@all << self
end

def self.create(name)
self.new(name).tap do |song|
song.save
end
end

#add_song
# assigns the current artist to the song's 'artist' property (song belongs to artist)
# does not assign the artist if the song already has an artist
# adds the song to the current artist's 'songs' collection
# does not add the song to the current artist's collection of songs if it already exists therein
def add_song(song)
song.artist = self unless song.artist == self
@songs << song unless @songs.include?(song)
end

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

@artists
end
16 changes: 16 additions & 0 deletions lib/concerns/findable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Concerns::Findable
def find_by_name(name)
all.detect {|instance| instance.name == name }
end

def find_or_create_by_name(name)
if
self.find_by_name(name)
return self.find_by_name(name)
else
self.create(name)
end

end

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

@@all = []

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

def self.all
@@all
end

def self.destroy_all
@@all.clear
end

def save
@@all << self
end

def self.create(name)
self.new(name).tap do |song|
song.save
end
end

def artists
songs.map { | song| song.artist }.uniq
end
end
18 changes: 18 additions & 0 deletions lib/music_importer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'pry'

class MusicImporter
attr_accessor :path, :files

def initialize(path)
@path = path
end

def files
Dir.glob("#{path}/*").map { |file| file.gsub("#{path}/", '') }
end

def import
# binding.pry
files.each { |file| Song.create_from_filename(file) }
end
end
91 changes: 91 additions & 0 deletions lib/music_library_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require 'pry'
class MusicLibraryController

def initialize(path = './db/mp3s')
MusicImporter.new(path).import
end

def call
input = ''
while input != 'exit'
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?"

input = gets.strip

case input
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
Song.all.sort_by(&:name).each.with_index(1) do |song, index|
puts "#{index}. #{song.artist.name} - #{song.name} - #{song.genre.name}"
end
end

def list_artists
Artist.all.sort_by(&:name).each.with_index(1) do |artist, index|
puts "#{index}. #{artist.name}"
end
end

def list_genres
Genre.all.sort_by(&:name).each.with_index(1) do |genre, index|
puts "#{index}. #{genre.name}"
end
end

def list_songs_by_artist
puts "Please enter the name of an artist:"


input = gets.strip

if artist = Artist.find_by_name(input)
artist.songs.sort_by(&:name).each.with_index(1) do |song, index|
puts "#{index}. #{song.name} - #{song.genre.name}"
end
end
end

def list_songs_by_genre
puts "Please enter the name of a genre:"
input = gets.strip

if genre = Genre.find_by_name(input)
genre.songs.sort_by(&:name).each.with_index(1) do |song, index|
puts "#{index}. #{song.artist.name} - #{song.name}"
end
end
end

def play_song
puts "Which song number would you like to play?"
input = gets.strip.to_i
x = Song.all.length
if (1..x).include?(input)
song = Song.all.sort_by(&:name)[input-1]
puts "Playing #{song.name} by #{song.artist.name}"
end
end
end
76 changes: 76 additions & 0 deletions lib/song.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require 'pry'

class Song
attr_accessor :name, :artist, :genre

@@all = []

def initialize(name, artist=nil, genre=nil) # can be invoked with optional argument, object to be assigned to property
@name = name
self.artist = artist if artist
self.genre = genre if genre
end

def self.all
@@all
end

def self.destroy_all
@@all.clear
end

def save
@@all << self
end

def self.create(name)
self.new(name).tap do |song|
song.save
end
end

# invokes Artist#add_song to add itself to the artist's collection of songs(artist has many songs)
def artist=(artist)
if @artist == nil
@artist = artist
else
@artist = @artist
end
if self.artist != nil
@artist.add_song(self)
end
@artist
end

def genre=(genre)
@genre = genre
genre.songs << self unless genre.songs.include?(self)
end

def self.find_by_name(name)
@@all.find {|song| name == song.name }
end

def self.find_or_create_by_name(name)
if
self.find_by_name(name)
return self.find_by_name(name)
else
self.create(name)
end
end

def self.new_from_filename(name)
artist, song, genre_name = name.split(' - ')
fixed_name = genre_name.gsub('.mp3', '')
artist = Artist.find_or_create_by_name(artist)
genre = Genre.find_or_create_by_name(fixed_name)
new(song, artist, genre)
end

def self.create_from_filename(name)
# binding.pry
new_from_filename(name).save
end

end