1
0
mirror of https://github.com/danbee/mpd-client synced 2025-03-04 08:39:09 +00:00
mpd-client/lib/mpd_client/song.rb
2013-12-12 18:19:40 +00:00

62 lines
1.1 KiB
Ruby

module MPDClient
class Song
include ClassToProc
include Comparable
include Jsonable
extend Forwardable
delegate %i(id track artist album title genre date time pos) => :@song
def initialize(song, pos: nil)
@song = song
end
def playing?
if current = self.class.current_song
[artist, album, title] == [current.artist, current.album, current.title]
end
end
def length
time
end
def <=>(other)
[artist, album, track] <=> [other.artist, other.album, other.track]
end
def to_h
{
id: id,
track: track,
artist: artist,
album: album,
title: title,
length: length,
pos: pos,
playing: playing?
}
end
class << self
def by(**params)
params.delete_if {|_, v| v.nil? }
MPDClient.conn.where(params).map(&self)
end
def all
MPDClient.conn.songs.map(&self)
end
def current_song
if song = MPDClient.conn.current_song
new(song)
end
end
end
end
end