class Drum::Playlist

A list of tracks with metadata.

@!attribute id

@return [String] An internal id for the playlist

@!attribute name

@return [String] The name of the playlist

@!attribute description

@return [optional, String] A description of the playlist

@!attribute author_id

@return [optional, String] The author id

@!attribute path

@return [optional, Array<String>] The path of parent 'folders' to this playlist.

@!attribute users

@return [optional, Hash<String, User>] A hash of ids to users used somewhere in the playlist

@!attribute artists

@return [optional, Hash<String, Artist>] A hash of ids to artists used somewhere in the playlist

@!attribute albums

@return [optional, Hash<String, Album>] A hash of ids to albums used somewhere in the playlist

@!attribute tracks

@return [optional, Array<Track>] The list of tracks of the playlist, order matters here

@!attribute spotify

@return [optional, PlaylistSpotify] Spotify-specific metadata

@!attribute applemusic

@return [optional, PlaylistAppleMusic] Apple Music-specific metadata

Public Class Methods

deserialize(h) click to toggle source

Parses a playlist from a nested Hash that uses string keys.

@param [Hash<String, Object>] h The Hash to be parsed @return [Playlist] The parsed playlist

# File lib/drum/model/playlist.rb, line 98
def self.deserialize(h)
  Playlist.new(
    id: h['id'],
    name: h['name'],
    description: h['description'],
    author_id: h['author_id'],
    path: h['path'],
    users: h['users']&.map { |u| User.deserialize(u) }&.to_h_by_id,
    artists: h['artists']&.map { |a| Artist.deserialize(a) }&.to_h_by_id,
    albums: h['albums']&.map { |a| Album.deserialize(a) }&.to_h_by_id,
    tracks: h['tracks']&.map { |t| Track.deserialize(t) },
    spotify: h['spotify'].try { |s| PlaylistSpotify.deserialize(s) },
    applemusic: h['applemusic'].try { |a| PlaylistAppleMusic.deserialize(a) }
  )
end
new(*) click to toggle source
Calls superclass method
# File lib/drum/model/playlist.rb, line 41
def initialize(*)
  super
  self.description ||= ''
  self.path ||= []
  self.users ||= {}
  self.artists ||= {}
  self.albums ||= {}
  self.tracks ||= []
end

Public Instance Methods

serialize() click to toggle source

Serializes the playlist to a nested Hash that uses string keys.

@return [Hash<String, Object>] The serialized representation

# File lib/drum/model/playlist.rb, line 117
def serialize
  {
    'id' => self.id,
    'name' => self.name,
    'description' => (self.description unless self.description.empty?),
    'author_id' => self.author_id,
    'path' => (self.path unless self.path.empty?),
    'users' => (self.users.each_value.map { |u| u.serialize } unless self.users.empty?),
    'artists' => (self.artists.each_value.map { |a| a.serialize } unless self.artists.empty?),
    'albums' => (self.albums.each_value.map { |a| a.serialize } unless self.albums.empty?),
    'tracks' => (self.tracks.map { |t| t.serialize } unless self.tracks.empty?),
    'spotify' => self.spotify&.serialize,
    'applemusic' => self.applemusic&.serialize
  }.compact
end
store_album(album) click to toggle source

Stores an album if it does not exist already.

@param [Album] album The album to store.

# File lib/drum/model/playlist.rb, line 74
def store_album(album)
  unless self.albums.key?(album.id)
    self.albums[album.id] = album
  end
end
store_artist(artist) click to toggle source

Stores an artist if it does not exist already.

@param [Artist] artist The artist to store.

# File lib/drum/model/playlist.rb, line 65
def store_artist(artist)
  unless self.artists.key?(artist.id)
    self.artists[artist.id] = artist
  end
end
store_track(track) click to toggle source

Stores a track.

@param [Track] track The track to store.

# File lib/drum/model/playlist.rb, line 83
def store_track(track)
  self.tracks << track
end
store_user(user) click to toggle source

Stores a user if it does not exist already.

@param [User] user The user to store.

# File lib/drum/model/playlist.rb, line 56
def store_user(user)
  unless self.users.key?(user.id)
    self.users[user.id] = user
  end
end
track_search_phrase(track) click to toggle source

Describes a track in a way useful for song matching by search.

@return [String] A short description of track name and artists

# File lib/drum/model/playlist.rb, line 90
def track_search_phrase(track)
  "#{track.name} #{track.artist_ids.filter_map { |i| self.artists[i]&.name }.join(' ')}"
end