Class: Rider::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/rider/queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Queue

Returns a new instance of Queue.

Raises:

  • (ArgumentError)


4
5
6
7
# File 'lib/rider/queue.rb', line 4

def initialize(filename)
  raise(ArgumentError, "queues must have a filename") if !filename or filename.empty?
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



3
4
5
# File 'lib/rider/queue.rb', line 3

def filename
  @filename
end

Instance Method Details

#clearObject



31
32
33
34
# File 'lib/rider/queue.rb', line 31

def clear
  File.unlink(filename) if File.exist?(filename)
  return true
end

#empty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rider/queue.rb', line 36

def empty?
  !File.exist?(filename) or File.open(filename).read == ""
end

#push(item) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/rider/queue.rb', line 9

def push(item)
  Rider.log.debug("Q #{filename} PUSH #{item}")
  File.open(filename, "a") do |file|
    file.puts(item)
  end
  return true
end

#shiftObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rider/queue.rb', line 17

def shift
  if empty?
    Rider.log.debug("Q #{filename} SHIFT nil")        
    return nil
  end
  lines = File.readlines(filename)
  item = lines.shift.strip
  File.open(filename, "w") do |file|
    file.write(lines.join)
  end
  Rider.log.debug("Q #{filename} SHIFT #{item}")
  return item
end