From de8d11790c6c20cda59ec14f4188cfbbba6b9eb2 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 14 Dec 2015 04:16:16 +0000 Subject: tracklist: add versioning This will allow us to implement the versioning policy of mpd. But perhaps that's a bad idea... --- lib/dtas/track.rb | 4 +++- lib/dtas/tracklist.rb | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/dtas/track.rb b/lib/dtas/track.rb index e5a3051..e62a30e 100644 --- a/lib/dtas/track.rb +++ b/lib/dtas/track.rb @@ -6,9 +6,11 @@ require_relative '../dtas' class DTAS::Track # :nodoc: attr_reader :track_id attr_reader :to_path + attr_reader :version - def initialize(track_id, path) + def initialize(track_id, path, version) @track_id = track_id @to_path = path + @version = version end end diff --git a/lib/dtas/tracklist.rb b/lib/dtas/tracklist.rb index 6e0dd75..78be091 100644 --- a/lib/dtas/tracklist.rb +++ b/lib/dtas/tracklist.rb @@ -24,6 +24,7 @@ class DTAS::Tracklist # :nodoc: obj = new obj.instance_eval do list = hash['list'] and @list.replace(list.map { |s| new_track(s) }) + _inc_version SIVS.each do |k| instance_variable_set("@#{k}", hash[k] || TL_DEFAULTS[k]) end @@ -62,6 +63,7 @@ class DTAS::Tracklist # :nodoc: @goto_off = @goto_pos = nil @track_nr = 0 @shuffle = false + @version = 0 end def new_track(path) @@ -71,7 +73,7 @@ class DTAS::Tracklist # :nodoc: # avoid promoting to Bignum on 32-bit @track_nr = n = 1 if n >= 0x3fffffff - DTAS::Track.new(n, path) + DTAS::Track.new(n, path, @version) end def reset @@ -147,6 +149,7 @@ class DTAS::Tracklist # :nodoc: _idx_of(@shuffle, after_track_id) or raise ArgumentError, 'after_track_id invalid' end + _inc_version @list[idx, 1] = [ @list[idx], track ] # add into random position if shuffling @@ -161,6 +164,7 @@ class DTAS::Tracklist # :nodoc: @pos += 1 if @pos > idx end else # nil = first_track + _inc_version @list.unshift(track) if @shuffle @@ -197,6 +201,7 @@ class DTAS::Tracklist # :nodoc: len = @list.size @pos = len - 1 if @pos >= len @goto_pos = @goto_pos = nil # TODO: reposition? + _inc_version track.to_path end @@ -244,6 +249,12 @@ class DTAS::Tracklist # :nodoc: end end end + _inc_version true end + + def _inc_version + @version += 1 + @version = 0 if @version > 0xffffffff + end end -- cgit v1.2.3-24-ge0c7