ayl-rails 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -20,6 +20,28 @@ becomes
20
20
  ayl_after_create :method #,
21
21
  ayl_after_update :method
22
22
 
23
+ In addition, the callback definitions can be parameterized in a couple of different ways.
24
+
25
+ First, you can make the callbacks conditional in the same way that you can make standard ActiveRecord callbacks conditional:
26
+
27
+ ayl_after_create :method, :if => :should_it_be_called?
28
+
29
+ In this example, the method will only be invoked on create if the method "should_it_be_called?" responds with true.
30
+ Alternatively, something like:
31
+
32
+ ayl_after_create :method, :unless => :should_it_be_called?
33
+
34
+ In this example, the method will only be invoked on create if the method "should_it_be_called? responds with false.
35
+
36
+ You can also specify message parameters to control how the asynchronous message is processed. For example,
37
+
38
+ ayl_after_create :method, :message_options => { :delay => 20 }
39
+
40
+ will request that the underlying queuing engine will delay processing of the message for 20 seconds.
41
+
42
+ Naturally, the two types of parameters can be combined for a callback:
43
+
44
+ ayl_after_create :method, :message_options => { :delay => 20 }, :unless => :it_should_not_be_called?
23
45
 
24
46
  == Contributing to ayl-rails
25
47
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/ayl-rails.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ayl-rails"
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["j0hnds@gmail.com"]
12
- s.date = "2011-11-27"
12
+ s.date = "2011-11-30"
13
13
  s.description = "Partner to ayl to allow ActiveRecord after_* hooks to be made asynchronous."
14
14
  s.email = "j0hnds@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -31,20 +31,28 @@ module Ayl
31
31
  def add_ayl_hook(hook, *args, &block)
32
32
  if args && args.first.is_a?(Symbol)
33
33
  method = args.shift
34
- ayl_hooks(*args)[hook] << lambda{|o| o.send(method)}
34
+ ayl_hooks(hook, *args) << lambda{|o| o.send(method)}
35
35
  else
36
- ayl_hooks(*args)[hook] << block
36
+ ayl_hooks(hook, *args) << block
37
37
  end
38
38
  end
39
39
 
40
- def ayl_hooks(*args)
41
- @ayn_hooks ||= Hash.new do |hash, hook|
40
+ def ayl_hooks(hook_key, *args)
41
+ @ayl_hooks ||= Hash.new { |hash, hook| hash[hook] = [] }
42
+
43
+ message_options = {}
44
+ if args && args.first.is_a?(Hash) && args.first.has_key?(:message_options)
45
+ message_options = args.first[:message_options]
46
+ args.first.delete(:message_options)
47
+ end
48
+
49
+ if not @ayl_hooks.has_key?(hook_key)
42
50
  # Remember: this block is invoked only once for each
43
51
  # access of a key that has not been used before.
44
52
 
45
53
  # Define the name of a method that the standard hook
46
54
  # method will call when the standard hook fires
47
- ahook = "_ayl_#{hook}".to_sym
55
+ ahook = "_ayl_#{hook_key}".to_sym
48
56
 
49
57
  # This is for the producer's benefit
50
58
  # So, this is the equivalent of performing the following
@@ -58,24 +66,25 @@ module Ayl
58
66
  # So, the self.class target for the ayl_send is because we
59
67
  # need to call the ayl_send method at the singleton level.
60
68
  #
61
- send(hook, *args) { |o| self.class.ayl_send(ahook, o) }
69
+ send(hook_key, *args) { |o| self.class.ayl_send_opts(ahook, message_options, o) }
62
70
 
63
71
  # This is for the worker's benefit
64
72
  #
65
73
  # This defines the instance method
66
74
  method_code = <<-EOF
67
75
  def #{ahook}(o)
68
- _run_ayl_hooks(#{hook.inspect}, o)
76
+ _run_ayl_hooks(#{hook_key.inspect}, o)
69
77
  end
70
78
  EOF
71
79
  instance_eval(method_code, __FILE__, __LINE__ - 1)
72
-
73
- hash[hook] = []
74
80
  end
81
+
82
+ # Return the array associated with the hook key
83
+ @ayl_hooks[hook_key]
75
84
  end
76
85
 
77
86
  def _run_ayl_hooks(hook, o)
78
- ayl_hooks[hook].each { |b| b.call(o) }
87
+ ayl_hooks(hook).each { |b| b.call(o) }
79
88
  end
80
89
 
81
90
  end
@@ -60,6 +60,15 @@ class AfterCreate::MyModel < ActiveRecord::Base
60
60
  end
61
61
  end
62
62
 
63
+ module MessageOptions; end
64
+
65
+ class MessageOptions::MyModel < ActiveRecord::Base
66
+
67
+ ayl_after_create :handle_after_create, :message_options => { :delay => 20 }
68
+ ayl_after_update :handle_after_update
69
+
70
+ end
71
+
63
72
  module ConditionalCallbacks; end
64
73
 
65
74
  class ConditionalCallbacks::MyModel < ActiveRecord::Base
@@ -67,7 +76,7 @@ class ConditionalCallbacks::MyModel < ActiveRecord::Base
67
76
 
68
77
  ayl_after_create :handle_after_create, :if => :should_do_callback?
69
78
 
70
- ayl_after_update :handle_after_update, :if => :should_do_callback?
79
+ ayl_after_update :handle_after_update, :unless => :should_do_callback?
71
80
 
72
81
  private
73
82
 
@@ -153,7 +162,7 @@ describe "Rails Extensions" do
153
162
  it "should represent the instance of a particular model using a 'find'" do
154
163
  model = InstanceMethod::MyModel.create(:name => 'loud')
155
164
 
156
- model.to_rrepr.should == "InstanceMethod::MyModel.find(#{model.id})"
165
+ model.to_rrepr.should == "InstanceMethod::MyModel.unscoped.find(#{model.id})"
157
166
  end
158
167
 
159
168
  it "should invoke the instance method asynchronously with no options" do
@@ -192,21 +201,34 @@ describe "Rails Extensions" do
192
201
 
193
202
  context "when using conditional callbacks" do
194
203
 
195
- it "should invoke the after_create and after_update callbacks when the flag is true" do
204
+ it "should invoke the after_create but not the after_update callbacks when the flag is true" do
196
205
  model = ConditionalCallbacks::MyModel.new(:name => "spud")
197
206
  model.do_callback = true # Should allow after_create to be called, but not after_update
198
207
  model.save
199
208
  model.update_attribute(:name, "dog")
200
- WhatHappened.instance.what_ran.should == [ "handle after create", "handle after update" ]
209
+ WhatHappened.instance.what_ran.should == [ "handle after create" ]
201
210
  end
202
211
 
203
- it "should not invoke the after_create or after_update callbacks when the flag is false" do
212
+ it "should invoke the after_update but not the after_create callbacks when the flag is false" do
204
213
  model = ConditionalCallbacks::MyModel.new(:name => "spud")
205
214
  model.do_callback = false # Should allow after_update to be called, but not after_create
206
215
  model.save
207
216
  model.update_attribute(:name, "dog")
208
- WhatHappened.instance.what_ran.should be_blank
217
+ WhatHappened.instance.what_ran.should == [ "handle after update" ]
218
+ end
219
+ end
220
+
221
+ context "when using message parameters" do
222
+
223
+ it "should pass the message options to the ayl_after_create, but not to the ayl_after_update" do
224
+ model = MessageOptions::MyModel.new(:name => "spud")
225
+ MessageOptions::MyModel.should_receive(:ayl_send_opts).with(:_ayl_after_create, { :delay => 20 }, model)
226
+ MessageOptions::MyModel.should_receive(:ayl_send_opts).with(:_ayl_after_update, { }, model)
227
+
228
+ model.save
229
+ model.update_attribute(:name, "dog")
209
230
  end
231
+
210
232
  end
211
233
 
212
234
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ayl-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-27 00:00:00.000000000Z
12
+ date: 2011-11-30 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ayl
16
- requirement: &82467240 !ruby/object:Gem::Requirement
16
+ requirement: &79641030 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *82467240
24
+ version_requirements: *79641030
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rails
27
- requirement: &82466930 !ruby/object:Gem::Requirement
27
+ requirement: &79640730 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *82466930
35
+ version_requirements: *79640730
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &82466600 !ruby/object:Gem::Requirement
38
+ requirement: &79640380 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.3.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *82466600
46
+ version_requirements: *79640380
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &82466170 !ruby/object:Gem::Requirement
49
+ requirement: &79639970 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.0.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *82466170
57
+ version_requirements: *79639970
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: jeweler
60
- requirement: &82465650 !ruby/object:Gem::Requirement
60
+ requirement: &79639420 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.6.4
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *82465650
68
+ version_requirements: *79639420
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rcov
71
- requirement: &82465210 !ruby/object:Gem::Requirement
71
+ requirement: &79639010 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *82465210
79
+ version_requirements: *79639010
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: sqlite3
82
- requirement: &82434600 !ruby/object:Gem::Requirement
82
+ requirement: &79609090 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *82434600
90
+ version_requirements: *79609090
91
91
  description: Partner to ayl to allow ActiveRecord after_* hooks to be made asynchronous.
92
92
  email: j0hnds@gmail.com
93
93
  executables: []
@@ -126,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
126
  version: '0'
127
127
  segments:
128
128
  - 0
129
- hash: 812368029
129
+ hash: 1033823859
130
130
  required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  none: false
132
132
  requirements: