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) # 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

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.



61
62
63
64
65
66
67
68
69
70
# 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

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 Also known as: eql?

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



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

def ==(another)
  to_h == another.to_h
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



56
57
58
# File 'lib/ruby-mpd/song.rb', line 56

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

#elapsedObject



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

def elapsed
  @time.first
end

#lengthString

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

Returns:

  • (String)

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



47
48
49
50
# File 'lib/ruby-mpd/song.rb', line 47

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

#to_hObject



27
28
29
30
31
32
33
34
35
36
# 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_lengthObject



42
43
44
# File 'lib/ruby-mpd/song.rb', line 42

def track_length
  @time.last
end