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(options) ⇒ Song

Returns a new instance of Song.



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

def initialize(options)
  @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.



33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby-mpd/song.rb', line 33

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.



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

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

#lengthString

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

Returns:

  • (String)

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



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

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