Class: AppleMusicLibrary::Artist

Inherits:
Object
  • Object
show all
Defined in:
lib/apple_music_library/artist.rb

Constant Summary collapse

@@artists =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Artist

Returns a new instance of Artist.



8
9
10
11
12
# File 'lib/apple_music_library/artist.rb', line 8

def initialize(name)
  @name = name&.strip
  @albums = []
  @tracks = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/apple_music_library/artist.rb', line 4

def name
  @name
end

Class Method Details

.allObject



14
15
16
# File 'lib/apple_music_library/artist.rb', line 14

def self.all
  @@artists.values
end

.find_by_name(artist_name) ⇒ Object



37
38
39
40
41
42
# File 'lib/apple_music_library/artist.rb', line 37

def self.find_by_name(artist_name)
  if @@artists[artist_name]
    return @@artists[artist_name]
  end
  nil
end

.find_or_create(name) ⇒ Object



30
31
32
33
34
35
# File 'lib/apple_music_library/artist.rb', line 30

def self.find_or_create(name)
  if @@artists[name]
    return @@artists[name]
  end
  @@artists[name] = self.new(name)
end

.most_albums(limit = 20) ⇒ Object



26
27
28
# File 'lib/apple_music_library/artist.rb', line 26

def self.most_albums(limit = 20)
  self.sorted_set(:album_count, limit)
end

.most_played(limit = 20) ⇒ Object



18
19
20
# File 'lib/apple_music_library/artist.rb', line 18

def self.most_played(limit = 20)
  self.sorted_set(:play_count, limit)
end

.most_tracks(limit = 20) ⇒ Object



22
23
24
# File 'lib/apple_music_library/artist.rb', line 22

def self.most_tracks(limit = 20)
  self.sorted_set(:track_count, limit)
end

Instance Method Details

#add_album(album) ⇒ Object



44
45
46
47
48
# File 'lib/apple_music_library/artist.rb', line 44

def add_album(album)
  unless @albums.include?(album)
    @albums << album
  end
end

#add_track(track) ⇒ Object



50
51
52
53
54
# File 'lib/apple_music_library/artist.rb', line 50

def add_track(track)
  unless @tracks.include?(track)
    @tracks << track
  end
end

#album_countObject



60
61
62
# File 'lib/apple_music_library/artist.rb', line 60

def album_count
  @albums.size
end

#albumsObject



56
57
58
# File 'lib/apple_music_library/artist.rb', line 56

def albums
  @albums.sort_by(&:year)
end

#genresObject



76
77
78
# File 'lib/apple_music_library/artist.rb', line 76

def genres
  @genres ||= tracks.map(&:genre).uniq
end

#play_countObject



72
73
74
# File 'lib/apple_music_library/artist.rb', line 72

def play_count
  tracks.map(&:play_count).sum
end

#track_countObject



68
69
70
# File 'lib/apple_music_library/artist.rb', line 68

def track_count
  @tracks.size
end

#tracksObject



64
65
66
# File 'lib/apple_music_library/artist.rb', line 64

def tracks
  @tracks
end