Class: Segue::Queue
- Inherits:
-
Object
- Object
- Segue::Queue
- Defined in:
- lib/segue/queue.rb
Overview
The queue is a directory of yaml files named after the time they were added, so ordering is just sorting by filename.
Constant Summary collapse
- EXTENSIONS =
%w[.mp3 .m4a .flac .ogg .opus .wav .aac .wma].freeze
Class Method Summary collapse
- .add(path) ⇒ Object
- .clear ⇒ Object
- .each ⇒ Object
- .enqueue(track) ⇒ Object
-
.entries ⇒ Object
Dir sorts its results, and the filenames are timestamps, so this is the queue in the order tracks were added.
- .length ⇒ Object
- .remove(index) ⇒ Object
- .swap(index_a, index_b) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Queue
constructor
A new instance of Queue.
- #next ⇒ Object
Constructor Details
#initialize ⇒ Queue
Returns a new instance of Queue.
14 15 16 |
# File 'lib/segue/queue.rb', line 14 def initialize @current_path = nil end |
Class Method Details
.add(path) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/segue/queue.rb', line 71 def add(path) unless EXTENSIONS.include?(File.extname(path).downcase) puts "skipping #{path}" return end puts "adding #{path}" = Segue::Ffmpeg.new(path) enqueue( title: .title, artist: .artist, album: .album, length: .time, path: File.(path) ) end |
.clear ⇒ Object
34 35 36 |
# File 'lib/segue/queue.rb', line 34 def clear FileUtils.rm_rf Segue::Paths.queue end |
.each ⇒ Object
42 43 44 |
# File 'lib/segue/queue.rb', line 42 def each entries.each { |path| yield YAML.load_file(path) } end |
.enqueue(track) ⇒ Object
88 89 90 91 92 |
# File 'lib/segue/queue.rb', line 88 def enqueue(track) File.open(File.join(Segue::Paths.queue, next_name), "w") do |file| file.puts track.to_yaml end end |
.entries ⇒ Object
Dir sorts its results, and the filenames are timestamps, so this is the queue in the order tracks were added
30 31 32 |
# File 'lib/segue/queue.rb', line 30 def entries Dir[File.join(Segue::Paths.queue, "*.yml")] end |
.length ⇒ Object
38 39 40 |
# File 'lib/segue/queue.rb', line 38 def length entries.length end |
.remove(index) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/segue/queue.rb', line 46 def remove(index) path = entries[index.to_i] if path FileUtils.rm_f path yield else puts "Could not find track at position #{index}" end end |
.swap(index_a, index_b) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/segue/queue.rb', line 56 def swap(index_a, index_b) all = entries path_a = all[index_a.to_i] path_b = all[index_b.to_i] if path_a && path_b FileUtils.mv path_a, "#{path_a}.tmp" FileUtils.mv path_b, path_a FileUtils.mv "#{path_a}.tmp", path_b yield else puts "Could not find tracks at positions #{index_a} and #{index_b}" end end |