lazy_observers 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/gemfiles/rails-2.3.gemfile.lock +1 -1
- data/gemfiles/rails-3.0.gemfile.lock +1 -1
- data/gemfiles/rails-3.2.gemfile.lock +1 -1
- data/lib/lazy_observers.rb +20 -15
- data/lib/lazy_observers/version.rb +1 -1
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/lib/lazy_observers.rb
CHANGED
@@ -3,7 +3,7 @@ require 'active_record'
|
|
3
3
|
require 'active_record/observer'
|
4
4
|
|
5
5
|
module LazyObservers
|
6
|
-
def self.
|
6
|
+
def self.observed_loaded(klass)
|
7
7
|
class_name = klass.name
|
8
8
|
loaded << [klass, class_name]
|
9
9
|
observers.each do |observer, observed|
|
@@ -12,19 +12,19 @@ module LazyObservers
|
|
12
12
|
(on_load_callbacks[class_name]||[]).each{|block| block.call(klass) }
|
13
13
|
end
|
14
14
|
|
15
|
-
def self.
|
15
|
+
def self.observer_loaded(observer, classes)
|
16
|
+
observers[observer] = classes
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.observer_ready(observer, classes)
|
16
20
|
loaded.each do |klass, name|
|
17
21
|
connect!(observer, klass) if classes.include?(name)
|
18
22
|
end
|
19
23
|
end
|
20
24
|
|
21
|
-
def self.
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
def self.on_load(class_name, &block)
|
26
|
-
on_load_callbacks[class_name] ||= []
|
27
|
-
on_load_callbacks[class_name] << block
|
25
|
+
def self.on_load(observed, &block)
|
26
|
+
on_load_callbacks[observed] ||= []
|
27
|
+
on_load_callbacks[observed] << block
|
28
28
|
end
|
29
29
|
|
30
30
|
# to check you did not specify a class that does not exist
|
@@ -51,15 +51,20 @@ module LazyObservers
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def self.connect!(observer, klass)
|
54
|
+
return if connected?(observer, klass)
|
55
|
+
observer.observed_class_inherited(klass)
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.connected?(observer, klass)
|
54
59
|
@connected ||= {}
|
55
|
-
return if @connected[[observer, klass]]
|
60
|
+
return true if @connected[[observer, klass]]
|
56
61
|
@connected[[observer, klass]] = true
|
57
|
-
|
62
|
+
false
|
58
63
|
end
|
59
64
|
|
60
65
|
module InheritedNotifier
|
61
66
|
def inherited(subclass)
|
62
|
-
LazyObservers.
|
67
|
+
LazyObservers.observed_loaded subclass
|
63
68
|
super
|
64
69
|
end
|
65
70
|
end
|
@@ -77,13 +82,13 @@ module LazyObservers
|
|
77
82
|
end
|
78
83
|
|
79
84
|
descendants = (ActiveRecord::VERSION::MAJOR > 2 ? :descendants : :subclasses)
|
80
|
-
ActiveRecord::Base.send(descendants).each{|klass| LazyObservers.
|
85
|
+
ActiveRecord::Base.send(descendants).each{|klass| LazyObservers.observed_loaded(klass) }
|
81
86
|
|
82
87
|
ActiveRecord::Observer.class_eval do
|
83
88
|
def self.lazy_observe(*classes)
|
84
89
|
raise "pass class names, not classes or symbols!" unless classes.all?{|klass| klass.is_a?(String) }
|
85
90
|
define_method(:observed_classes) { Set.new } # prevent default of PostObserver -> Post
|
86
|
-
LazyObservers.
|
91
|
+
LazyObservers.observer_loaded self, classes
|
87
92
|
define_method(:lazy_observed_classes) { Set.new(classes) }
|
88
93
|
end
|
89
94
|
|
@@ -92,7 +97,7 @@ ActiveRecord::Observer.class_eval do
|
|
92
97
|
def initialize
|
93
98
|
initialize_without_lazy
|
94
99
|
if defined?(lazy_observed_classes)
|
95
|
-
LazyObservers.
|
100
|
+
LazyObservers.observer_ready(self, lazy_observed_classes)
|
96
101
|
end
|
97
102
|
end
|
98
103
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_observers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -77,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
77
|
version: '0'
|
78
78
|
segments:
|
79
79
|
- 0
|
80
|
-
hash:
|
80
|
+
hash: -521166920063735374
|
81
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
82
|
none: false
|
83
83
|
requirements:
|
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
86
|
version: '0'
|
87
87
|
segments:
|
88
88
|
- 0
|
89
|
-
hash:
|
89
|
+
hash: -521166920063735374
|
90
90
|
requirements: []
|
91
91
|
rubyforge_project:
|
92
92
|
rubygems_version: 1.8.24
|