backgrounded 2.0.0 → 2.1.0

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/.rvmrc CHANGED
@@ -1,4 +1,4 @@
1
- rvm use 1.9.3@backgrounded --create
1
+ rvm use @backgrounded --create
2
2
  if ! command -v bundle ; then
3
3
  gem install bundler
4
4
  bundle install --local
@@ -28,7 +28,7 @@ Backgrounded provides a thin wrapper around any background processing framework
28
28
 
29
29
  = after_commit_backgrounded Callback
30
30
 
31
- Automatically execute a callback in the background after a model has been saved to the database. All of the standard after_commit options are
31
+ Automatically execute a callback in the background after a model has been saved to the database. All of the standard after_commit options are
32
32
  available as well as an optional :backgrounded option which will be passed to the Backgrounded::Handler.
33
33
 
34
34
  class User < ActiveRecord::Base
@@ -48,7 +48,7 @@ Bundler Gemfile configuration
48
48
 
49
49
  = Configuration
50
50
 
51
- Backgrounded packages handlers for popular frameworks in separate gems. Just drop in the gem for your particular framework or write your own!
51
+ Backgrounded handlers are available for popular libraries in separate gems. Just drop in the gem for your particular framework or write your own!
52
52
 
53
53
  == Resque
54
54
  see http://github.com/wireframe/backgrounded-resque
@@ -57,11 +57,16 @@ see http://github.com/wireframe/backgrounded-resque
57
57
  see http://github.com/jnstq/job_fu/tree
58
58
 
59
59
  == Custom Handlers
60
+ Writing a custom handler is as simple as:
60
61
 
61
62
  # config/initializers/backgrounded.rb
62
63
  class MyHandler
63
- def request(object, method, *args)
64
- #process the call however you want!
64
+ # @param object is the target object to invoke the method upon
65
+ # @param method is the requested method to call
66
+ # @param args is the optional list of arguments to pass to the method
67
+ # @param options is the optional hash of options passed to the backgrounded call
68
+ def request(object, method, args, options={})
69
+ # process the call however you want!
65
70
  end
66
71
  end
67
72
  Backgrounded.handler = MyHandler.new
@@ -20,6 +20,8 @@ Gem::Specification.new do |s|
20
20
  s.add_development_dependency(%q<mocha>, [">= 0"])
21
21
  s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.3.2"])
22
22
  s.add_development_dependency(%q<rake>, [">= 0.9.2.2"])
23
+ s.add_development_dependency(%q<pry>, [">= 0.9.12"])
24
+ s.add_development_dependency(%q<test_after_commit>, [">= 0"])
23
25
 
24
26
  s.files = `git ls-files`.split("\n")
25
27
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -9,8 +9,9 @@ module Backgrounded
9
9
  # @option options [Hash] :backgrounded (optional) options to pass into the backgrounded handler
10
10
  # @see after_commit
11
11
  def after_commit_backgrounded(method_name, options={})
12
- backgrounded_options = options.delete :backgrounded
13
- after_commit Proc.new {|o| o.backgrounded(backgrounded_options).send(method_name) }, options
12
+ self.after_commit options.except(:backgrounded) do |instance|
13
+ instance.backgrounded(options[:backgrounded]).send(method_name)
14
+ end
14
15
  end
15
16
  end
16
17
  end
@@ -7,15 +7,13 @@ module Backgrounded
7
7
  module ClassMethods
8
8
  # @see Backgrounded::Concern#backgrounded
9
9
  def backgrounded(options={})
10
- Backgrounded.handler.options = options
11
- Backgrounded::Proxy.new self
10
+ Backgrounded::Proxy.new self, options
12
11
  end
13
12
  end
14
13
 
15
14
  # @param options (optional) options to pass into the backgrounded handler
16
15
  def backgrounded(options={})
17
- Backgrounded.handler.options = options
18
- Backgrounded::Proxy.new self
16
+ Backgrounded::Proxy.new self, options
19
17
  end
20
18
  end
21
19
  end
@@ -1,10 +1,8 @@
1
- require 'backgrounded/handler/abstract_handler'
2
-
3
1
  module Backgrounded
4
2
  module Handler
5
3
  #simple handler to process synchronously and not actually in the background
6
4
  #useful for testing
7
- class InprocessHandler < AbstractHandler
5
+ class InprocessHandler
8
6
  def request(object, method, args, options={})
9
7
  object.send method, *args
10
8
  end
@@ -1,11 +1,9 @@
1
- require 'backgrounded/handler/abstract_handler'
2
-
3
1
  module Backgrounded
4
2
  module Handler
5
3
  # throw requests out the window
6
4
  # useful for test environment to avoid any background work
7
- class NoOpHandler < AbstractHandler
8
- def request(object, method, args)
5
+ class NoOpHandler
6
+ def request(object, method, args, options={})
9
7
  # do nothing
10
8
  end
11
9
  end
@@ -1,12 +1,14 @@
1
1
  module Backgrounded
2
+ attr_reader :delegate, :options
2
3
  class Proxy
3
- def initialize(delegate)
4
+ def initialize(delegate, options={})
4
5
  @delegate = delegate
6
+ @options = options || {}
5
7
  end
6
8
 
7
9
  def method_missing(method_name, *args)
8
- Backgrounded.logger.debug("Requesting #{Backgrounded.handler} backgrounded method: #{method_name} for instance #{@delegate}")
9
- Backgrounded.handler.request(@delegate, method_name, args)
10
+ Backgrounded.logger.debug("Requesting #{Backgrounded.handler} backgrounded method: #{method_name} for instance #{@delegate} with options: #{@options}")
11
+ Backgrounded.handler.request(@delegate, method_name, args, @options)
10
12
  nil
11
13
  end
12
14
  end
@@ -1,3 +1,3 @@
1
1
  module Backgrounded
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -17,21 +17,21 @@ class ActiveRecordExtensionTest < Test::Unit::TestCase
17
17
  should 'be defined on ActiveRecord::Base' do
18
18
  assert ActiveRecord::Base.respond_to?(:after_commit_backgrounded)
19
19
  end
20
- context 'when using default options' do
20
+ context 'without options' do
21
21
  setup do
22
22
  @blog = Blog.new
23
- @blog.expects(:do_something_else)
24
- @blog.save
23
+ Backgrounded.handler.expects(:request).with(@blog, :do_something_else, [], {})
24
+ @blog.save!
25
25
  end
26
- should 'execute callbacks' do end # see expectations
26
+ should 'invoke Backgrounded.handler with no options' do end # see expectations
27
27
  end
28
- context 'when callback has :backgrounded options' do
28
+ context 'with options[:backgrounded]' do
29
29
  setup do
30
- Backgrounded.handler.expects(:options=).with(:priority => :high)
31
30
  @user = User.new
32
- @user.save
31
+ Backgrounded.handler.expects(:request).with(@user, :do_stuff, [], {:priority => :high})
32
+ @user.save!
33
33
  end
34
- should 'pass configure Backgrounded.handler.options' do end # see expectations
34
+ should 'pass options[:backgrounded] to Backgrounded.handler' do end # see expectations
35
35
  end
36
36
  end
37
37
  end
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  class BackgroundedTest < Test::Unit::TestCase
4
4
 
5
- class User
5
+ class Dog
6
6
  def do_stuff
7
7
  end
8
8
  def self.do_something_else
@@ -11,14 +11,14 @@ class BackgroundedTest < Test::Unit::TestCase
11
11
 
12
12
  context '#backgrounded' do
13
13
  should 'be defined for class' do
14
- assert User.respond_to?(:backgrounded)
14
+ assert Dog.respond_to?(:backgrounded)
15
15
  end
16
16
  should 'be defined for instance' do
17
- assert User.new.respond_to?(:backgrounded)
17
+ assert Dog.new.respond_to?(:backgrounded)
18
18
  end
19
19
  context 'invoking on class' do
20
20
  setup do
21
- @result = User.backgrounded
21
+ @result = Dog.backgrounded
22
22
  end
23
23
  should 'return instance of Backgrounded::Proxy' do
24
24
  assert @result.is_a?(Backgrounded::Proxy)
@@ -26,8 +26,8 @@ class BackgroundedTest < Test::Unit::TestCase
26
26
  end
27
27
  context 'invoking on an instance' do
28
28
  setup do
29
- @user = User.new
30
- @result = @user.backgrounded
29
+ @dog = Dog.new
30
+ @result = @dog.backgrounded
31
31
  end
32
32
  should 'return instance of Backgrounded::Proxy' do
33
33
  assert @result.is_a?(Backgrounded::Proxy)
@@ -35,8 +35,9 @@ class BackgroundedTest < Test::Unit::TestCase
35
35
  end
36
36
  context 'invoking with options' do
37
37
  setup do
38
- Backgrounded.handler.expects(:options=).with(:priority => :high)
39
- @result = User.backgrounded(:priority => :high)
38
+ @dog = Dog.new
39
+ Backgrounded.handler.expects(:request).with(@dog, :do_stuff, [], {:priority => :high})
40
+ @dog.backgrounded(:priority => :high).do_stuff
40
41
  end
41
42
  should 'pass options onto Backgrounded.handler' do end # see expectations
42
43
  end
@@ -1,40 +1,51 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  class ProxyTest < Test::Unit::TestCase
4
-
5
- class User
6
- def do_stuff
7
- end
4
+ class Person
8
5
  def self.do_something_else
9
6
  end
10
- end
11
-
12
- class Person
13
7
  def do_stuff(name, place, location)
14
8
  end
9
+ def do_stuff_without_arguments
10
+ end
15
11
  end
16
12
 
17
13
  context 'invoking delegate method' do
14
+ context 'with arguments and options' do
15
+ setup do
16
+ @delegate = Person.new
17
+ Backgrounded.handler.expects(:request).with(@delegate, :do_stuff, ['foo', 1, 'bar'], {:option_one => 'alpha'})
18
+ @proxy = Backgrounded::Proxy.new @delegate, :option_one => 'alpha'
19
+ @result = @proxy.do_stuff 'foo', 1, 'bar'
20
+ end
21
+ should "invokes Backgrounded.handler with delegate, method and args" do end #see expectations
22
+ should 'return nil' do
23
+ assert_nil @result
24
+ end
25
+ end
18
26
  context 'with arguments' do
19
27
  setup do
20
28
  @delegate = Person.new
21
- @delegate.expects(:do_stuff).with('foo', 1, 'bar')
29
+ Backgrounded.handler.expects(:request).with(@delegate, :do_stuff, ['foo', 1, 'bar'], {})
22
30
  @proxy = Backgrounded::Proxy.new @delegate
23
31
  @result = @proxy.do_stuff 'foo', 1, 'bar'
24
32
  end
25
- should "execute method on delegate" do end #see expectations
33
+ should "invokes Backgrounded.handler with delegate, method and args" do end #see expectations
26
34
  should 'return nil' do
27
35
  assert_nil @result
28
36
  end
29
37
  end
30
38
  context 'with no arguments' do
31
39
  setup do
32
- @delegate = User.new
33
- @delegate.expects(:do_stuff)
40
+ @delegate = Person.new
41
+ Backgrounded.handler.expects(:request).with(@delegate, :do_stuff_without_arguments, [], {})
34
42
  @proxy = Backgrounded::Proxy.new @delegate
35
- @result = @proxy.do_stuff
43
+ @result = @proxy.do_stuff_without_arguments
44
+ end
45
+ should "invokes Backgrounded.handler with delegate, method and args" do end #see expectations
46
+ should 'return nil' do
47
+ assert_nil @result
36
48
  end
37
- should "execute method on delegate" do end #see expectations
38
49
  end
39
50
  end
40
51
  end
@@ -0,0 +1,13 @@
1
+ config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'database.yml')))
2
+ ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
3
+ ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
4
+
5
+ ActiveRecord::Schema.define(:version => 1) do
6
+ create_table :blogs, :force => true do |t|
7
+ t.column :title, :string
8
+ t.column :body, :string
9
+ end
10
+ create_table :users, :force => true do |t|
11
+ t.column :name, :string
12
+ end
13
+ end
@@ -9,18 +9,16 @@ rescue Bundler::BundlerError => e
9
9
  end
10
10
  require 'test/unit'
11
11
  require 'shoulda'
12
+ require 'pry'
12
13
  require 'mocha'
13
14
  require 'active_record'
14
15
 
15
16
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
17
  $LOAD_PATH.unshift(File.dirname(__FILE__))
17
18
  require 'backgrounded'
19
+ require 'setup_database'
18
20
 
19
21
  Backgrounded.logger.level = Logger::DEBUG
20
22
 
21
- config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'database.yml')))
22
- ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
23
- ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
24
-
25
23
  class Test::Unit::TestCase
26
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backgrounded
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-16 00:00:00.000000000 Z
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -107,6 +107,38 @@ dependencies:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
109
  version: 0.9.2.2
110
+ - !ruby/object:Gem::Dependency
111
+ name: pry
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 0.9.12
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 0.9.12
126
+ - !ruby/object:Gem::Dependency
127
+ name: test_after_commit
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
110
142
  description: Execute any ActiveRecord Model method in the background
111
143
  email:
112
144
  - ryan@codecrate.com
@@ -124,7 +156,6 @@ files:
124
156
  - lib/backgrounded.rb
125
157
  - lib/backgrounded/active_record_extension.rb
126
158
  - lib/backgrounded/concern.rb
127
- - lib/backgrounded/handler/abstract_handler.rb
128
159
  - lib/backgrounded/handler/inprocess_handler.rb
129
160
  - lib/backgrounded/handler/no_op_handler.rb
130
161
  - lib/backgrounded/proxy.rb
@@ -133,6 +164,7 @@ files:
133
164
  - test/backgrounded_test.rb
134
165
  - test/database.yml
135
166
  - test/proxy_test.rb
167
+ - test/setup_database.rb
136
168
  - test/test_helper.rb
137
169
  homepage: http://github.com/wireframe/backgrounded
138
170
  licenses: []
@@ -148,7 +180,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
180
  version: '0'
149
181
  segments:
150
182
  - 0
151
- hash: -1433093514713784314
183
+ hash: -3451700657100239997
152
184
  required_rubygems_version: !ruby/object:Gem::Requirement
153
185
  none: false
154
186
  requirements:
@@ -157,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
189
  version: '0'
158
190
  segments:
159
191
  - 0
160
- hash: -1433093514713784314
192
+ hash: -3451700657100239997
161
193
  requirements: []
162
194
  rubyforge_project: backgrounded
163
195
  rubygems_version: 1.8.24
@@ -169,4 +201,5 @@ test_files:
169
201
  - test/backgrounded_test.rb
170
202
  - test/database.yml
171
203
  - test/proxy_test.rb
204
+ - test/setup_database.rb
172
205
  - test/test_helper.rb
@@ -1,11 +0,0 @@
1
- module Backgrounded
2
- module Handler
3
- class AbstractHandler
4
- attr_writer :options
5
-
6
- def options
7
- @options ||= {}
8
- end
9
- end
10
- end
11
- end