lazy_observers 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +6 -2
- data/Gemfile +1 -0
- data/Gemfile.lock +6 -1
- data/Rakefile +1 -17
- data/Readme.md +18 -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 +6 -0
- data/lib/lazy_observers/railtie.rb +7 -0
- data/lib/lazy_observers/version.rb +1 -1
- data/spec/app/t1.rb +5 -0
- data/spec/app/t2.rb +5 -0
- data/spec/app/t3.rb +2 -0
- data/spec/lazy_observers_spec.rb +28 -6
- metadata +14 -21
- data/spec/app/groups.rb +0 -5
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9368b4489ad2f76e6ecbdd5bf386200f3145ac1b
|
4
|
+
data.tar.gz: 911a834c8160fd2b27cc23062b783a260d34b2c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 90f41cb278d45021ad2ba6e98eb02c580bd817909ae23535199f4d1e11dfdcb69ba50d12fed7cef9193a60c680e6d0e5e8a8296f75b33d2ca77ba8e68fcbabc7
|
7
|
+
data.tar.gz: 82cacd550131dc729285201634ce87d4d3ec6587515631f6f0ebbc06ba9ae257b27f099f1ef926ae5e4e096cbc4fc8f33a9c74841330b616030b3224be24bd88
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lazy_observers (0.0
|
4
|
+
lazy_observers (0.1.0)
|
5
5
|
activerecord
|
6
6
|
|
7
7
|
GEM
|
@@ -23,6 +23,7 @@ GEM
|
|
23
23
|
rake
|
24
24
|
arel (3.0.2)
|
25
25
|
builder (3.0.0)
|
26
|
+
bump (0.5.2)
|
26
27
|
diff-lcs (1.1.3)
|
27
28
|
i18n (0.6.0)
|
28
29
|
multi_json (1.3.6)
|
@@ -43,7 +44,11 @@ PLATFORMS
|
|
43
44
|
|
44
45
|
DEPENDENCIES
|
45
46
|
appraisal
|
47
|
+
bump
|
46
48
|
lazy_observers!
|
47
49
|
rake
|
48
50
|
rspec (~> 2)
|
49
51
|
sqlite3
|
52
|
+
|
53
|
+
BUNDLED WITH
|
54
|
+
1.10.6
|
data/Rakefile
CHANGED
@@ -1,27 +1,11 @@
|
|
1
1
|
require 'bundler/gem_tasks'
|
2
2
|
require 'appraisal'
|
3
|
+
require 'bump/tasks'
|
3
4
|
|
4
5
|
task :spec do
|
5
6
|
sh "rspec spec/"
|
6
7
|
end
|
7
8
|
|
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
9
|
task :default do
|
26
10
|
sh "bundle exec rake appraisal:install && bundle exec rake appraisal spec"
|
27
11
|
end
|
data/Readme.md
CHANGED
@@ -2,6 +2,22 @@ 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
|
+
Recommended: turn your observers into modules
|
6
|
+
|
7
|
+
```Ruby
|
8
|
+
module CachingObserver
|
9
|
+
def self.included(base)
|
10
|
+
this = self
|
11
|
+
base.after_save { |record| this.do_something(record); true }
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.do_something(record)
|
15
|
+
... do something ...
|
16
|
+
end
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
|
5
21
|
Install
|
6
22
|
=======
|
7
23
|
|
@@ -19,7 +35,7 @@ Usage
|
|
19
35
|
### Extend models from gems after they are loaded
|
20
36
|
|
21
37
|
LazyObservers.on_load("Arturo::Feature") do |klass|
|
22
|
-
|
38
|
+
klass.class_eval do
|
23
39
|
... funky hacks ...
|
24
40
|
end
|
25
41
|
end
|
@@ -39,6 +55,7 @@ Loads all classes registered via observers, to make sure you did not misspell</b
|
|
39
55
|
|
40
56
|
TIPS
|
41
57
|
====
|
58
|
+
- .on_load is called before all methods are defined on the baseclass, so they might not be defined
|
42
59
|
- do not use observe and lazy_observe in the same observer (and if you have to, use observe after lay_observe)
|
43
60
|
- do not use Model classes with class-methods of the observer e.g. class FooObserver; Foo.something{}; def after_update; end; end
|
44
61
|
|
data/lib/lazy_observers.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'lazy_observers/version'
|
2
2
|
require 'active_record'
|
3
3
|
require 'active_record/observer'
|
4
|
+
require 'lazy_observers/railtie' if defined?(Rails)
|
4
5
|
|
5
6
|
module LazyObservers
|
6
7
|
def self.observed_loaded(klass)
|
@@ -36,6 +37,11 @@ module LazyObservers
|
|
36
37
|
ActiveRecord::Base.send(:extend, LazyObservers::InheritedDebugger)
|
37
38
|
end
|
38
39
|
|
40
|
+
def self.clear
|
41
|
+
@observers = {}
|
42
|
+
@loaded = []
|
43
|
+
end
|
44
|
+
|
39
45
|
private
|
40
46
|
|
41
47
|
def self.on_load_callbacks
|
data/spec/app/t1.rb
ADDED
data/spec/app/t2.rb
ADDED
data/spec/app/t3.rb
ADDED
data/spec/lazy_observers_spec.rb
CHANGED
@@ -68,12 +68,34 @@ describe LazyObservers do
|
|
68
68
|
NonLazyMovieObserver.instance.called.should == [[:after_create, [movie]], [:after_update, [movie]]]
|
69
69
|
end
|
70
70
|
|
71
|
-
|
72
|
-
loaded
|
73
|
-
|
74
|
-
|
71
|
+
context "#on_load" do
|
72
|
+
it "calls callback when matching class is loaded after block is defined" do
|
73
|
+
loaded = []
|
74
|
+
LazyObservers.on_load("T3") do |klass|
|
75
|
+
loaded << klass
|
76
|
+
end
|
77
|
+
require File.expand_path("../app/t3", __FILE__)
|
78
|
+
loaded.should == [T3]
|
79
|
+
end
|
80
|
+
|
81
|
+
it "calls callback when matching class is loaded before block is defined" do
|
82
|
+
loaded = []
|
83
|
+
LazyObservers.on_load("T2") do |klass|
|
84
|
+
loaded << klass
|
85
|
+
end
|
86
|
+
require File.expand_path("../app/t2", __FILE__)
|
87
|
+
loaded.should == [T2]
|
88
|
+
end
|
89
|
+
|
90
|
+
it "can use classmethods when on_load is called" do
|
91
|
+
pending do
|
92
|
+
loaded = []
|
93
|
+
LazyObservers.on_load("T1") do |klass|
|
94
|
+
loaded << klass.xxx
|
95
|
+
end
|
96
|
+
require File.expand_path("../app/t1", __FILE__)
|
97
|
+
loaded.should == [111]
|
98
|
+
end
|
75
99
|
end
|
76
|
-
require File.expand_path("../app/groups", __FILE__)
|
77
|
-
loaded.should == [Group]
|
78
100
|
end
|
79
101
|
end
|
metadata
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_observers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Michael Grosser
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-09-01 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activerecord
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
description:
|
@@ -33,7 +30,7 @@ executables: []
|
|
33
30
|
extensions: []
|
34
31
|
extra_rdoc_files: []
|
35
32
|
files:
|
36
|
-
- .travis.yml
|
33
|
+
- ".travis.yml"
|
37
34
|
- Appraisals
|
38
35
|
- Gemfile
|
39
36
|
- Gemfile.lock
|
@@ -47,8 +44,8 @@ files:
|
|
47
44
|
- gemfiles/rails-3.2.gemfile.lock
|
48
45
|
- lazy_observers.gemspec
|
49
46
|
- lib/lazy_observers.rb
|
47
|
+
- lib/lazy_observers/railtie.rb
|
50
48
|
- lib/lazy_observers/version.rb
|
51
|
-
- spec/app/groups.rb
|
52
49
|
- spec/app/inherited_movie.rb
|
53
50
|
- spec/app/movie.rb
|
54
51
|
- spec/app/movie_observer.rb
|
@@ -58,6 +55,9 @@ files:
|
|
58
55
|
- spec/app/product_observer.rb
|
59
56
|
- spec/app/products.rb
|
60
57
|
- spec/app/stupid_observer.rb
|
58
|
+
- spec/app/t1.rb
|
59
|
+
- spec/app/t2.rb
|
60
|
+
- spec/app/t3.rb
|
61
61
|
- spec/app/user_observer.rb
|
62
62
|
- spec/database.rb
|
63
63
|
- spec/lazy_observers_spec.rb
|
@@ -65,33 +65,26 @@ files:
|
|
65
65
|
homepage: http://github.com/grosser/lazy_observers
|
66
66
|
licenses:
|
67
67
|
- MIT
|
68
|
+
metadata: {}
|
68
69
|
post_install_message:
|
69
70
|
rdoc_options: []
|
70
71
|
require_paths:
|
71
72
|
- lib
|
72
73
|
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
74
|
requirements:
|
75
|
-
- -
|
75
|
+
- - ">="
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
|
-
segments:
|
79
|
-
- 0
|
80
|
-
hash: -521166920063735374
|
81
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
-
none: false
|
83
79
|
requirements:
|
84
|
-
- -
|
80
|
+
- - ">="
|
85
81
|
- !ruby/object:Gem::Version
|
86
82
|
version: '0'
|
87
|
-
segments:
|
88
|
-
- 0
|
89
|
-
hash: -521166920063735374
|
90
83
|
requirements: []
|
91
84
|
rubyforge_project:
|
92
|
-
rubygems_version:
|
85
|
+
rubygems_version: 2.2.2
|
93
86
|
signing_key:
|
94
|
-
specification_version:
|
87
|
+
specification_version: 4
|
95
88
|
summary: Makes Activerecord Observers lazy, do not load model on startup and only
|
96
89
|
listen once a model got loaded.
|
97
90
|
test_files: []
|