filequeue 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +3 -0
- data/LICENSE +20 -0
- data/README.md +20 -0
- data/Rakefile +23 -0
- data/VERSION +1 -0
- data/file_queue.rb +52 -0
- data/filequeue.gemspec +49 -0
- data/lib/filequeue.rb +57 -0
- data/spec/filequeue_spec.rb +96 -0
- data/spec/spec_helper.rb +4 -0
- metadata +79 -0
data/.rspec
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Max Ogden and daddz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
## FileQueue
|
2
|
+
...is a simple file based queue written in Ruby that uses the Ruby `File` class in standard library to push and pop items into a queue.
|
3
|
+
|
4
|
+
Originally written by [daddz](http://www.github.com/daddz) and found in [this gist](https://gist.github.com/352509). Thanks, daddz!
|
5
|
+
|
6
|
+
## Install
|
7
|
+
|
8
|
+
gem install filequeue
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
queue = FileQueue.new 'queue_party.txt'
|
13
|
+
|
14
|
+
queue.push "an item"
|
15
|
+
=> true
|
16
|
+
|
17
|
+
queue.pop
|
18
|
+
=> "an item"
|
19
|
+
|
20
|
+
See `spec/filequeue_spec.rb` for more usage details
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "filequeue"
|
8
|
+
gem.summary = %Q{A simple file based queue in Ruby}
|
9
|
+
gem.description = %Q{A simple file based queue in Ruby}
|
10
|
+
gem.email = "max@maxogden.com"
|
11
|
+
gem.homepage = "http://github.com/maxogden/filequeue"
|
12
|
+
gem.authors = ["Max Ogden", "daddz"]
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rspec/core/rake_task'
|
20
|
+
RSpec::Core::RakeTask.new(:spec)
|
21
|
+
|
22
|
+
task :default => :spec
|
23
|
+
task :test => :spec
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/file_queue.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
class FileQueue
|
2
|
+
|
3
|
+
def initialize(file_name)
|
4
|
+
@file_name = file_name
|
5
|
+
end
|
6
|
+
|
7
|
+
def push(obj)
|
8
|
+
safe_open('a') do |file|
|
9
|
+
file.write(obj + "\n")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
alias << push
|
14
|
+
|
15
|
+
def pop
|
16
|
+
value = nil
|
17
|
+
rest = nil
|
18
|
+
safe_open('r') do |file|
|
19
|
+
value = file.gets
|
20
|
+
rest = file.read
|
21
|
+
end
|
22
|
+
safe_open('w+') do |file|
|
23
|
+
file.write(rest)
|
24
|
+
end
|
25
|
+
value
|
26
|
+
end
|
27
|
+
|
28
|
+
def length
|
29
|
+
count = 0
|
30
|
+
safe_open('r') do |file|
|
31
|
+
count = file.read.count("\n")
|
32
|
+
end
|
33
|
+
count
|
34
|
+
end
|
35
|
+
|
36
|
+
def empty?
|
37
|
+
return length == 0
|
38
|
+
end
|
39
|
+
|
40
|
+
def clear
|
41
|
+
safe_open('w') do |file| end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def safe_open(mode)
|
46
|
+
File.open(@file_name, mode) do |file|
|
47
|
+
file.flock(File::LOCK_EX)
|
48
|
+
yield file
|
49
|
+
file.flock(File::LOCK_UN)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/filequeue.gemspec
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{filequeue}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Max Ogden", "daddz"]
|
12
|
+
s.date = %q{2011-04-05}
|
13
|
+
s.description = %q{A simple file based queue in Ruby}
|
14
|
+
s.email = %q{max@maxogden.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".rspec",
|
21
|
+
"LICENSE",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"file_queue.rb",
|
26
|
+
"filequeue.gemspec",
|
27
|
+
"lib/filequeue.rb",
|
28
|
+
"spec/filequeue_spec.rb",
|
29
|
+
"spec/spec_helper.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/maxogden/filequeue}
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = %q{1.4.2}
|
34
|
+
s.summary = %q{A simple file based queue in Ruby}
|
35
|
+
s.test_files = [
|
36
|
+
"spec/filequeue_spec.rb",
|
37
|
+
"spec/spec_helper.rb"
|
38
|
+
]
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
else
|
45
|
+
end
|
46
|
+
else
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
data/lib/filequeue.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
class FileQueue
|
2
|
+
attr_accessor :file_name, :delimiter
|
3
|
+
|
4
|
+
def initialize(file_name, delimiter="\n")
|
5
|
+
@delimiter = delimiter
|
6
|
+
@file_name = file_name
|
7
|
+
end
|
8
|
+
|
9
|
+
def push(obj)
|
10
|
+
if obj.match Regexp.new @delimiter
|
11
|
+
raise "Queue objects cannot contain the queue delimiter"
|
12
|
+
end
|
13
|
+
safe_open 'a' do |file|
|
14
|
+
file.write(obj + @delimiter)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
alias << push
|
19
|
+
|
20
|
+
def pop
|
21
|
+
value = nil
|
22
|
+
rest = nil
|
23
|
+
safe_open 'r' do |file|
|
24
|
+
value = file.gets @delimiter
|
25
|
+
rest = file.read
|
26
|
+
end
|
27
|
+
safe_open 'w+' do |file|
|
28
|
+
file.write rest
|
29
|
+
end
|
30
|
+
value ? value[0..-(@delimiter.length) - 1] : nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def length
|
34
|
+
count = 0
|
35
|
+
safe_open 'r' do |file|
|
36
|
+
count = file.read.count @delimiter
|
37
|
+
end
|
38
|
+
count
|
39
|
+
end
|
40
|
+
|
41
|
+
def empty?
|
42
|
+
return length == 0
|
43
|
+
end
|
44
|
+
|
45
|
+
def clear
|
46
|
+
safe_open 'w' do |file| end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def safe_open(mode)
|
51
|
+
File.open(@file_name, mode) do |file|
|
52
|
+
file.flock File::LOCK_EX
|
53
|
+
yield file
|
54
|
+
file.flock File::LOCK_UN
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
test_file = "pizza.waffle"
|
4
|
+
|
5
|
+
describe FileQueue do
|
6
|
+
subject do
|
7
|
+
FileQueue.new test_file
|
8
|
+
end
|
9
|
+
|
10
|
+
after :each do
|
11
|
+
File.open(test_file, 'w') {|file| file.truncate 0 }
|
12
|
+
end
|
13
|
+
|
14
|
+
after :all do
|
15
|
+
File.delete test_file
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#initialize' do
|
19
|
+
it 'should have a file name and default delimiter' do
|
20
|
+
subject.file_name.should == test_file
|
21
|
+
subject.delimiter.should == "\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should let you specify your own queue delimiter' do
|
25
|
+
FileQueue.new(test_file, ",").delimiter.should == ","
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#push' do
|
30
|
+
it 'should add an entry to the queue' do
|
31
|
+
subject.push "bacon"
|
32
|
+
File.open(test_file, 'r').read.should == "bacon\n"
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should add multiple entries to the queue' do
|
36
|
+
subject.push "cats"
|
37
|
+
subject.push "are awesome"
|
38
|
+
File.open(test_file, 'r').read.should == "cats\nare awesome\n"
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should use the delimiter to separate entries in the queue' do
|
42
|
+
FileQueue.new(test_file, ",").push "jello"
|
43
|
+
File.open(test_file, 'r').read.should == "jello,"
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should raise if your object contains the delimiter' do
|
47
|
+
lambda { subject.push "yee\nhaw" }.should raise_error(StandardError, "Queue objects cannot contain the queue delimiter")
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should let you use the << syntax to add things to the queue' do
|
51
|
+
subject << "angle brackets!"
|
52
|
+
subject.pop.should == "angle brackets!"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#pop' do
|
57
|
+
it 'should retrieve an item from the queue' do
|
58
|
+
subject.push "important document"
|
59
|
+
subject.pop.should == "important document"
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should return the oldest item in the queue' do
|
63
|
+
subject.push "old thing"
|
64
|
+
subject.push "new thing"
|
65
|
+
subject.pop.should == "old thing"
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should return nil if the queue is empty' do
|
69
|
+
subject.pop.should be_nil
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '#length' do
|
74
|
+
it 'should tell you the size of the queue' do
|
75
|
+
subject.length.should == 0
|
76
|
+
subject.push "content"
|
77
|
+
subject.length.should == 1
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#empty' do
|
82
|
+
it 'should tell you if the queue is empty or not' do
|
83
|
+
subject.empty?.should be_true
|
84
|
+
subject.push "content"
|
85
|
+
subject.empty?.should be_false
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe '#clear' do
|
90
|
+
it 'should empty the queue' do
|
91
|
+
subject.push "content"
|
92
|
+
subject.clear
|
93
|
+
subject.empty?.should be_true
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filequeue
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Max Ogden
|
14
|
+
- daddz
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-04-05 00:00:00 -07:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: A simple file based queue in Ruby
|
24
|
+
email: max@maxogden.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files:
|
30
|
+
- LICENSE
|
31
|
+
- README.md
|
32
|
+
files:
|
33
|
+
- .rspec
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- VERSION
|
38
|
+
- file_queue.rb
|
39
|
+
- filequeue.gemspec
|
40
|
+
- lib/filequeue.rb
|
41
|
+
- spec/filequeue_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/maxogden/filequeue
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.4.2
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: A simple file based queue in Ruby
|
77
|
+
test_files:
|
78
|
+
- spec/filequeue_spec.rb
|
79
|
+
- spec/spec_helper.rb
|