require_hooks 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +70 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/lib/require_hooks.rb +32 -0
- data/require_hooks.gemspec +58 -0
- data/spec/require_hooks_spec.rb +7 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +102 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Phil Smith
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
= require_hooks
|
2
|
+
|
3
|
+
This extension to ActiveSupport adds callbacks to be run before or after the dependency management
|
4
|
+
loads specific files.
|
5
|
+
|
6
|
+
Register callbacks with <tt>#before_require</tt> and <tt>#after_require</tt>:
|
7
|
+
|
8
|
+
# app/models/post.rb
|
9
|
+
class Post < ActiveRecord::Base
|
10
|
+
end
|
11
|
+
|
12
|
+
# lib/my_hot_extension.rb
|
13
|
+
module MyHotExtension
|
14
|
+
unloadable # prevent "TypeError: Can't dup NilClass"
|
15
|
+
|
16
|
+
def self.included(post)
|
17
|
+
post.instance_eval do
|
18
|
+
has_many :whatnots
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# config/initializers/my_hot_extension.rb
|
24
|
+
after_require 'post' do
|
25
|
+
require_or_load 'my_hot_extension'
|
26
|
+
Post.send :include, MyHotExtension
|
27
|
+
end
|
28
|
+
|
29
|
+
This can be handy if you want to transparently do things when a distant file is loaded (such as
|
30
|
+
load local modifications), but you A) can't touch the distant file and B) you don't want to just
|
31
|
+
eagerly load it, but react only _if_ it is ever loaded.
|
32
|
+
|
33
|
+
= "TypeError: Can't dup NilClass"
|
34
|
+
|
35
|
+
This problem isn't directly related to this gem, but it is certainly in the neighborhood of my
|
36
|
+
intended usage for it.
|
37
|
+
|
38
|
+
Say you're writing a rails plugin that wants to provide an AR model or augment an application
|
39
|
+
model. You put your extension in a module and have your users include it into their model class.
|
40
|
+
When they fire up <tt>rails s</tt>, the first request they make works great, but the second one
|
41
|
+
falls down crying something about duping NilClass. If they fire it up in production mode, it
|
42
|
+
works fine.
|
43
|
+
|
44
|
+
You are being bit by some auto-loading magic rails does behind the scenes. Things in the user's
|
45
|
+
<tt>app</tt> directory are auto-reloaded every request in development mode. Things in engines,
|
46
|
+
plugins, etc, are auto-loaded once. This impedance mismatch between "every" and "once" is what's
|
47
|
+
causing the pain: if you could change the "once"s to "every"s, you'd be fine.
|
48
|
+
|
49
|
+
There are some ways to proceed. The first is a monkey trick: explicitly mark your extension as
|
50
|
+
"unloadable", which tells rails to remove the module at the end of the request (in dev mode only.)
|
51
|
+
This tricks the loader into concluding, on the next request, that your module hadn't been loaded
|
52
|
+
after all (and so it hasn't been loaded once), and happily loads it again.
|
53
|
+
|
54
|
+
A better way may be to just ask rails to not treat your module as a "load once" dependency. In the
|
55
|
+
console of an application using your extension, peek at
|
56
|
+
<tt>ActiveSupport::Dependencies.autoload_paths</tt> and
|
57
|
+
<tt>ActiveSupport::Dependencies.autoload_once_paths</tt>, which is a strict subset of the former.
|
58
|
+
(Actually, rails 3 beta4 and earlier call them <tt>load_paths</tt> and <tt>load_once_paths</tt>.)
|
59
|
+
Notice <tt>autoload_once_paths</tt> includes your extension's load paths; this is why rails treats
|
60
|
+
it as a "load once" dependency. You can remove them from the array to fix the problem (they'll still
|
61
|
+
be in <tt>autoload_paths</tt>.)
|
62
|
+
|
63
|
+
This area of the rails code appears to currently be in flux (as of just before 3.0.0 rc1.) If you
|
64
|
+
are encountering this problem and the above doesn't look right or work for you, check out the
|
65
|
+
<tt>autoload_once_paths</tt> and <tt>autoload_paths</tt> properties of your engine's configuration
|
66
|
+
object.
|
67
|
+
|
68
|
+
== Copyright
|
69
|
+
|
70
|
+
Copyright (c) 2010 Phil Smith. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "require_hooks"
|
8
|
+
gem.summary = %Q{React to ActiveSupport loading or auto-loading a file}
|
9
|
+
gem.description = %Q{Extend ActiveSupport to run blocks before or after a file is "require_or_load"ed into memory.}
|
10
|
+
gem.email = "phil.h.smith@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/phs/require_hooks"
|
12
|
+
gem.authors = ["Phil Smith"]
|
13
|
+
gem.add_dependency "activesupport", "3.0.0.beta4"
|
14
|
+
# gem.add_dependency "activesupport", ">= 3.0.0.rc1" # TODO: load_paths is renamed autoload_paths on next release
|
15
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :spec => :check_dependencies
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
require 'rake/rdoctask'
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
42
|
+
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = "require_hooks #{version}"
|
45
|
+
rdoc.rdoc_files.include('README*')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
47
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'active_support/dependencies'
|
2
|
+
require 'active_support/lazy_load_hooks'
|
3
|
+
require 'active_support/core_ext/module/aliasing'
|
4
|
+
require 'active_support/core_ext/module/delegation'
|
5
|
+
|
6
|
+
module ActiveSupport
|
7
|
+
module Dependencies
|
8
|
+
|
9
|
+
def before_require(file_name, &block)
|
10
|
+
resolved = search_for_file(file_name) || file_name
|
11
|
+
ActiveSupport.on_load("before_#{resolved}", :yield => true, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def after_require(file_name, &block)
|
15
|
+
resolved = search_for_file(file_name) || file_name
|
16
|
+
ActiveSupport.on_load("after_#{resolved}", :yield => true, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def require_or_load_with_load_hooks(file_name)
|
20
|
+
resolved = search_for_file(file_name) || file_name
|
21
|
+
ActiveSupport.run_load_hooks("before_#{resolved}")
|
22
|
+
require_or_load_without_load_hooks(file_name)
|
23
|
+
ActiveSupport.run_load_hooks("after_#{resolved}")
|
24
|
+
end
|
25
|
+
|
26
|
+
alias_method_chain :require_or_load, :load_hooks
|
27
|
+
|
28
|
+
module Loadable
|
29
|
+
delegate :before_require, :after_require, :to => Dependencies
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{require_hooks}
|
8
|
+
s.version = "0.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Phil Smith"]
|
12
|
+
s.date = %q{2010-07-08}
|
13
|
+
s.description = %q{Extend ActiveSupport to run blocks before or after a file is "require_or_load"ed into memory.}
|
14
|
+
s.email = %q{phil.h.smith@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/require_hooks.rb",
|
27
|
+
"require_hooks.gemspec",
|
28
|
+
"spec/require_hooks_spec.rb",
|
29
|
+
"spec/spec.opts",
|
30
|
+
"spec/spec_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/phs/require_hooks}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.6}
|
36
|
+
s.summary = %q{React to ActiveSupport loading or auto-loading a file}
|
37
|
+
s.test_files = [
|
38
|
+
"spec/require_hooks_spec.rb",
|
39
|
+
"spec/spec_helper.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_runtime_dependency(%q<activesupport>, ["= 3.0.0.beta4"])
|
48
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<activesupport>, ["= 3.0.0.beta4"])
|
51
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<activesupport>, ["= 3.0.0.beta4"])
|
55
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: require_hooks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 0.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Phil Smith
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-08 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activesupport
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 3
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
- beta4
|
32
|
+
version: 3.0.0.beta4
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rspec
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 1
|
44
|
+
- 2
|
45
|
+
- 9
|
46
|
+
version: 1.2.9
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
description: Extend ActiveSupport to run blocks before or after a file is "require_or_load"ed into memory.
|
50
|
+
email: phil.h.smith@gmail.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files:
|
56
|
+
- LICENSE
|
57
|
+
- README.rdoc
|
58
|
+
files:
|
59
|
+
- .document
|
60
|
+
- .gitignore
|
61
|
+
- LICENSE
|
62
|
+
- README.rdoc
|
63
|
+
- Rakefile
|
64
|
+
- VERSION
|
65
|
+
- lib/require_hooks.rb
|
66
|
+
- require_hooks.gemspec
|
67
|
+
- spec/require_hooks_spec.rb
|
68
|
+
- spec/spec.opts
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://github.com/phs/require_hooks
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options:
|
76
|
+
- --charset=UTF-8
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 1.3.6
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: React to ActiveSupport loading or auto-loading a file
|
100
|
+
test_files:
|
101
|
+
- spec/require_hooks_spec.rb
|
102
|
+
- spec/spec_helper.rb
|