lazy_observers 0.0.2 → 0.0.3

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.2)
4
+ lazy_observers (0.0.3)
5
5
  activerecord
6
6
 
7
7
  GEM
data/Readme.md CHANGED
@@ -2,8 +2,6 @@ Make Activerecord Observers not load observed models -> faster/safer environment
2
2
  - faster tests + console
3
3
  - able to boot environment without/with empty/with old database
4
4
 
5
- Works on rails 2.3 + 3.0 (not yet 3.2)
6
-
7
5
  Install
8
6
  =======
9
7
 
@@ -39,10 +37,10 @@ Loads all classes registered via observers, to make sure you did not misspell</b
39
37
 
40
38
  LazyObservers.check_classes
41
39
 
42
-
43
- # TODO
44
- - Rails 3.0: does not work for models that are loaded before the observers
45
- - Rails 3.2: blows up
40
+ TIPS
41
+ ====
42
+ - do not use observe and lazy_observe in the same observer (and if you have to, use observe after lay_observe)
43
+ - do not use Model classes with class-methods of the observer e.g. class FooObserver; Foo.something{}; def after_update; end; end
46
44
 
47
45
  Author
48
46
  ======
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/lazy_observers
3
3
  specs:
4
- lazy_observers (0.0.0)
4
+ lazy_observers (0.0.2)
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.0)
4
+ lazy_observers (0.0.2)
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.0)
4
+ lazy_observers (0.0.2)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -7,21 +7,24 @@ module LazyObservers
7
7
  class_name = klass.name
8
8
  loaded << [klass, class_name]
9
9
  observers.each do |observer, observed|
10
- connect!(observer, klass) if observed.include?(class_name)
10
+ connect!(observer.instance, klass) if observed.include?(class_name)
11
11
  end
12
- (on_loads[class_name]||[]).each{|block| block.call(klass) }
12
+ (on_load_callbacks[class_name]||[]).each{|block| block.call(klass) }
13
13
  end
14
14
 
15
- def self.register_observer(observer, classes)
16
- observers[observer] = classes
15
+ def self.register_observer_instance(observer, classes)
17
16
  loaded.each do |klass, name|
18
17
  connect!(observer, klass) if classes.include?(name)
19
18
  end
20
19
  end
21
20
 
21
+ def self.register_observer_class(observer, classes)
22
+ observers[observer] = classes
23
+ end
24
+
22
25
  def self.on_load(class_name, &block)
23
- on_loads[class_name] ||= []
24
- on_loads[class_name] << block
26
+ on_load_callbacks[class_name] ||= []
27
+ on_load_callbacks[class_name] << block
25
28
  end
26
29
 
27
30
  # to check you did not specify a class that does not exist
@@ -35,7 +38,7 @@ module LazyObservers
35
38
 
36
39
  private
37
40
 
38
- def self.on_loads
41
+ def self.on_load_callbacks
39
42
  @on_loads ||= {}
40
43
  end
41
44
 
@@ -48,21 +51,12 @@ module LazyObservers
48
51
  end
49
52
 
50
53
  def self.connect!(observer, klass)
51
- observer.instance.observed_class_inherited(klass)
52
- end
53
- end
54
-
55
- ActiveRecord::Base.send(:subclasses).each{|klass| LazyObservers.register_observed(klass) }
56
-
57
- ActiveRecord::Observer.class_eval do
58
- def self.lazy_observe(*classes)
59
- raise "pass class names, not classes or symbols!" unless classes.all?{|klass| klass.is_a?(String) }
60
- define_method(:observed_classes) { Set.new }
61
- LazyObservers.register_observer self, classes
54
+ @connected ||= {}
55
+ return if @connected[[observer, klass]]
56
+ @connected[[observer, klass]] = true
57
+ observer.observed_class_inherited(klass)
62
58
  end
63
- end
64
59
 
65
- module LazyObservers
66
60
  module InheritedNotifier
67
61
  def inherited(subclass)
68
62
  LazyObservers.register_observed subclass
@@ -72,9 +66,9 @@ module LazyObservers
72
66
 
73
67
  module InheritedDebugger
74
68
  def inherited(subclass)
75
- @inherited_counter ||= 0
76
- @inherited_counter += 1
77
- puts "##{@inherited_counter} #{subclass}"
69
+ $lazy_observers_inherited_counter ||= 0
70
+ $lazy_observers_inherited_counter += 1
71
+ puts "##{$lazy_observers_inherited_counter} #{subclass}"
78
72
  puts caller
79
73
  puts "-" * 72
80
74
  super
@@ -82,4 +76,25 @@ module LazyObservers
82
76
  end
83
77
  end
84
78
 
79
+ descendants = (ActiveRecord::VERSION::MAJOR > 2 ? :descendants : :subclasses)
80
+ ActiveRecord::Base.send(descendants).each{|klass| LazyObservers.register_observed(klass) }
81
+
82
+ ActiveRecord::Observer.class_eval do
83
+ def self.lazy_observe(*classes)
84
+ raise "pass class names, not classes or symbols!" unless classes.all?{|klass| klass.is_a?(String) }
85
+ define_method(:observed_classes) { Set.new } # prevent default of PostObserver -> Post
86
+ LazyObservers.register_observer_class self, classes
87
+ define_method(:lazy_observed_classes) { Set.new(classes) }
88
+ end
89
+
90
+ # since AR uses respond_to? on the observer we need our observer to be fully defined before registering
91
+ alias_method :initialize_without_lazy, :initialize
92
+ def initialize
93
+ initialize_without_lazy
94
+ if defined?(lazy_observed_classes)
95
+ LazyObservers.register_observer_instance(self, lazy_observed_classes)
96
+ end
97
+ end
98
+ end
99
+
85
100
  ActiveRecord::Base.send(:extend, LazyObservers::InheritedNotifier)
@@ -1,3 +1,3 @@
1
1
  module LazyObservers
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -0,0 +1,19 @@
1
+ class NonLazyMovieObserver < ActiveRecord::Observer
2
+ observe Movie
3
+
4
+ def called
5
+ @called ||= []
6
+ end
7
+
8
+ def after_update(*args)
9
+ @called << [:after_update, args]
10
+ end
11
+
12
+ def after_save(*args)
13
+ @called << [:after_update, args]
14
+ end
15
+
16
+ def after_create(*args)
17
+ @called << [:after_create, args]
18
+ end
19
+ end
@@ -2,6 +2,7 @@ require "spec_helper"
2
2
  require File.expand_path("../app/user_observer", __FILE__)
3
3
  require File.expand_path("../app/post_observer", __FILE__)
4
4
  require File.expand_path("../app/product_observer", __FILE__)
5
+ require File.expand_path("../app/non_lazy_movie_observer", __FILE__)
5
6
 
6
7
  describe LazyObservers do
7
8
  it "has a VERSION" do
@@ -61,6 +62,12 @@ describe LazyObservers do
61
62
  }.to raise_error(/not classes or symbols/i)
62
63
  end
63
64
 
65
+ it "does not break normal observers" do
66
+ NonLazyMovieObserver.instance.called.clear # in case other specs ran first
67
+ movie = Movie.create!
68
+ NonLazyMovieObserver.instance.called.should == [[:after_create, [movie]], [:after_update, [movie]]]
69
+ end
70
+
64
71
  it "calls callback when matching class is loaded" do
65
72
  loaded = []
66
73
  LazyObservers.on_load("Group") do |klass|
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.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: 2012-07-07 00:00:00.000000000 Z
12
+ date: 2012-07-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -52,6 +52,7 @@ files:
52
52
  - spec/app/inherited_movie.rb
53
53
  - spec/app/movie.rb
54
54
  - spec/app/movie_observer.rb
55
+ - spec/app/non_lazy_movie_observer.rb
55
56
  - spec/app/post.rb
56
57
  - spec/app/post_observer.rb
57
58
  - spec/app/product_observer.rb
@@ -76,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
77
  version: '0'
77
78
  segments:
78
79
  - 0
79
- hash: -4605400004788605618
80
+ hash: 3011854085487653016
80
81
  required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  none: false
82
83
  requirements:
@@ -85,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
86
  version: '0'
86
87
  segments:
87
88
  - 0
88
- hash: -4605400004788605618
89
+ hash: 3011854085487653016
89
90
  requirements: []
90
91
  rubyforge_project:
91
92
  rubygems_version: 1.8.24