Class: FileQueue
- Inherits:
-
Object
- Object
- FileQueue
- Defined in:
- lib/filequeue.rb
Instance Attribute Summary collapse
-
#delimiter ⇒ Object
Returns the value of attribute delimiter.
-
#file_name ⇒ Object
Returns the value of attribute file_name.
-
#lock_timeout ⇒ Object
Returns the value of attribute lock_timeout.
Instance Method Summary collapse
- #clear ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(file_name, delimiter = "\n", lock_timeout: 10) ⇒ FileQueue
constructor
A new instance of FileQueue.
- #length ⇒ Object
- #pop ⇒ Object
- #push(obj) ⇒ Object (also: #<<)
Constructor Details
#initialize(file_name, delimiter = "\n", lock_timeout: 10) ⇒ FileQueue
6 7 8 9 10 |
# File 'lib/filequeue.rb', line 6 def initialize(file_name, delimiter="\n", lock_timeout: 10) @delimiter = delimiter @file_name = file_name @lock_timeout = lock_timeout end |
Instance Attribute Details
#delimiter ⇒ Object
Returns the value of attribute delimiter.
4 5 6 |
# File 'lib/filequeue.rb', line 4 def delimiter @delimiter end |
#file_name ⇒ Object
Returns the value of attribute file_name.
4 5 6 |
# File 'lib/filequeue.rb', line 4 def file_name @file_name end |
#lock_timeout ⇒ Object
Returns the value of attribute lock_timeout.
4 5 6 |
# File 'lib/filequeue.rb', line 4 def lock_timeout @lock_timeout end |
Instance Method Details
#clear ⇒ Object
50 51 52 |
# File 'lib/filequeue.rb', line 50 def clear safe_open 'w' do |file| end end |
#empty? ⇒ Boolean
46 47 48 |
# File 'lib/filequeue.rb', line 46 def empty? return length == 0 end |
#length ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/filequeue.rb', line 35 def length count = 0 unless(File.exist?(@file_name)) return 0 end safe_open 'r' do |file| count = file.read.count @delimiter end count end |
#pop ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/filequeue.rb', line 23 def pop value = nil safe_open 'r+' do |file| value = file.gets @delimiter rest = file.read file.rewind file.truncate(0) file.write(rest) end value ? value[0..-(@delimiter.length) - 1] : nil end |
#push(obj) ⇒ Object Also known as: <<
12 13 14 15 16 17 18 19 |
# File 'lib/filequeue.rb', line 12 def push(obj) if obj.match Regexp.new @delimiter raise "Queue objects cannot contain the queue delimiter" end safe_open 'a' do |file| file.write(obj + @delimiter) end end |