backgrounded 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 6
3
+ :minor: 7
4
4
  :build:
5
- :patch: 2
5
+ :patch: 0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{backgrounded}
8
- s.version = "0.6.2"
8
+ s.version = "0.7.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ryan Sonnek"]
12
- s.date = %q{2010-10-27}
12
+ s.date = %q{2010-10-30}
13
13
  s.email = %q{ryan.sonnek@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -6,8 +6,11 @@ module Backgrounded
6
6
  methods_with_options.merge! options
7
7
  methods_with_options.each_pair do |method, options|
8
8
  method_basename, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
9
- define_method "#{method_basename}_backgrounded#{punctuation}" do |*args|
10
- Backgrounded.handler.request(self, method, *args)
9
+ backgrounded_method = "#{method_basename}_backgrounded#{punctuation}"
10
+ class_eval do
11
+ define_method backgrounded_method do |*args|
12
+ Backgrounded.handler.request(self, method, *args)
13
+ end
11
14
  end
12
15
  end
13
16
  cattr_accessor :backgrounded_options
@@ -8,14 +8,29 @@ module Backgrounded
8
8
  @@queue = DEFAULT_QUEUE
9
9
 
10
10
  def request(object, method, *args)
11
- @@queue = object.backgrounded_options[method.to_sym][:queue] || DEFAULT_QUEUE
12
- Resque.enqueue(ResqueHandler, object.class.name, object.id, method, *args)
11
+ options = object.backgrounded_options[method.to_sym]
12
+ @@queue = options[:queue] || DEFAULT_QUEUE
13
+ instance, id = instance_identifiers(object)
14
+ Resque.enqueue(ResqueHandler, instance, id, method, *args)
13
15
  end
14
16
  def self.queue
15
17
  @@queue
16
18
  end
17
19
  def self.perform(clazz, id, method, *args)
18
- clazz.constantize.find(id).send(method, *args)
20
+ find_instance(clazz, id, method).send(method, *args)
21
+ end
22
+
23
+ private
24
+ def self.find_instance(clazz, id, method)
25
+ clazz = clazz.constantize
26
+ clazz.respond_to?(method) ? clazz : clazz.find(id)
27
+ end
28
+ def instance_identifiers(object)
29
+ instance, id = if object.is_a?(Class)
30
+ [object.name, -1]
31
+ else
32
+ [object.class.name, object.id]
33
+ end
19
34
  end
20
35
  end
21
36
  end
@@ -1,17 +1,33 @@
1
1
  class Backgrounded::Handler::WorklingHandler
2
+ class BackgroundedWorker < Workling::Base
3
+ def perform(options = {})
4
+ find_instance(options[:class], options[:id], options[:method]).send(options[:method], *options[:params])
5
+ end
6
+
7
+ private
8
+ def find_instance(clazz, id, method)
9
+ clazz = clazz.constantize
10
+ clazz.respond_to?(method) ? clazz : clazz.find(id)
11
+ end
12
+ end
13
+
2
14
  def request(object, method, *args)
15
+ instance, id = instance_identifiers(object)
3
16
  options = {
4
- :class => object.class.name,
5
- :id => object.id,
17
+ :class => instance,
18
+ :id => id,
6
19
  :method => method,
7
20
  :params => args
8
21
  }
9
22
  BackgroundedWorker.async_perform options
10
23
  end
11
24
 
12
- class BackgroundedWorker < Workling::Base
13
- def perform(options = {})
14
- options[:class].constantize.find(options[:id]).send(options[:method], *options[:params])
25
+ private
26
+ def instance_identifiers(object)
27
+ instance, id = if object.is_a?(Class)
28
+ [object.name, -1]
29
+ else
30
+ [object.class.name, object.id]
15
31
  end
16
32
  end
17
33
  end
@@ -26,6 +26,15 @@ class ResqueHandlerTest < Test::Unit::TestCase
26
26
  end
27
27
  end
28
28
 
29
+ class Blog < ActiveRecord::Base
30
+ class << self
31
+ backgrounded :do_stuff
32
+
33
+ def do_stuff
34
+ end
35
+ end
36
+ end
37
+
29
38
  context 'when backgrounded is configured with resque' do
30
39
  setup do
31
40
  Resque.reset!
@@ -33,6 +42,25 @@ class ResqueHandlerTest < Test::Unit::TestCase
33
42
  Backgrounded.handler = @handler
34
43
  end
35
44
 
45
+ context 'a class level backgrounded method' do
46
+ context "invoking backgrounded method" do
47
+ setup do
48
+ Blog.do_stuff_backgrounded
49
+ end
50
+ should "enqueue job to resque" do
51
+ assert_queued Backgrounded::Handler::ResqueHandler
52
+ assert_equal Backgrounded::Handler::ResqueHandler::DEFAULT_QUEUE, Resque.queue_from_class(Backgrounded::Handler::ResqueHandler)
53
+ end
54
+ context "running background job" do
55
+ setup do
56
+ Blog.expects(:do_stuff)
57
+ Resque.run!
58
+ end
59
+ should "invoke method on class" do end #see expectations
60
+ end
61
+ end
62
+ end
63
+
36
64
  context 'a persisted object with a single backgrounded method' do
37
65
  setup do
38
66
  @user = User.create
@@ -1,5 +1,5 @@
1
1
  require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
- RAILS_DEFAULT_LOGGER = Logger.new 'foo'
2
+ RAILS_DEFAULT_LOGGER = Logger.new STDOUT
3
3
  RAILS_ENV = 'test'
4
4
  require 'newrelic_rpm'
5
5
  require 'memcache'
@@ -26,6 +26,15 @@ class WorklingHandlerTest < Test::Unit::TestCase
26
26
  end
27
27
  end
28
28
 
29
+ class Post < ActiveRecord::Base
30
+ class << self
31
+ backgrounded :do_stuff
32
+
33
+ def do_stuff
34
+ end
35
+ end
36
+ end
37
+
29
38
  context 'when backgrounded is configured with workling' do
30
39
  setup do
31
40
  @handler = Backgrounded::Handler::WorklingHandler.new
@@ -44,5 +53,15 @@ class WorklingHandlerTest < Test::Unit::TestCase
44
53
  should 'dispatch through workling back to the object' do end #see expectations
45
54
  end
46
55
  end
56
+
57
+ context 'a class level backgrounded method' do
58
+ context "invoking backgrounded method" do
59
+ setup do
60
+ Post.expects(:do_stuff).with('a string')
61
+ Post.do_stuff_backgrounded 'a string'
62
+ end
63
+ should 'dispatch through workling back to the object' do end #see expectations
64
+ end
65
+ end
47
66
  end
48
67
  end
@@ -49,14 +49,30 @@ class BackgroundedTest < Test::Unit::TestCase
49
49
  end
50
50
  end
51
51
 
52
+ class Blog
53
+ class << self
54
+ backgrounded :update_info
55
+ def update_info
56
+ end
57
+ end
58
+ end
52
59
 
53
60
  context 'an object with a single backgrounded method' do
54
61
  setup do
55
62
  @user = User.new
56
63
  end
57
- should "execute method in background" do
58
- @user.expects(:do_stuff)
59
- @user.do_stuff_backgrounded
64
+ should 'define backgrounded method' do
65
+ assert @user.respond_to?('do_stuff_backgrounded')
66
+ end
67
+ should 'save backgrounded options for method' do
68
+ assert_not_nil User.backgrounded_options[:do_stuff]
69
+ end
70
+ context 'executing backgrounded method' do
71
+ setup do
72
+ @user.expects(:do_stuff)
73
+ @user.do_stuff_backgrounded
74
+ end
75
+ should "execute method in background" do end #see expectations
60
76
  end
61
77
  end
62
78
 
@@ -110,4 +126,20 @@ class BackgroundedTest < Test::Unit::TestCase
110
126
  assert_equal :low, @dog.backgrounded_options[:bark][:priority]
111
127
  end
112
128
  end
129
+
130
+ context 'a class with backgrounded method' do
131
+ should 'define backgrounded method' do
132
+ assert Blog.respond_to?('update_info_backgrounded')
133
+ end
134
+ should 'save backgrounded options for method' do
135
+ assert_not_nil Blog.backgrounded_options[:update_info]
136
+ end
137
+ context 'invoking backgrounded method' do
138
+ setup do
139
+ Blog.expects(:update_info)
140
+ Blog.update_info_backgrounded
141
+ end
142
+ should 'invoke class method' do end #see expectations
143
+ end
144
+ end
113
145
  end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 2
10
- version: 0.6.2
8
+ - 7
9
+ - 0
10
+ version: 0.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Sonnek
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-27 00:00:00 -05:00
18
+ date: 2010-10-30 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency