lib/radiodan/playlist.rb in radiodan-0.0.4 vs lib/radiodan/playlist.rb in radiodan-1.0.0
- old
+ new
@@ -27,11 +27,12 @@
extend Forwardable
STATES = [:play, :stop, :pause]
MODES = [:sequential, :resume, :random]
attr_reader :state, :mode, :repeat, :tracks, :position, :seek, :volume
- def_delegators :@tracks, :size
+ alias_method :repeat?, :repeat
+ def_delegators :@tracks, :size, :length, :empty?
def initialize(options={})
self.state = options.fetch(:state, STATES.first)
self.mode = options.fetch(:mode, MODES.first)
self.repeat = options.fetch(:repeat, false)
@@ -42,11 +43,19 @@
end
def current
tracks[position]
end
-
+
+ def random?
+ self.mode == :random
+ end
+
+ def state
+ empty? ? :stop : @state
+ end
+
def state=(new_state)
state = new_state.to_sym
if STATES.include? state
@state = state
@@ -96,17 +105,29 @@
raise SeekError, "#{new_seek} invalid"
end
end
def volume=(new_volume)
+ # -1 is allowed when volume cannot be determined
begin
new_volume = Integer(new_volume)
- raise ArgumentError if new_volume > 100 || new_volume < 0
+ raise ArgumentError if new_volume > 100 || new_volume < -1
rescue ArgumentError
- raise VolumeError, "#{new_volume} not an integer 0-100"
+ raise VolumeError, "#{new_volume} not an integer -1-100"
end
@volume = new_volume
end
+
+ def attributes
+ { :state => state,
+ :mode => mode,
+ :repeat => repeat,
+ :tracks => begin tracks.collect(&:attributes) rescue []; end,
+ :position => position,
+ :seek => seek,
+ :volume => volume }
+ end
+ alias_method :as_json, :attributes
end
end