Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions lib/ruby-mpd/song.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
require 'hashie/mash'

class MPD; end

# Object representation of a song.
#
# If the field doesn't exist or isn't set, nil will be returned
class MPD::Song
# length in seconds
attr_reader :file, :title, :time, :artist, :album, :albumartist
class MPD::Song < Hashie::Mash

def initialize(mpd, options)
# allowed fields are @types + :file
def initialize(mpd, data)
@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
super data
end

# Two songs are the same when they are the same file.
Expand All @@ -25,11 +19,11 @@ def ==(another)
end

def elapsed
@time.first
time.first
end

def track_length
@time.last
time.last
end

# @return [String] A formatted representation of the song length ("1:02")
Expand All @@ -43,18 +37,6 @@ def length
# @return [Hash] Key value pairs from "comments" metadata on a file.
# @return [Boolean] True if comments are empty
def comments
@comments ||= @mpd.send_command :readcomments, @file
end

# Pass any unknown calls over to the data hash.
def method_missing(m, *a)
key = m #.to_s
if key =~ /=$/
@data[$`] = a[0]
elsif a.empty?
@data[key]
else
raise NoMethodError, "#{m}"
end
@comments ||= @mpd.send_command :readcomments, file
end
end
2 changes: 2 additions & 0 deletions ruby-mpd.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "hashie", "~> 3.4.2"

spec.add_development_dependency "bundler", "~> 1.9"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", "~> 5.5"
Expand Down