lazy_observers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -0
- data/Appraisals +12 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +49 -0
- data/Rakefile +27 -0
- data/Readme.md +52 -0
- data/gemfiles/rails-2.3.gemfile +11 -0
- data/gemfiles/rails-2.3.gemfile.lock +37 -0
- data/gemfiles/rails-3.0.gemfile +11 -0
- data/gemfiles/rails-3.0.gemfile.lock +48 -0
- data/gemfiles/rails-3.2.gemfile +11 -0
- data/gemfiles/rails-3.2.gemfile.lock +50 -0
- data/lazy_observers.gemspec +13 -0
- data/lib/lazy_observers.rb +83 -0
- data/lib/lazy_observers/version.rb +3 -0
- data/spec/app/groups.rb +5 -0
- data/spec/app/inherited_movie.rb +2 -0
- data/spec/app/movie.rb +2 -0
- data/spec/app/movie_observer.rb +19 -0
- data/spec/app/post.rb +2 -0
- data/spec/app/post_observer.rb +19 -0
- data/spec/app/product_observer.rb +19 -0
- data/spec/app/products.rb +5 -0
- data/spec/app/stupid_observer.rb +3 -0
- data/spec/app/user_observer.rb +3 -0
- data/spec/database.rb +29 -0
- data/spec/lazy_observers_spec.rb +72 -0
- data/spec/spec_helper.rb +10 -0
- metadata +96 -0
data/.travis.yml
ADDED
data/Appraisals
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
appraise "rails-2.3" do
|
2
|
+
gem "activerecord", "~> 2.3.6", :require => "active_record"
|
3
|
+
end
|
4
|
+
|
5
|
+
appraise "rails-3.0" do
|
6
|
+
gem "activerecord", "~> 3.0.15", :require => "active_record"
|
7
|
+
end
|
8
|
+
|
9
|
+
appraise "rails-3.2" do
|
10
|
+
gem "activerecord", "~> 3.2.6", :require => "active_record"
|
11
|
+
end
|
12
|
+
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
lazy_observers (0.0.1)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.2.6)
|
11
|
+
activesupport (= 3.2.6)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
activerecord (3.2.6)
|
14
|
+
activemodel (= 3.2.6)
|
15
|
+
activesupport (= 3.2.6)
|
16
|
+
arel (~> 3.0.2)
|
17
|
+
tzinfo (~> 0.3.29)
|
18
|
+
activesupport (3.2.6)
|
19
|
+
i18n (~> 0.6)
|
20
|
+
multi_json (~> 1.0)
|
21
|
+
appraisal (0.4.1)
|
22
|
+
bundler
|
23
|
+
rake
|
24
|
+
arel (3.0.2)
|
25
|
+
builder (3.0.0)
|
26
|
+
diff-lcs (1.1.3)
|
27
|
+
i18n (0.6.0)
|
28
|
+
multi_json (1.3.6)
|
29
|
+
rake (0.9.2)
|
30
|
+
rspec (2.6.0)
|
31
|
+
rspec-core (~> 2.6.0)
|
32
|
+
rspec-expectations (~> 2.6.0)
|
33
|
+
rspec-mocks (~> 2.6.0)
|
34
|
+
rspec-core (2.6.4)
|
35
|
+
rspec-expectations (2.6.0)
|
36
|
+
diff-lcs (~> 1.1.2)
|
37
|
+
rspec-mocks (2.6.0)
|
38
|
+
sqlite3 (1.3.6)
|
39
|
+
tzinfo (0.3.33)
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
ruby
|
43
|
+
|
44
|
+
DEPENDENCIES
|
45
|
+
appraisal
|
46
|
+
lazy_observers!
|
47
|
+
rake
|
48
|
+
rspec (~> 2)
|
49
|
+
sqlite3
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'appraisal'
|
3
|
+
|
4
|
+
task :spec do
|
5
|
+
sh "rspec spec/"
|
6
|
+
end
|
7
|
+
|
8
|
+
# extracted from https://github.com/grosser/project_template
|
9
|
+
rule /^version:bump:.*/ do |t|
|
10
|
+
sh "git status | grep 'nothing to commit'" # ensure we are not dirty
|
11
|
+
index = ['major', 'minor','patch'].index(t.name.split(':').last)
|
12
|
+
file = 'lib/lazy_observers/version.rb'
|
13
|
+
|
14
|
+
version_file = File.read(file)
|
15
|
+
old_version, *version_parts = version_file.match(/(\d+)\.(\d+)\.(\d+)/).to_a
|
16
|
+
version_parts[index] = version_parts[index].to_i + 1
|
17
|
+
version_parts[2] = 0 if index < 2 # remove patch for minor
|
18
|
+
version_parts[1] = 0 if index < 1 # remove minor for major
|
19
|
+
new_version = version_parts * '.'
|
20
|
+
File.open(file,'w'){|f| f.write(version_file.sub(old_version, new_version)) }
|
21
|
+
|
22
|
+
sh "bundle && git add #{file} Gemfile.lock && git commit -m 'bump version to #{new_version}'"
|
23
|
+
end
|
24
|
+
|
25
|
+
task :default do
|
26
|
+
sh "bundle exec rake appraisal:install && bundle exec rake appraisal spec"
|
27
|
+
end
|
data/Readme.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
Make Activerecord Observers not load observed models -> faster/safer environment boot.
|
2
|
+
- faster tests + console
|
3
|
+
- able to boot environment without/with empty/with old database
|
4
|
+
|
5
|
+
Works on rails 2.3 + 3.0 (not 3.2)
|
6
|
+
|
7
|
+
Install
|
8
|
+
=======
|
9
|
+
|
10
|
+
gem install lazy_observers
|
11
|
+
|
12
|
+
Usage
|
13
|
+
=====
|
14
|
+
|
15
|
+
class FooObserver < ActiveRecord::Observer
|
16
|
+
lazy_observe "User", "Foo::Bar"
|
17
|
+
|
18
|
+
...
|
19
|
+
end
|
20
|
+
|
21
|
+
### Extend models from gems after they are loaded
|
22
|
+
|
23
|
+
LazyObservers.on_load("Arturo::Feature") do |klass|
|
24
|
+
Arturo::Feature.class_eval do
|
25
|
+
... funky hacks ...
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
### Catch models that are loaded in application startup.
|
30
|
+
|
31
|
+
LazyObservers.debug_active_record_loading
|
32
|
+
|
33
|
+
`script/console` or `rails c`
|
34
|
+
|
35
|
+
### Verify you did not misspell
|
36
|
+
Loads all classes registered via observers, to make sure you did not misspell</br>
|
37
|
+
(negates the effect of lazyness, so only use for debugging)
|
38
|
+
|
39
|
+
|
40
|
+
LazyObservers.check_classes
|
41
|
+
|
42
|
+
|
43
|
+
# TODO
|
44
|
+
- Rails 3.0: does not work for models that are loaded before the observers
|
45
|
+
- Rails 3.2: blows up
|
46
|
+
|
47
|
+
Author
|
48
|
+
======
|
49
|
+
[Michael Grosser](http://grosser.it)<br/>
|
50
|
+
michael@grosser.it<br/>
|
51
|
+
License: MIT<br/>
|
52
|
+
[![Build Status](https://secure.travis-ci.org/grosser/lazy_observers.png)](http://travis-ci.org/grosser/lazy_observers)
|
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/mgrosser/code/tools/lazy_observers
|
3
|
+
specs:
|
4
|
+
lazy_observers (0.0.0)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activerecord (2.3.14)
|
11
|
+
activesupport (= 2.3.14)
|
12
|
+
activesupport (2.3.14)
|
13
|
+
appraisal (0.4.1)
|
14
|
+
bundler
|
15
|
+
rake
|
16
|
+
diff-lcs (1.1.3)
|
17
|
+
rake (0.9.2.2)
|
18
|
+
rspec (2.10.0)
|
19
|
+
rspec-core (~> 2.10.0)
|
20
|
+
rspec-expectations (~> 2.10.0)
|
21
|
+
rspec-mocks (~> 2.10.0)
|
22
|
+
rspec-core (2.10.1)
|
23
|
+
rspec-expectations (2.10.0)
|
24
|
+
diff-lcs (~> 1.1.3)
|
25
|
+
rspec-mocks (2.10.1)
|
26
|
+
sqlite3 (1.3.6)
|
27
|
+
|
28
|
+
PLATFORMS
|
29
|
+
ruby
|
30
|
+
|
31
|
+
DEPENDENCIES
|
32
|
+
activerecord (~> 2.3.6)
|
33
|
+
appraisal
|
34
|
+
lazy_observers!
|
35
|
+
rake
|
36
|
+
rspec (~> 2)
|
37
|
+
sqlite3
|
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/mgrosser/code/tools/lazy_observers
|
3
|
+
specs:
|
4
|
+
lazy_observers (0.0.0)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.0.15)
|
11
|
+
activesupport (= 3.0.15)
|
12
|
+
builder (~> 2.1.2)
|
13
|
+
i18n (~> 0.5.0)
|
14
|
+
activerecord (3.0.15)
|
15
|
+
activemodel (= 3.0.15)
|
16
|
+
activesupport (= 3.0.15)
|
17
|
+
arel (~> 2.0.10)
|
18
|
+
tzinfo (~> 0.3.23)
|
19
|
+
activesupport (3.0.15)
|
20
|
+
appraisal (0.4.1)
|
21
|
+
bundler
|
22
|
+
rake
|
23
|
+
arel (2.0.10)
|
24
|
+
builder (2.1.2)
|
25
|
+
diff-lcs (1.1.3)
|
26
|
+
i18n (0.5.0)
|
27
|
+
rake (0.9.2.2)
|
28
|
+
rspec (2.10.0)
|
29
|
+
rspec-core (~> 2.10.0)
|
30
|
+
rspec-expectations (~> 2.10.0)
|
31
|
+
rspec-mocks (~> 2.10.0)
|
32
|
+
rspec-core (2.10.1)
|
33
|
+
rspec-expectations (2.10.0)
|
34
|
+
diff-lcs (~> 1.1.3)
|
35
|
+
rspec-mocks (2.10.1)
|
36
|
+
sqlite3 (1.3.6)
|
37
|
+
tzinfo (0.3.33)
|
38
|
+
|
39
|
+
PLATFORMS
|
40
|
+
ruby
|
41
|
+
|
42
|
+
DEPENDENCIES
|
43
|
+
activerecord (~> 3.0.15)
|
44
|
+
appraisal
|
45
|
+
lazy_observers!
|
46
|
+
rake
|
47
|
+
rspec (~> 2)
|
48
|
+
sqlite3
|
@@ -0,0 +1,50 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/mgrosser/code/tools/lazy_observers
|
3
|
+
specs:
|
4
|
+
lazy_observers (0.0.0)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.2.6)
|
11
|
+
activesupport (= 3.2.6)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
activerecord (3.2.6)
|
14
|
+
activemodel (= 3.2.6)
|
15
|
+
activesupport (= 3.2.6)
|
16
|
+
arel (~> 3.0.2)
|
17
|
+
tzinfo (~> 0.3.29)
|
18
|
+
activesupport (3.2.6)
|
19
|
+
i18n (~> 0.6)
|
20
|
+
multi_json (~> 1.0)
|
21
|
+
appraisal (0.4.1)
|
22
|
+
bundler
|
23
|
+
rake
|
24
|
+
arel (3.0.2)
|
25
|
+
builder (3.0.0)
|
26
|
+
diff-lcs (1.1.3)
|
27
|
+
i18n (0.6.0)
|
28
|
+
multi_json (1.3.6)
|
29
|
+
rake (0.9.2.2)
|
30
|
+
rspec (2.10.0)
|
31
|
+
rspec-core (~> 2.10.0)
|
32
|
+
rspec-expectations (~> 2.10.0)
|
33
|
+
rspec-mocks (~> 2.10.0)
|
34
|
+
rspec-core (2.10.1)
|
35
|
+
rspec-expectations (2.10.0)
|
36
|
+
diff-lcs (~> 1.1.3)
|
37
|
+
rspec-mocks (2.10.1)
|
38
|
+
sqlite3 (1.3.6)
|
39
|
+
tzinfo (0.3.33)
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
ruby
|
43
|
+
|
44
|
+
DEPENDENCIES
|
45
|
+
activerecord (~> 3.2.6)
|
46
|
+
appraisal
|
47
|
+
lazy_observers!
|
48
|
+
rake
|
49
|
+
rspec (~> 2)
|
50
|
+
sqlite3
|
@@ -0,0 +1,13 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
name = "lazy_observers"
|
3
|
+
require "#{name}/version"
|
4
|
+
|
5
|
+
Gem::Specification.new name, LazyObservers::VERSION do |s|
|
6
|
+
s.summary = "Makes Activerecord Observers lazy, do not load model on startup and only listen once a model got loaded."
|
7
|
+
s.authors = ["Michael Grosser"]
|
8
|
+
s.email = "michael@grosser.it"
|
9
|
+
s.homepage = "http://github.com/grosser/#{name}"
|
10
|
+
s.files = `git ls-files`.split("\n")
|
11
|
+
s.license = 'MIT'
|
12
|
+
s.add_runtime_dependency "activerecord"
|
13
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'lazy_observers/version'
|
2
|
+
require 'active_record'
|
3
|
+
require 'active_record/observer'
|
4
|
+
|
5
|
+
module LazyObservers
|
6
|
+
def self.register_observed(klass)
|
7
|
+
class_name = klass.name
|
8
|
+
loaded << [klass, class_name]
|
9
|
+
observers.each do |observer, observed|
|
10
|
+
connect!(observer, klass) if observed.include?(class_name)
|
11
|
+
end
|
12
|
+
(on_loads[class_name]||[]).each{|block| block.call(klass) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.register_observer(observer, classes)
|
16
|
+
observers[observer] = classes
|
17
|
+
loaded.each do |klass, name|
|
18
|
+
connect!(observer, klass) if classes.include?(name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.on_load(class_name, &block)
|
23
|
+
on_loads[class_name] ||= []
|
24
|
+
on_loads[class_name] << block
|
25
|
+
end
|
26
|
+
|
27
|
+
# to check you did not specify a class that does not exist
|
28
|
+
def self.check_classes
|
29
|
+
observers.values.flatten.uniq.each { |klass| klass.constantize }
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.debug_active_record_loading
|
33
|
+
ActiveRecord::Base.send(:extend, LazyObservers::InheritedDebugger)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def self.on_loads
|
39
|
+
@on_loads ||= {}
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.observers
|
43
|
+
@observers ||= {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.loaded
|
47
|
+
@loaded ||= []
|
48
|
+
end
|
49
|
+
|
50
|
+
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
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
module LazyObservers
|
66
|
+
module InheritedNotifier
|
67
|
+
def inherited(subclass)
|
68
|
+
LazyObservers.register_observed subclass
|
69
|
+
super
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
module InheritedDebugger
|
74
|
+
def inherited(subclass)
|
75
|
+
puts subclass
|
76
|
+
puts caller
|
77
|
+
puts "-" * 72
|
78
|
+
super
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
ActiveRecord::Base.send(:extend, LazyObservers::InheritedNotifier)
|
data/spec/app/groups.rb
ADDED
data/spec/app/movie.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
class MovieObserver < ActiveRecord::Observer
|
2
|
+
lazy_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
|
data/spec/app/post.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
class PostObserver < ActiveRecord::Observer
|
2
|
+
lazy_observe "Post"
|
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
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class ProductObserver < ActiveRecord::Observer
|
2
|
+
lazy_observe "Product"
|
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
|
data/spec/database.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "active_record"
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(
|
4
|
+
:adapter => "sqlite3",
|
5
|
+
:database => ":memory:",
|
6
|
+
)
|
7
|
+
|
8
|
+
# create tables
|
9
|
+
ActiveRecord::Schema.define(:version => 1) do
|
10
|
+
create_table :users do |t|
|
11
|
+
t.string :name
|
12
|
+
end
|
13
|
+
|
14
|
+
create_table :posts do |t|
|
15
|
+
t.string :title
|
16
|
+
end
|
17
|
+
|
18
|
+
create_table :movies do |t|
|
19
|
+
t.string :title
|
20
|
+
end
|
21
|
+
|
22
|
+
create_table :products do |t|
|
23
|
+
t.string :title
|
24
|
+
end
|
25
|
+
|
26
|
+
create_table :groups do |t|
|
27
|
+
t.string :name
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require File.expand_path("../app/user_observer", __FILE__)
|
3
|
+
require File.expand_path("../app/post_observer", __FILE__)
|
4
|
+
require File.expand_path("../app/product_observer", __FILE__)
|
5
|
+
|
6
|
+
describe LazyObservers do
|
7
|
+
it "has a VERSION" do
|
8
|
+
LazyObservers::VERSION.should =~ /^[\.\da-z]+$/
|
9
|
+
end
|
10
|
+
|
11
|
+
it "does not load models when being loaded" do
|
12
|
+
defined?(UserObserver).should == "constant"
|
13
|
+
UserObserver.instance
|
14
|
+
defined?(User).should == nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "makes the observer observe nothing" do
|
18
|
+
PostObserver.instance.observed_classes.should == Set.new
|
19
|
+
end
|
20
|
+
|
21
|
+
it "listens to events after models are loaded" do
|
22
|
+
PostObserver.instance.called.should == []
|
23
|
+
defined?(Post).should == nil
|
24
|
+
require File.expand_path("../app/post", __FILE__)
|
25
|
+
post = Post.create!
|
26
|
+
PostObserver.instance.called.should == [[:after_create, [post]], [:after_update, [post]]]
|
27
|
+
end
|
28
|
+
|
29
|
+
it "listens to events if models are loaded before observer" do
|
30
|
+
defined?(MovieObserver).should == nil
|
31
|
+
require File.expand_path("../app/movie_observer", __FILE__)
|
32
|
+
MovieObserver.instance.called.should == []
|
33
|
+
movie = Movie.create!
|
34
|
+
MovieObserver.instance.called.should == [[:after_create, [movie]], [:after_update, [movie]]]
|
35
|
+
end
|
36
|
+
|
37
|
+
it "observes inherited classes" do
|
38
|
+
defined?(Product).should == nil
|
39
|
+
defined?(InheritedProduct).should == nil
|
40
|
+
require File.expand_path("../app/products", __FILE__)
|
41
|
+
|
42
|
+
# no duplicates (sets are weirdly nested :/ )
|
43
|
+
ProductObserver.instance.observed_classes.map{|x| x.is_a?(Set) ? x.to_a : x }.flatten.map{|x| x.is_a?(Set) ? x.to_a : x }.flatten.should == [Product, InheritedProduct]
|
44
|
+
|
45
|
+
# for inherited
|
46
|
+
ProductObserver.instance.called.should == []
|
47
|
+
product = InheritedProduct.create!
|
48
|
+
ProductObserver.instance.called.should == [[:after_create, [product]], [:after_update, [product]]]
|
49
|
+
|
50
|
+
ProductObserver.instance.called.clear
|
51
|
+
|
52
|
+
# for normal
|
53
|
+
ProductObserver.instance.called.should == []
|
54
|
+
product = Product.create!
|
55
|
+
ProductObserver.instance.called.should == [[:after_create, [product]], [:after_update, [product]]]
|
56
|
+
end
|
57
|
+
|
58
|
+
it "blows up when you pass it a class, since this means you did not understand the concept" do
|
59
|
+
expect{
|
60
|
+
require File.expand_path("../app/stupid_observer", __FILE__)
|
61
|
+
}.to raise_error(/not classes or symbols/i)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "calls callback when matching class is loaded" do
|
65
|
+
loaded = []
|
66
|
+
LazyObservers.on_load("Group") do |klass|
|
67
|
+
loaded << klass
|
68
|
+
end
|
69
|
+
require File.expand_path("../app/groups", __FILE__)
|
70
|
+
loaded.should == [Group]
|
71
|
+
end
|
72
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.expand_path("../database", __FILE__)
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift 'lib'
|
4
|
+
|
5
|
+
# movie is used to test that things loaded before lazy_observers also gets observed
|
6
|
+
raise if defined?(Movie)
|
7
|
+
raise if defined?(LazyObservers.loaded)
|
8
|
+
require File.expand_path("../app/movie", __FILE__)
|
9
|
+
require "lazy_observers"
|
10
|
+
raise unless defined?(LazyObservers.loaded)
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy_observers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Grosser
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description:
|
31
|
+
email: michael@grosser.it
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- .travis.yml
|
37
|
+
- Appraisals
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- Rakefile
|
41
|
+
- Readme.md
|
42
|
+
- gemfiles/rails-2.3.gemfile
|
43
|
+
- gemfiles/rails-2.3.gemfile.lock
|
44
|
+
- gemfiles/rails-3.0.gemfile
|
45
|
+
- gemfiles/rails-3.0.gemfile.lock
|
46
|
+
- gemfiles/rails-3.2.gemfile
|
47
|
+
- gemfiles/rails-3.2.gemfile.lock
|
48
|
+
- lazy_observers.gemspec
|
49
|
+
- lib/lazy_observers.rb
|
50
|
+
- lib/lazy_observers/version.rb
|
51
|
+
- spec/app/groups.rb
|
52
|
+
- spec/app/inherited_movie.rb
|
53
|
+
- spec/app/movie.rb
|
54
|
+
- spec/app/movie_observer.rb
|
55
|
+
- spec/app/post.rb
|
56
|
+
- spec/app/post_observer.rb
|
57
|
+
- spec/app/product_observer.rb
|
58
|
+
- spec/app/products.rb
|
59
|
+
- spec/app/stupid_observer.rb
|
60
|
+
- spec/app/user_observer.rb
|
61
|
+
- spec/database.rb
|
62
|
+
- spec/lazy_observers_spec.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
homepage: http://github.com/grosser/lazy_observers
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
hash: 3828034774033645633
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
hash: 3828034774033645633
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.24
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Makes Activerecord Observers lazy, do not load model on startup and only
|
95
|
+
listen once a model got loaded.
|
96
|
+
test_files: []
|