Skip to content

Commit 61b4ebd

Browse files
author
Blaž Hrastnik
committed
WIP: Introduce hashie to simplify object wrappers.
1 parent 4124023 commit 61b4ebd

File tree

2 files changed

+11
-27
lines changed

2 files changed

+11
-27
lines changed

lib/ruby-mpd/song.rb

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1+
require 'hashie/mash'
2+
13
class 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
6042
end

ruby-mpd.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
1919
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2020
spec.require_paths = ["lib"]
2121

22+
spec.add_dependency "hashie", "~> 3.4.2"
23+
2224
spec.add_development_dependency "bundler", "~> 1.9"
2325
spec.add_development_dependency "rake", "~> 10.0"
2426
spec.add_development_dependency "minitest", "~> 5.5"

0 commit comments

Comments
 (0)