ruby-mpd
ruby-mpd copied to clipboard
Command Lists (Automatic Result Types)
Implements command lists in a manner that (a) allows various results to be interleaved and still return the correct types, and (b) allows playlist objects to be used directly in a command list.
# Do not parse response (for speed)
@mpd.command_list do
add song
volume 50
shuffle
end
# Array of values from each response line
ids = @mpd.command_list(results:true) do
addid song1
addid song2
end
#=> [113,114]
# Arrays of Songs
songs = @mpd.command_list(results:true) do
where genre:'electronic'
where genre:'trance'
end
#=> [
#=> [ <MPD::Song>, <MPD::Song>, … ],
#=> [ <MPD::Song>, <MPD::Song>, … ]
#=> ]
# Array of Playlists
results = @mpd.command_list(results:true) do
clear
where({genre:'electronic'},{add:true})
save 'new-playlist'
playlists
end
#=> [
#=> [ <MPD::Playlist>, <MPD::Playlist>, … ]
#=> ]
# Using Playlist inside Command List
def shuffle_playlist( playlist )
song_count = playlist.songs.length
@mpd.command_list do
(song_count-1).downto(1){ |i| playlist.move i, rand(i+1) }
end
end
This has a small class used for the DSL, but that is not strictly necessary. Though it doesn't look as nice, the following code works:
@mpd.command_list do
@mpd.clear
@mpd.addid my_song
@mpd.songs
end
…because starting a command_list
causes all other commands to ignore the response, but to record the commands used for later parsing of the command_list results.
Sorry for the long wait, it's quite a big PR. I'm a bit anxious to just merge it, but the tests you've added seem sound. If you address the minor comments I'll merge this :)
I know its been a whole Ruby version but what needs to be done to get this merged?