1+ require 'hashie/mash'
2+
13class MPD ; end
24
35# Object representation of a song.
46#
57# If the field doesn't exist or isn't set, nil will be returned
6- class MPD ::Song
7- # length in seconds
8- attr_reader :file , :title , :time , :artist , :album , :albumartist
8+ class MPD ::Song < Hashie ::Mash
99
10- def initialize ( mpd , options )
10+ # allowed fields are @types + :file
11+ def initialize ( mpd , data )
1112 @mpd = mpd
12- @data = { } # allowed fields are @types + :file
13- @time = options . delete ( :time ) # an array of 2 items where last is time
14- @file = options . delete ( :file )
15- @title = options . delete ( :title )
16- @artist = options . delete ( :artist )
17- @album = options . delete ( :album )
18- @albumartist = options . delete ( :albumartist )
19- @data . merge! options
13+ super data
2014 end
2115
2216 # Two songs are the same when they are the same file.
@@ -25,11 +19,11 @@ def ==(another)
2519 end
2620
2721 def elapsed
28- @ time. first
22+ time . first
2923 end
3024
3125 def track_length
32- @ time. last
26+ time . last
3327 end
3428
3529 # @return [String] A formatted representation of the song length ("1:02")
@@ -43,18 +37,6 @@ def length
4337 # @return [Hash] Key value pairs from "comments" metadata on a file.
4438 # @return [Boolean] True if comments are empty
4539 def comments
46- @comments ||= @mpd . send_command :readcomments , @file
47- end
48-
49- # Pass any unknown calls over to the data hash.
50- def method_missing ( m , *a )
51- key = m #.to_s
52- if key =~ /=$/
53- @data [ $`] = a [ 0 ]
54- elsif a . empty?
55- @data [ key ]
56- else
57- raise NoMethodError , "#{ m } "
58- end
40+ @comments ||= @mpd . send_command :readcomments , file
5941 end
6042end
0 commit comments