Class: MPD::Song
- Inherits:
-
Object
- Object
- MPD::Song
- 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
-
#album ⇒ Object
readonly
length in seconds.
-
#albumartist ⇒ Object
readonly
length in seconds.
-
#artist ⇒ Object
readonly
length in seconds.
-
#file ⇒ Object
readonly
length in seconds.
-
#time ⇒ Object
readonly
length in seconds.
-
#title ⇒ Object
readonly
length in seconds.
Instance Method Summary collapse
-
#==(another) ⇒ Object
Two songs are the same when they are the same file.
-
#initialize(options) ⇒ Song
constructor
A new instance of Song.
-
#length ⇒ String
A formatted representation of the song length (“1:02”).
-
#method_missing(m, *a) ⇒ Object
Pass any unknown calls over to the data hash.
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() @data = {} # allowed fields are @types + :file @time = .delete(:time) { [nil] }.first # HAXX for array return @file = .delete(:file) @title = .delete(:title) @artist = .delete(:artist) @album = .delete(:album) @albumartist = .delete(:albumartist) @data.merge! 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
#album ⇒ Object (readonly)
length in seconds
8 9 10 |
# File 'lib/ruby-mpd/song.rb', line 8 def album @album end |
#albumartist ⇒ Object (readonly)
length in seconds
8 9 10 |
# File 'lib/ruby-mpd/song.rb', line 8 def albumartist @albumartist end |
#artist ⇒ Object (readonly)
length in seconds
8 9 10 |
# File 'lib/ruby-mpd/song.rb', line 8 def artist @artist end |
#file ⇒ Object (readonly)
length in seconds
8 9 10 |
# File 'lib/ruby-mpd/song.rb', line 8 def file @file end |
#time ⇒ Object (readonly)
length in seconds
8 9 10 |
# File 'lib/ruby-mpd/song.rb', line 8 def time @time end |
#title ⇒ Object (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 |
#length ⇒ String
Returns 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 |