module MPD::Plugins::Database

Commands for interacting with the music database.

Public Instance Methods

albums(artist = nil) click to toggle source

Lists all of the albums in the database. The optional argument is for specifying an artist to list the albums for

@return [Array<String>] An array of album names.

# File lib/ruby-mpd/plugins/database.rb, line 133
def albums(artist = nil)
  list :album, artist
end
artists() click to toggle source

Lists all of the artists in the database.

@return [Array<String>] An array of artist names.

# File lib/ruby-mpd/plugins/database.rb, line 140
def artists
  list :artist
end
count(type, what) click to toggle source

Counts the number of songs and their total playtime in the db matching, matching the searched tag exactly.

If you need a count on the entire database, look at {#stats stats}. @return [Hash] a hash with songs and playtime keys.

# File lib/ruby-mpd/plugins/database.rb, line 12
def count(type, what)
  send_command :count, type, what
end
directories(path = nil) click to toggle source

List all of the directories in the database, starting at path. If path isn’t specified, the root of the database is used.

@return [Array<String>] Array of directory names

# File lib/ruby-mpd/plugins/database.rb, line 124
def directories(path = nil)
  return files(path)[:directory]
end
files(path = nil) click to toggle source

List all of the files in the database, starting at path. If path isn’t specified, the root of the database is used.

@return [Hash<String>] hash with array keys :file, :directory and :playlist.

# File lib/ruby-mpd/plugins/database.rb, line 29
def files(path = nil)
  send_command :listall, path
end
list(type, arg = nil) click to toggle source

List all tags of the specified type. Type can be any tag supported by MPD or :file. If type is ‘album’ then arg can be a specific artist to list the albums for

@return [Array<String>]

# File lib/ruby-mpd/plugins/database.rb, line 21
def list(type, arg = nil)
  send_command :list, type, arg
end
rescan(path = nil) click to toggle source

Same as {#update}, but also rescans unmodified files.

@return (see update)

# File lib/ruby-mpd/plugins/database.rb, line 114
def rescan(path = nil)
  send_command :rescan, path
end
songs(path = nil) click to toggle source

List all of the songs in the database starting at path. If path isn’t specified, the root of the database is used

@return [Array<MPD::Song>]

# File lib/ruby-mpd/plugins/database.rb, line 37
def songs(path = nil)
  build_songs_list send_command(:listallinfo, path)
end
songs_by_artist(artist) click to toggle source

List all of the songs by an artist.

@return [Array<MPD::Song>]

# File lib/ruby-mpd/plugins/database.rb, line 147
def songs_by_artist(artist)
  where(artist: artist)
end
update(path = nil) click to toggle source

Tell the server to update the database. Optionally, specify the path to update.

@return [Integer] Update job ID

# File lib/ruby-mpd/plugins/database.rb, line 107
def update(path = nil)
  send_command :update, path
end
where(params, options = {}) click to toggle source

Searches the database for any songs that match the specified parameters. Searching is loose (case insensitive and allow partial matching) by default.

The search keys can be any of the tags supported by MPD, or one of the two special parameters: :file to search by full path (relative to database root), and :any to match against all available tags.

mpd.where(artist: "DJ Shadow", album: "Endtroducing.....")

A hash of options can be passed as a last parameter:

mpd.where({artist: "Nujabes", album: "Modal Soul"}, {add: true})

Options:

  • add: Add search results to the queue.

  • strict: Search will be *case sensitive* and allow only *full matches*.

@param [Hash] params A hash of search parameters. @param [Hash] options A hash of options. @return [Array<MPD::Song>] Songs that matched. @return [true] if :add is enabled.

# File lib/ruby-mpd/plugins/database.rb, line 88
def where(params, options = {})
  if options[:add]
    command = options[:strict] ? :findadd : :searchadd
  else
    command = options[:strict] ? :find : :search
  end

  response = send_command(command, params)
  if response == true
    return true
  else
    build_songs_list response
  end
end