Module: MPD::Plugins::Database

Included in:
MPD
Defined in:
lib/ruby-mpd/plugins/database.rb

Overview

Commands for interacting with the music database.

Instance Method Summary collapse

Instance Method Details

#albums(artist = nil) ⇒ Array<String>

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

Returns:

  • (Array<String>)

    An array of album names.



133
134
135
# File 'lib/ruby-mpd/plugins/database.rb', line 133

def albums(artist = nil)
  list :album, artist
end

#artistsArray<String>

Lists all of the artists in the database.

Returns:

  • (Array<String>)

    An array of artist names.



140
141
142
# File 'lib/ruby-mpd/plugins/database.rb', line 140

def artists
  list :artist
end

#count(type, what) ⇒ Hash

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.

Returns:

  • (Hash)

    a hash with songs and playtime keys.



12
13
14
# File 'lib/ruby-mpd/plugins/database.rb', line 12

def count(type, what)
  send_command :count, type, what
end

#directories(path = nil) ⇒ Array<String>

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

Returns:

  • (Array<String>)

    Array of directory names



124
125
126
# File 'lib/ruby-mpd/plugins/database.rb', line 124

def directories(path = nil)
  return files(path)[:directory]
end

#files(path = nil) ⇒ Hash<String>

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

Returns:

  • (Hash<String>)

    hash with array keys :file, :directory and :playlist.



29
30
31
# File 'lib/ruby-mpd/plugins/database.rb', line 29

def files(path = nil)
  send_command :listall, path
end

#list(type, arg = nil) ⇒ Array<String>

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

Returns:

  • (Array<String>)


21
22
23
# File 'lib/ruby-mpd/plugins/database.rb', line 21

def list(type, arg = nil)
  send_command :list, type, arg
end

#rescan(path = nil) ⇒ Integer

Same as #update, but also rescans unmodified files.

Returns:

  • (Integer)

    Update job ID



114
115
116
# File 'lib/ruby-mpd/plugins/database.rb', line 114

def rescan(path = nil)
  send_command :rescan, path
end

#search(type, what, options = {}) ⇒ Array<MPD::Song>, true

Deprecated.

Use #where instead.

Searches for any song that contains what in the type field. Searches are case insensitive by default, however you can enable it using the third argument.

Options:

  • add: Add the search results to the queue.

  • case_sensitive: Make the query case sensitive.

Parameters:

  • type (Symbol)

    Can be any tag 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.

  • options (Hash) (defaults to: {})

    A hash of options.

Returns:

  • (Array<MPD::Song>)

    Songs that matched.

  • (true)

    if :add is enabled.



60
61
62
63
64
# File 'lib/ruby-mpd/plugins/database.rb', line 60

def search(type, what, options = {})
  warn "MPD#search is deprecated! Use MPD#where instead."
  options[:strict] = options[:case_sensitive] # transparently upgrade renamed option
  where({type => what}, options)
end

#songs(path = nil) ⇒ Array<MPD::Song>

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

Returns:



37
38
39
# 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) ⇒ Array<MPD::Song>

List all of the songs by an artist.

Returns:



147
148
149
# File 'lib/ruby-mpd/plugins/database.rb', line 147

def songs_by_artist(artist)
  where(artist: artist)
end

#update(path = nil) ⇒ Integer

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

Returns:

  • (Integer)

    Update job ID



107
108
109
# File 'lib/ruby-mpd/plugins/database.rb', line 107

def update(path = nil)
  send_command :update, path
end

#where(params, options = {}) ⇒ Array<MPD::Song>, true

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*.

Parameters:

  • params (Hash)

    A hash of search parameters.

  • options (Hash) (defaults to: {})

    A hash of options.

Returns:

  • (Array<MPD::Song>)

    Songs that matched.

  • (true)

    if :add is enabled.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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