live_resource-activerecord 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,15 +30,34 @@ module LiveResource
30
30
  end
31
31
 
32
32
  def observe(event)
33
- dependency = self
34
- model_class.class_eval do
35
- send(event, dependency)
33
+ # Declare variables in method scope so they're preserved in the block
34
+ dependency = self
35
+ subscribed_events = @events
36
+ target = @model_class
37
+ class_name = "#{@resource.name}_dependency_on_#{@model_class}_for_#{@events.join('_')}"
38
+
39
+ # Construct a new observer class
40
+ dependency_observer = Class.new(::ActiveRecord::Observer) do
41
+ observe target
42
+
43
+ # Give it a class name otherwise ActiveRecord will explode
44
+ @_class_name = class_name
45
+ class << self
46
+ def name
47
+ @_class_name
48
+ end
49
+ end
50
+
51
+ # Create callback methods on the observer class for each subscribed event
52
+ subscribed_events.each do |event_name|
53
+ define_method(event_name) do |*args|
54
+ dependency.invoke(args[0], event_name)
55
+ end
56
+ end
36
57
  end
37
- end
38
58
 
39
- ::ActiveRecord::Callbacks::CALLBACKS.each do |callback|
40
- event = callback
41
- define_method(callback) { |record| invoke(record, event) }
59
+ # Instantiating the observer class will make it hook itself up
60
+ dependency_observer.send :new
42
61
  end
43
62
  end
44
63
 
@@ -1,5 +1,5 @@
1
1
  module LiveResource
2
2
  module ActiveRecord
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -18,19 +18,6 @@ describe Dependency do
18
18
  let(:events) { nil }
19
19
  let(:proc) { double(Proc) }
20
20
 
21
- ActiveRecord::Callbacks::CALLBACKS.each do |callback|
22
- context "##{callback}" do
23
- subject { dependency.send(callback.to_sym, record) }
24
-
25
- let(:record) { model_class.new }
26
-
27
- it "should call #invoke with the ActiveRecord instance and the callback name" do
28
- dependency.should_receive(:invoke).with(record, callback)
29
- subject
30
- end
31
- end
32
- end
33
-
34
21
  it "should pass the target to the superclass" do
35
22
  expect(dependency.target).to be model_class
36
23
  end
@@ -84,19 +71,4 @@ describe Dependency do
84
71
  end
85
72
  end
86
73
  end
87
-
88
- describe "#observe" do
89
- subject { dependency.observe(event) }
90
-
91
- let(:event) { :after_create }
92
-
93
- before do
94
- model_class.stub(event)
95
- end
96
-
97
- it "should register itself for callbacks" do
98
- model_class.should_receive(event).with(dependency)
99
- subject
100
- end
101
- end
102
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: live_resource-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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: 2013-05-16 00:00:00.000000000 Z
12
+ date: 2013-05-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -161,7 +161,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
161
161
  version: '0'
162
162
  segments:
163
163
  - 0
164
- hash: -3578353206421120448
164
+ hash: -16503447049522901
165
165
  required_rubygems_version: !ruby/object:Gem::Requirement
166
166
  none: false
167
167
  requirements:
@@ -170,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
170
  version: '0'
171
171
  segments:
172
172
  - 0
173
- hash: -3578353206421120448
173
+ hash: -16503447049522901
174
174
  requirements: []
175
175
  rubyforge_project:
176
176
  rubygems_version: 1.8.25