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

Command Lists (Automatic Result Types) #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Phrogz
Copy link
Contributor

@Phrogz Phrogz commented Mar 8, 2016

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.

hash[key] = Array(hash[key]) << object
# cannot use Array(hash[key]) or [*hash[key]] because Time instances get splatted
# cannot check for is_a?(Array) because some values (time) are already arrays
unless array_keys[key]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try using Array.wrap? I think that one should safely work with Time objects as well, since it uses to_ary instead of to_a. http://apidock.com/rails/Array/wrap/class

@archseer
Copy link
Owner

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 :)

@LevitatingBusinessMan
Copy link

LevitatingBusinessMan commented Jan 20, 2022

I know its been a whole Ruby version but what needs to be done to get this merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants