resque_def 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/Gemfile +3 -0
- data/README.md +100 -0
- data/Rakefile +14 -0
- data/lib/resque_def.rb +46 -0
- data/lib/resque_def/version.rb +3 -0
- data/resque_def.gemspec +25 -0
- data/test/resque_def_test.rb +21 -0
- data/test/test_helper.rb +41 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e97978c1e1e7b7361fc20e62cdad2791d0fc205b
|
4
|
+
data.tar.gz: 4b134ee7cc1316f0052076bb17af5376751798fd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fc5c45d2c0177dd2b82d2926b6197c2d3efdafcbd3c4395a7b0d42867ae510e92cb96dbcc8175933f557271ac2c8d1c40ab35d50fd295d1566719744f573e74d
|
7
|
+
data.tar.gz: d302af7e191e301010cf95060cda342f69011d884f67e3ea4854fd73472fcad09329fad0acdc268805d39061229528fa1adb995de2fe4419f2323c6e82ba7b98
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
## ResqueDef
|
2
|
+
|
3
|
+
Forget resque boilerplate: focus on your code.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
In your `Gemfile` add:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'resque_def'
|
11
|
+
```
|
12
|
+
|
13
|
+
Then run `$ bundle install`
|
14
|
+
|
15
|
+
## What
|
16
|
+
|
17
|
+
I love Resque, but I'm not in love with the amount of code needed to define and use a Resque task to do something like pull a user from a database, find open issues (assuming an issue model) and sending an email might look like this:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class User < ActiveRecord::Base
|
21
|
+
|
22
|
+
class DelaySendIssues
|
23
|
+
@queue = :delay_send_issues
|
24
|
+
|
25
|
+
def self.perform(id, state)
|
26
|
+
user = User.find(id)
|
27
|
+
issues = user.issues.where(state: state).all
|
28
|
+
UserMailer.send_issues(user: user, issues: issues).deliver
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
user = User.last
|
34
|
+
Resque.enqueue(User::SendDailyTriageEmail, user.id, 'open')
|
35
|
+
```
|
36
|
+
|
37
|
+
With ResqueDef, you can include a module, and define and use a Resque job like this:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class User < ActiveRecord::Base
|
41
|
+
include ResqueDef
|
42
|
+
|
43
|
+
resque_def(:delay_send_issues) do |id, state|
|
44
|
+
user = User.find(id)
|
45
|
+
issues = user.issues.where(state: state).all
|
46
|
+
UserMailer.send_issues(user: user, issues: issues).deliver
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
user = User.last
|
51
|
+
User.delay_send_issues(user.id, 'open')
|
52
|
+
```
|
53
|
+
|
54
|
+
So looking at the boiler plate (and none of the logic) we are reducing this:
|
55
|
+
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class DelaySendIssues
|
59
|
+
@queue = :delay_send_issues
|
60
|
+
|
61
|
+
def self.perform(id, state)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
To this:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
resque_def(:delay_send_issues) do |id, state|
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
Pretty cool, huh? To do this, ResqueDef uses simple metaprogramming: `Class.new` is used to define the require class, and then `define_singleton_method` adds our method to the class included `ResqueDef`. Not doing anything too magical. Check out the code for yourself!
|
74
|
+
|
75
|
+
|
76
|
+
## Serializing Objects
|
77
|
+
|
78
|
+
By default Resque can only store JSON-able objects (strings, arrays, hashes, booleans). It cannot store active record objects.
|
79
|
+
|
80
|
+
## TIL with Blocks
|
81
|
+
|
82
|
+
You can set default values in blocks like this:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
resque_def(:foo) do |id, state = 'open', username = 'schneems'|
|
86
|
+
# ...
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
You can have an unlimited amount of args using a splat:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
resque_def(:foo) do |id, *args|
|
94
|
+
# ...
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
|
4
|
+
require 'rake'
|
5
|
+
require 'rake/testtask'
|
6
|
+
|
7
|
+
task :default => [:test]
|
8
|
+
|
9
|
+
test_task = Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = false
|
14
|
+
end
|
data/lib/resque_def.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module ResqueDef
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def resque_def(resque_name, &block)
|
8
|
+
# convert resque_name like :delay_send_issues to DelaySendIssues for klass name
|
9
|
+
resque_klass_name = resque_name.to_s.capitalize.gsub(/_\S/) {|m| m.upcase}.gsub('_', '')
|
10
|
+
|
11
|
+
# create the resque klass
|
12
|
+
resque_klass = Class.new do
|
13
|
+
@queue = resque_name
|
14
|
+
|
15
|
+
def self.perform(*args)
|
16
|
+
@job.call(*args)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.job=(job)
|
20
|
+
@job = job
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.queue=(queue)
|
24
|
+
@queue = queue
|
25
|
+
end
|
26
|
+
end
|
27
|
+
resque_klass.job = block
|
28
|
+
resque_klass.queue = name
|
29
|
+
|
30
|
+
# assign the object to a constant we can look up later
|
31
|
+
resque_klass = self.const_set(resque_klass_name, resque_klass)
|
32
|
+
|
33
|
+
# create the class method to enqueue the resque job
|
34
|
+
define_singleton_method(resque_name) do |*args|
|
35
|
+
Resque.enqueue(resque_klass, *args)
|
36
|
+
end
|
37
|
+
|
38
|
+
# helper instance method, calls the class method
|
39
|
+
define_method(resque_name) do |*args|
|
40
|
+
self.class.send(resque_name, *args)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
require 'resque_def/version'
|
data/resque_def.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'resque_def/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "resque_def"
|
8
|
+
gem.version = ResqueDef::VERSION
|
9
|
+
gem.authors = ["Richard Schneeman"]
|
10
|
+
gem.email = ["richard.schneeman+rubygems@gmail.com"]
|
11
|
+
gem.description = %q{ Defining Resque boilerplate since 2013 }
|
12
|
+
gem.summary = %q{ Defining Resque boilerplate since 2013 }
|
13
|
+
gem.homepage = "https://github.com/heroku/resque_def"
|
14
|
+
gem.license = "MIT"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_dependency "resque"
|
22
|
+
gem.add_development_dependency "rake"
|
23
|
+
gem.add_development_dependency "mocha"
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ResqueDefTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_enqueue
|
9
|
+
Resque.expects(:enqueue).with(User::Foo, 1,2,3).once
|
10
|
+
Resque.expects(:enqueue).with(User::Foo, 4,5,6).once
|
11
|
+
|
12
|
+
User.foo(1,2,3)
|
13
|
+
User.new.foo(4,5,6)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_dequeue
|
17
|
+
User.expects(:bar).with("a", "b", "c").twice
|
18
|
+
User.bar("a", "b", "c")
|
19
|
+
User.new.bar("a", "b", "c")
|
20
|
+
end
|
21
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
Bundler.require
|
2
|
+
|
3
|
+
require 'resque'
|
4
|
+
require 'resque_def'
|
5
|
+
require 'test/unit'
|
6
|
+
require "mocha/setup"
|
7
|
+
|
8
|
+
|
9
|
+
Resque.inline = true # for testing, tasks are called automatically
|
10
|
+
|
11
|
+
|
12
|
+
class User
|
13
|
+
include ResqueDef
|
14
|
+
|
15
|
+
def self.find(id)
|
16
|
+
self.new
|
17
|
+
end
|
18
|
+
|
19
|
+
resque_def :foo do
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.bar(*args)
|
23
|
+
end
|
24
|
+
|
25
|
+
resque_def :bar do |*args|
|
26
|
+
User.bar(*args)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
# make sure the namespace works correctly Foo::Bar and User::Bar
|
32
|
+
class Foo
|
33
|
+
include ResqueDef
|
34
|
+
|
35
|
+
def self.bar(*args)
|
36
|
+
end
|
37
|
+
|
38
|
+
resque_def :bar do |*args|
|
39
|
+
Foo.bar(*args)
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resque_def
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Richard Schneeman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: resque
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mocha
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: ' Defining Resque boilerplate since 2013 '
|
56
|
+
email:
|
57
|
+
- richard.schneeman+rubygems@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- Gemfile.lock
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/resque_def.rb
|
68
|
+
- lib/resque_def/version.rb
|
69
|
+
- resque_def.gemspec
|
70
|
+
- test/resque_def_test.rb
|
71
|
+
- test/test_helper.rb
|
72
|
+
homepage: https://github.com/heroku/resque_def
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.0.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Defining Resque boilerplate since 2013
|
96
|
+
test_files:
|
97
|
+
- test/resque_def_test.rb
|
98
|
+
- test/test_helper.rb
|