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
(also: #eql?)
Two songs are the same when they share the same hash.
-
#comments ⇒ Hash, Boolean
Retrieve “comments” metadata from a file and cache it in the object.
- #elapsed ⇒ Object
-
#initialize(mpd, 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.
- #to_h ⇒ Object
- #track_length ⇒ Object
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, ) @mpd = mpd @data = {} # allowed fields are @types + :file @time = .delete(:time) # an array of 2 items where last is time @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.
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
#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 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 |
#comments ⇒ Hash, Boolean
Retrieve “comments” metadata from a file and cache it in the object.
56 57 58 |
# File 'lib/ruby-mpd/song.rb', line 56 def comments @comments ||= @mpd.send_command :readcomments, @file end |
#elapsed ⇒ Object
38 39 40 |
# File 'lib/ruby-mpd/song.rb', line 38 def elapsed @time.first end |
#length ⇒ String
Returns 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_h ⇒ Object
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_length ⇒ Object
42 43 44 |
# File 'lib/ruby-mpd/song.rb', line 42 def track_length @time.last end |