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 CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lazy_observers (0.0.3)
4
+ lazy_observers (0.0.4)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/lazy_observers
3
3
  specs:
4
- lazy_observers (0.0.2)
4
+ lazy_observers (0.0.3)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/lazy_observers
3
3
  specs:
4
- lazy_observers (0.0.2)
4
+ lazy_observers (0.0.3)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/lazy_observers
3
3
  specs:
4
- lazy_observers (0.0.2)
4
+ lazy_observers (0.0.3)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -3,7 +3,7 @@ require 'active_record'
3
3
  require 'active_record/observer'
4
4
 
5
5
  module LazyObservers
6
- def self.register_observed(klass)
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.register_observer_instance(observer, classes)
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.register_observer_class(observer, classes)
22
- observers[observer] = classes
23
- end
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
- observer.observed_class_inherited(klass)
62
+ false
58
63
  end
59
64
 
60
65
  module InheritedNotifier
61
66
  def inherited(subclass)
62
- LazyObservers.register_observed subclass
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.register_observed(klass) }
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.register_observer_class self, classes
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.register_observer_instance(self, lazy_observed_classes)
100
+ LazyObservers.observer_ready(self, lazy_observed_classes)
96
101
  end
97
102
  end
98
103
  end
@@ -1,3 +1,3 @@
1
1
  module LazyObservers
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  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.3
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: 3011854085487653016
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: 3011854085487653016
89
+ hash: -521166920063735374
90
90
  requirements: []
91
91
  rubyforge_project:
92
92
  rubygems_version: 1.8.24