Class: MPD::Song

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-mpd/song.rb

Overview

Object representation of a song.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mpd, options) ⇒ Song

Returns a new instance of Song.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby-mpd/song.rb', line 10

def initialize(mpd, options)
  @mpd = mpd
  @data = {} # allowed fields are @types + :file
  @time = options.delete(:time) { [nil] }.first # HAXX for array return
  @file = options.delete(:file)
  @title = options.delete(:title)
  @artist = options.delete(:artist)
  @album = options.delete(:album)
  @albumartist = options.delete(:albumartist)
  @data.merge! options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a) ⇒ Object

Pass any unknown calls over to the data hash.



42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby-mpd/song.rb', line 42

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

Instance Attribute Details

#albumObject (readonly)

length in seconds



8
9
10
# File 'lib/ruby-mpd/song.rb', line 8

def album
  @album
end

#albumartistObject (readonly)

length in seconds



8
9
10
# File 'lib/ruby-mpd/song.rb', line 8

def albumartist
  @albumartist
end

#artistObject (readonly)

length in seconds



8
9
10
# File 'lib/ruby-mpd/song.rb', line 8

def artist
  @artist
end

#fileObject (readonly)

length in seconds



8
9
10
# File 'lib/ruby-mpd/song.rb', line 8

def file
  @file
end

#timeObject (readonly)

length in seconds



8
9
10
# File 'lib/ruby-mpd/song.rb', line 8

def time
  @time
end

#titleObject (readonly)

length in seconds



8
9
10
# File 'lib/ruby-mpd/song.rb', line 8

def title
  @title
end

Instance Method Details

#==(another) ⇒ Object

Two songs are the same when they are the same file.



23
24
25
# File 'lib/ruby-mpd/song.rb', line 23

def ==(another)
  self.class == another.class && self.file == another.file
end

#commentsHash, Boolean

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

Returns:

  • (Hash)

    Key value pairs from “comments” metadata on a file.

  • (Boolean)

    True if comments are empty



37
38
39
# File 'lib/ruby-mpd/song.rb', line 37

def comments
    @comments ||= @mpd.send_command :readcomments, @file
end

#lengthString

Returns A formatted representation of the song length (“1:02”).

Returns:

  • (String)

    A formatted representation of the song length (“1:02”)



28
29
30
31
# File 'lib/ruby-mpd/song.rb', line 28

def length
  return '--:--' if @time.nil?
  "#{@time / 60}:#{"%02d" % (@time % 60)}"
end