Class: Segue::Queue

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeQueue

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}"
  tags = Segue::Ffmpeg.new(path)
  enqueue(
    title: tags.title,
    artist: tags.artist,
    album: tags.album,
    length: tags.time,
    path: File.expand_path(path)
  )
end

.clearObject



34
35
36
# File 'lib/segue/queue.rb', line 34

def clear
  FileUtils.rm_rf Segue::Paths.queue
end

.eachObject



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

.entriesObject

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

.lengthObject



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

Instance Method Details

#nextObject



18
19
20
21
22
23
24
25
# File 'lib/segue/queue.rb', line 18

def next
  FileUtils.rm_f @current_path if @current_path
  @current_path = Segue::Queue.entries.first
  return unless @current_path

  result = YAML.load_file(@current_path)
  File.exist?(result[:path]) ? result : self.next
end