filequeue-mcpolemic 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ff7431204135b29687631dc9c89688a0b5e7cae
4
+ data.tar.gz: fdd1978695cf0b3fe428722c806f5445c36c546b
5
+ SHA512:
6
+ metadata.gz: c61eebb6428c8cc6130877b82225450a940037d982de9d2a79a79ed5a53d1aa6bca540b461ee0bb02efe73346f5afb5450d98d4a93fe86d90f87a06d8c6852fb
7
+ data.tar.gz: 6fe6ba8f9fcdd4c76c03f57e76d7670d6f9453a832d99144467d36f81f99b31077dd5ab145b50988aa519853c75d8f19cb8a46022f7b0b7860d700a1ee8361e2
data/.gemtest ADDED
File without changes
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --backtrace
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,26 @@
1
+
2
+
3
+ ## FileQueue [![Build Status](https://travis-ci.org/pezra/filequeue.svg)](https://travis-ci.org/pezra/filequeue)
4
+ ...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. It's not web scale but is nice for lightweight async queuing apps.
5
+
6
+ Originally written by [daddz](http://www.github.com/daddz) and found in [this gist](https://gist.github.com/352509). Thanks, daddz!
7
+
8
+ ## Install
9
+
10
+ gem install filequeue
11
+
12
+ ## Usage
13
+
14
+ queue = FileQueue.new 'queue_party.txt'
15
+
16
+ queue.push "an item"
17
+ => true
18
+
19
+ queue.pop
20
+ => "an item"
21
+
22
+ See `spec/filequeue_spec.rb` for more usage details
23
+
24
+ ## Continuous Integration
25
+
26
+ [![Build Status](http://travis-ci.org/maxogden/filequeue.png)](http://travis-ci.org/maxogden/filequeue)
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.2
data/filequeue.gemspec ADDED
@@ -0,0 +1,43 @@
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-mcpolemic}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Max Ogden", "daddz", "Peter Williams"]
12
+ s.date = %q{2011-04-05}
13
+ s.description = %q{A simple file based queue in Ruby}
14
+ s.email = %q{pezra@barelyenough.org}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".gemtest",
21
+ ".rspec",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "filequeue.gemspec",
27
+ "lib/filequeue.rb",
28
+ "pkg/filequeue-0.0.2.gem",
29
+ "spec/filequeue_spec.rb",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/maxogden/filequeue}
33
+ s.require_paths = ["lib"]
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
+ s.add_development_dependency "rspec", "~> 3.3"
41
+ s.add_development_dependency "rake"
42
+ end
43
+
data/lib/filequeue.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'timeout'
2
+
3
+ class FileQueue
4
+ attr_accessor :file_name, :delimiter
5
+
6
+ def initialize(file_name, delimiter="\n")
7
+ @delimiter = delimiter
8
+ @file_name = file_name
9
+ end
10
+
11
+ def push(obj)
12
+ if obj.match Regexp.new @delimiter
13
+ raise "Queue objects cannot contain the queue delimiter"
14
+ end
15
+ safe_open 'a' do |file|
16
+ file.write(obj + @delimiter)
17
+ end
18
+ end
19
+
20
+ alias << push
21
+
22
+ def pop
23
+ value = nil
24
+ rest = nil
25
+ safe_open 'r+' do |file|
26
+ value = file.gets @delimiter
27
+ rest = file.read
28
+ file.rewind
29
+ file.write rest
30
+ file.truncate(file.pos)
31
+ end
32
+ value ? value[0..-(@delimiter.length) - 1] : nil
33
+ end
34
+
35
+ def length
36
+ count = 0
37
+ safe_open 'r' do |file|
38
+ count = file.read.count @delimiter
39
+ end
40
+ count
41
+ end
42
+
43
+ def empty?
44
+ return length == 0
45
+ end
46
+
47
+ def clear
48
+ safe_open 'w' do |file| end
49
+ end
50
+
51
+ protected
52
+
53
+ def safe_open(mode)
54
+ File.open(@file_name, mode) do |file|
55
+ lock file
56
+ yield file
57
+ end
58
+ end
59
+
60
+ # Locks the queue file for exclusive access.
61
+ #
62
+ # Raises `FileLockError` if unable to acquire a lock.
63
+ #
64
+ # Return is undefined.
65
+ def lock(file)
66
+ tries = 1000
67
+ until tries == 0 || lock_acquired = file.flock(File::LOCK_NB|File::LOCK_EX)
68
+ tries -= 1
69
+ Thread.pass
70
+ end
71
+
72
+ (raise FileLockError, "Queue file appears to be permanently lockecd") unless lock_acquired
73
+ end
74
+
75
+ FileLockError = Class.new(Timeout::Error)
76
+ end
Binary file
@@ -0,0 +1,110 @@
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
+
97
+ describe '#safe_open' do
98
+ it 'should lock files when doing IO' do
99
+ # Expect that we lock files
100
+ File.any_instance.should_receive(:flock).and_return(0)
101
+
102
+ subject.push("things")
103
+ end
104
+
105
+ it 'should raise FileLockError if unable to acquire lock' do
106
+ File.open(test_file).flock(File::LOCK_EX | File::LOCK_NB)
107
+ lambda { subject.push("example") }.should raise_exception(FileQueue::FileLockError)
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'filequeue'
4
+
5
+ RSpec.configure do |c|
6
+ c.expect_with(:rspec) { |c| c.syntax = :should }
7
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filequeue-mcpolemic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Max Ogden
8
+ - daddz
9
+ - Peter Williams
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-04-05 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '3.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '3.3'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ description: A simple file based queue in Ruby
44
+ email: pezra@barelyenough.org
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files:
48
+ - LICENSE
49
+ - README.md
50
+ files:
51
+ - ".gemtest"
52
+ - ".rspec"
53
+ - LICENSE
54
+ - README.md
55
+ - Rakefile
56
+ - VERSION
57
+ - filequeue.gemspec
58
+ - lib/filequeue.rb
59
+ - pkg/filequeue-0.0.2.gem
60
+ - spec/filequeue_spec.rb
61
+ - spec/spec_helper.rb
62
+ homepage: http://github.com/maxogden/filequeue
63
+ licenses: []
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.4.5.1
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: A simple file based queue in Ruby
85
+ test_files:
86
+ - spec/filequeue_spec.rb
87
+ - spec/spec_helper.rb