class MPD::Song

Object representation of a song.

If the field doesn’t exist or isn’t set, nil will be returned

Attributes

album[R]

length in seconds

albumartist[R]

length in seconds

artist[R]

length in seconds

file[R]

length in seconds

time[R]

length in seconds

title[R]

length in seconds

Public Class Methods

new(mpd, options) click to toggle source
# File lib/ruby-mpd/song.rb, line 10
def initialize(mpd, options)
  @mpd = mpd
  @data = {} # allowed fields are @types + :file
  @time = options.delete(:time) # an array of 2 items where last is time
  @file = options.delete(:file)
  @title = options.delete(:title)
  @artist = options.delete(:artist)
  @album = options.delete(:album)
  @albumartist = options.delete(:albumartist)
  @data.merge! options
end

Public Instance Methods

==(another) click to toggle source

Two songs are the same when they share the same hash.

# File lib/ruby-mpd/song.rb, line 23
def ==(another)
  to_h == another.to_h
end
Also aliased as: eql?
comments() click to toggle source

Retrieve “comments” metadata from a file and cache it in the object.

@return [Hash] Key value pairs from “comments” metadata on a file. @return [Boolean] True if comments are empty

# File lib/ruby-mpd/song.rb, line 56
def comments
  @comments ||= @mpd.send_command :readcomments, @file
end
elapsed() click to toggle source
# File lib/ruby-mpd/song.rb, line 38
def elapsed
  @time.first
end
eql?(another)
Alias for: ==
length() click to toggle source

@return [String] A formatted representation of the song length (“1:02”)

# File lib/ruby-mpd/song.rb, line 47
def length
  return '--:--' if track_length.nil?
  "#{track_length / 60}:#{"%02d" % (track_length % 60)}"
end
method_missing(m, *a) click to toggle source

Pass any unknown calls over to the data hash.

# File lib/ruby-mpd/song.rb, line 61
def method_missing(m, *a)
  key = m #.to_s
  if key =~ /=$/
    @data[$`] = a[0]
  elsif a.empty?
    @data[key]
  else
    raise NoMethodError, "#{m}"
  end
end
to_h() click to toggle source
# File lib/ruby-mpd/song.rb, line 27
def to_h
  {
    time: @time,
    file: @file,
    title: @title,
    artist: @artist,
    album: @album,
    albumartist: @albumartist
  }.merge(@data)
end
track_length() click to toggle source
# File lib/ruby-mpd/song.rb, line 42
def track_length
  @time.last
end