backgrounded 0.3.1
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.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +68 -0
- data/Rakefile +56 -0
- data/VERSION.yml +5 -0
- data/backgrounded.gemspec +53 -0
- data/lib/backgrounded.rb +37 -0
- data/lib/handler.rb +10 -0
- data/lib/handler/delayed_job_handler.rb +13 -0
- data/lib/handler/inprocess_handler.rb +11 -0
- data/test/backgrounded_test.rb +75 -0
- data/test/test_helper.rb +11 -0
- metadata +69 -0
data/.document
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2009 Ryan Sonnek
|
|
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.rdoc
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
= Backgrounded
|
|
2
|
+
|
|
3
|
+
Background processing done right.
|
|
4
|
+
|
|
5
|
+
= Features
|
|
6
|
+
* clean and concise API
|
|
7
|
+
* testable
|
|
8
|
+
* integrates with any background processing framework (DelayedJob, JobFu, Workling, etc)
|
|
9
|
+
|
|
10
|
+
= Usage
|
|
11
|
+
|
|
12
|
+
#declaration
|
|
13
|
+
class User
|
|
14
|
+
backgrounded :do_stuff
|
|
15
|
+
def do_stuff
|
|
16
|
+
# do all your work here
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
#usage
|
|
21
|
+
user = User.new
|
|
22
|
+
user.do_stuff_backgrounded
|
|
23
|
+
|
|
24
|
+
= Installation
|
|
25
|
+
|
|
26
|
+
Command line installation
|
|
27
|
+
|
|
28
|
+
sudo gem install wireframe-backgrounded
|
|
29
|
+
|
|
30
|
+
Rails environment.rb configuration
|
|
31
|
+
|
|
32
|
+
config.gem 'wireframe-backgrounded', :source => 'http://gems.github.com', :lib => 'backgrounded'
|
|
33
|
+
|
|
34
|
+
= Configuration
|
|
35
|
+
|
|
36
|
+
Backgrounded provides a thin wrapper around any background processing framework that implements the Backgrounded handler API. This makes it trivial to swap out processing frameworks with no impact on your code.
|
|
37
|
+
|
|
38
|
+
= DelayedJob
|
|
39
|
+
|
|
40
|
+
An implementation for DelayedJob is packaged directly with Backgrounded for a slick out of the box experience.
|
|
41
|
+
see http://github.com/tobi/delayed_job/tree/master
|
|
42
|
+
|
|
43
|
+
# rails config/initializers/backgrounded.rb
|
|
44
|
+
Backgrounded.handler = Backgrounded::Handler::DelayedJobHandler.new
|
|
45
|
+
|
|
46
|
+
= JobFu
|
|
47
|
+
|
|
48
|
+
Developers using the JobFu library have it easy as well!
|
|
49
|
+
see http://github.com/jnstq/job_fu/tree
|
|
50
|
+
|
|
51
|
+
# rails config/initializers/backgrounded.rb
|
|
52
|
+
Backgrounded.handler = JobFu::Backgrounded::Handler.new
|
|
53
|
+
|
|
54
|
+
= Custom Handlers
|
|
55
|
+
|
|
56
|
+
It's trivial to write your own plugin for processing Backgrounded events!
|
|
57
|
+
|
|
58
|
+
# rails config/initializers/backgrounded.rb
|
|
59
|
+
class MyHandler
|
|
60
|
+
def request(object, method, *args)
|
|
61
|
+
#process the call however you want!
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
Backgrounded.handler = MyHandler.new
|
|
65
|
+
|
|
66
|
+
== Copyright
|
|
67
|
+
|
|
68
|
+
Copyright (c) 2009 Ryan Sonnek. See LICENSE for details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake'
|
|
3
|
+
|
|
4
|
+
begin
|
|
5
|
+
require 'jeweler'
|
|
6
|
+
Jeweler::Tasks.new do |gem|
|
|
7
|
+
gem.name = "backgrounded"
|
|
8
|
+
gem.summary = %Q{Simple API to run Model methods in the background}
|
|
9
|
+
gem.email = "ryan.sonnek@gmail.com"
|
|
10
|
+
gem.homepage = "http://github.com/wireframe/backgrounded"
|
|
11
|
+
gem.authors = ["Ryan Sonnek"]
|
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
|
13
|
+
end
|
|
14
|
+
Jeweler::GemcutterTasks.new
|
|
15
|
+
rescue LoadError
|
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
require 'rake/testtask'
|
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
|
21
|
+
test.libs << 'lib' << 'test'
|
|
22
|
+
test.pattern = 'test/**/*_test.rb'
|
|
23
|
+
test.verbose = true
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
begin
|
|
27
|
+
require 'rcov/rcovtask'
|
|
28
|
+
Rcov::RcovTask.new do |test|
|
|
29
|
+
test.libs << 'test'
|
|
30
|
+
test.pattern = 'test/**/*_test.rb'
|
|
31
|
+
test.verbose = true
|
|
32
|
+
end
|
|
33
|
+
rescue LoadError
|
|
34
|
+
task :rcov do
|
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
task :default => :test
|
|
41
|
+
|
|
42
|
+
require 'rake/rdoctask'
|
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
|
44
|
+
if File.exist?('VERSION.yml')
|
|
45
|
+
config = YAML.load(File.read('VERSION.yml'))
|
|
46
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
|
47
|
+
else
|
|
48
|
+
version = ""
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
52
|
+
rdoc.title = "backgrounded #{version}"
|
|
53
|
+
rdoc.rdoc_files.include('README*')
|
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
55
|
+
end
|
|
56
|
+
|
data/VERSION.yml
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Generated by jeweler
|
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
|
4
|
+
# -*- encoding: utf-8 -*-
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |s|
|
|
7
|
+
s.name = %q{backgrounded}
|
|
8
|
+
s.version = "0.3.1"
|
|
9
|
+
|
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
|
+
s.authors = ["Ryan Sonnek"]
|
|
12
|
+
s.date = %q{2009-10-26}
|
|
13
|
+
s.email = %q{ryan.sonnek@gmail.com}
|
|
14
|
+
s.extra_rdoc_files = [
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"README.rdoc"
|
|
17
|
+
]
|
|
18
|
+
s.files = [
|
|
19
|
+
".document",
|
|
20
|
+
".gitignore",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"README.rdoc",
|
|
23
|
+
"Rakefile",
|
|
24
|
+
"VERSION.yml",
|
|
25
|
+
"backgrounded.gemspec",
|
|
26
|
+
"lib/backgrounded.rb",
|
|
27
|
+
"lib/handler.rb",
|
|
28
|
+
"lib/handler/delayed_job_handler.rb",
|
|
29
|
+
"lib/handler/inprocess_handler.rb",
|
|
30
|
+
"test/backgrounded_test.rb",
|
|
31
|
+
"test/test_helper.rb"
|
|
32
|
+
]
|
|
33
|
+
s.homepage = %q{http://github.com/wireframe/backgrounded}
|
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
|
35
|
+
s.require_paths = ["lib"]
|
|
36
|
+
s.rubygems_version = %q{1.3.5}
|
|
37
|
+
s.summary = %q{Simple API to run Model methods in the background}
|
|
38
|
+
s.test_files = [
|
|
39
|
+
"test/backgrounded_test.rb",
|
|
40
|
+
"test/test_helper.rb"
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
if s.respond_to? :specification_version then
|
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
45
|
+
s.specification_version = 3
|
|
46
|
+
|
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
48
|
+
else
|
|
49
|
+
end
|
|
50
|
+
else
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
data/lib/backgrounded.rb
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require 'activesupport'
|
|
2
|
+
|
|
3
|
+
module Backgrounded
|
|
4
|
+
mattr_accessor :handler
|
|
5
|
+
def self.handler
|
|
6
|
+
@@handler ||= Backgrounded::Handler::InprocessHandler.new
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
autoload :Handler, 'handler'
|
|
10
|
+
|
|
11
|
+
module Model
|
|
12
|
+
def self.included(base)
|
|
13
|
+
base.extend(ClassMethods)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module ClassMethods
|
|
17
|
+
def backgrounded(*methods)
|
|
18
|
+
methods.each do |method|
|
|
19
|
+
method_basename, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
|
|
20
|
+
define_method :"#{method_basename}_backgrounded#{punctuation}" do |*args|
|
|
21
|
+
Backgrounded.handler.request(self, method, *args)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
include Backgrounded::Model::InstanceMethods
|
|
25
|
+
extend Backgrounded::Model::SingletonMethods
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
module SingletonMethods
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
module InstanceMethods
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
Object.send(:include, Backgrounded::Model)
|
data/lib/handler.rb
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
require 'activesupport'
|
|
2
|
+
|
|
3
|
+
module Backgrounded
|
|
4
|
+
module Handler
|
|
5
|
+
Dir["#{File.dirname(__FILE__)}/#{name.demodulize.underscore}/*.rb"].each do |handler_file|
|
|
6
|
+
handler_name = File.basename(handler_file, '.rb').camelize.to_sym
|
|
7
|
+
autoload handler_name, File.expand_path(handler_file)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require 'delayed_job'
|
|
2
|
+
|
|
3
|
+
module Backgrounded
|
|
4
|
+
module Handler
|
|
5
|
+
# invoke the operation in the background using delayed job
|
|
6
|
+
# see http://github.com/tobi/delayed_job/tree/master
|
|
7
|
+
class DelayedJobHandler
|
|
8
|
+
def request(object, method, *args)
|
|
9
|
+
object.send_later(method.to_sym, *args)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class User
|
|
4
|
+
backgrounded :do_stuff
|
|
5
|
+
|
|
6
|
+
def do_stuff
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class Person
|
|
11
|
+
backgrounded :do_stuff
|
|
12
|
+
|
|
13
|
+
def do_stuff(name, place, location)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class Post
|
|
18
|
+
backgrounded :do_stuff, :notify_users
|
|
19
|
+
|
|
20
|
+
def do_stuff
|
|
21
|
+
end
|
|
22
|
+
def notify_users
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
class Comment
|
|
27
|
+
backgrounded :delete_spam!
|
|
28
|
+
|
|
29
|
+
def delete_spam!
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class BackgroundedTest < Test::Unit::TestCase
|
|
34
|
+
context 'an object with a single backgrounded method' do
|
|
35
|
+
setup do
|
|
36
|
+
@user = User.new
|
|
37
|
+
end
|
|
38
|
+
should "execute method in background" do
|
|
39
|
+
@user.expects(:do_stuff)
|
|
40
|
+
@user.do_stuff_backgrounded
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
context 'an object with a backgrounded method that accepts parameters' do
|
|
45
|
+
setup do
|
|
46
|
+
@person = Person.new
|
|
47
|
+
end
|
|
48
|
+
should 'forward them' do
|
|
49
|
+
@person.expects(:do_stuff).with('ryan', 2, 3)
|
|
50
|
+
@person.do_stuff_backgrounded('ryan', 2, 3)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
context 'an object with a backgrounded method included punctuation' do
|
|
55
|
+
setup do
|
|
56
|
+
@comment = Comment.new
|
|
57
|
+
end
|
|
58
|
+
should 'move punctuation to the end of the new method' do
|
|
59
|
+
assert @comment.respond_to?(:'delete_spam_backgrounded!')
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
context 'an object with multiple backgrounded methods' do
|
|
64
|
+
setup do
|
|
65
|
+
@post = Post.new
|
|
66
|
+
end
|
|
67
|
+
should "execute method either method in background" do
|
|
68
|
+
@post.expects(:do_stuff)
|
|
69
|
+
@post.do_stuff_backgrounded
|
|
70
|
+
|
|
71
|
+
@post.expects(:notify_users)
|
|
72
|
+
@post.notify_users_backgrounded
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: backgrounded
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.3.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ryan Sonnek
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-10-26 00:00:00 -05:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description:
|
|
17
|
+
email: ryan.sonnek@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- LICENSE
|
|
24
|
+
- README.rdoc
|
|
25
|
+
files:
|
|
26
|
+
- .document
|
|
27
|
+
- .gitignore
|
|
28
|
+
- LICENSE
|
|
29
|
+
- README.rdoc
|
|
30
|
+
- Rakefile
|
|
31
|
+
- VERSION.yml
|
|
32
|
+
- backgrounded.gemspec
|
|
33
|
+
- lib/backgrounded.rb
|
|
34
|
+
- lib/handler.rb
|
|
35
|
+
- lib/handler/delayed_job_handler.rb
|
|
36
|
+
- lib/handler/inprocess_handler.rb
|
|
37
|
+
- test/backgrounded_test.rb
|
|
38
|
+
- test/test_helper.rb
|
|
39
|
+
has_rdoc: true
|
|
40
|
+
homepage: http://github.com/wireframe/backgrounded
|
|
41
|
+
licenses: []
|
|
42
|
+
|
|
43
|
+
post_install_message:
|
|
44
|
+
rdoc_options:
|
|
45
|
+
- --charset=UTF-8
|
|
46
|
+
require_paths:
|
|
47
|
+
- lib
|
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: "0"
|
|
53
|
+
version:
|
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: "0"
|
|
59
|
+
version:
|
|
60
|
+
requirements: []
|
|
61
|
+
|
|
62
|
+
rubyforge_project:
|
|
63
|
+
rubygems_version: 1.3.5
|
|
64
|
+
signing_key:
|
|
65
|
+
specification_version: 3
|
|
66
|
+
summary: Simple API to run Model methods in the background
|
|
67
|
+
test_files:
|
|
68
|
+
- test/backgrounded_test.rb
|
|
69
|
+
- test/test_helper.rb
|