constance 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +65 -0
- data/Rakefile +11 -0
- data/lib/constance/config.rb +13 -0
- data/lib/constance/dependencies.rb +20 -0
- data/lib/constance/resolver.rb +35 -0
- data/lib/constance/version.rb +3 -0
- data/lib/constance.rb +4 -0
- metadata +53 -0
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
Constance for Rails 3.x+
|
2
|
+
=====
|
3
|
+
|
4
|
+
Debug or override Rails 3.x+ constant loading.
|
5
|
+
|
6
|
+
### Installation
|
7
|
+
|
8
|
+
Put this in your Gemfile:
|
9
|
+
|
10
|
+
gem 'constance'
|
11
|
+
|
12
|
+
Do:
|
13
|
+
|
14
|
+
bundle install
|
15
|
+
|
16
|
+
### Configuration
|
17
|
+
|
18
|
+
In `application.rb` just above your application and after the Bundler config, add the Constance configuration, e.g.:
|
19
|
+
|
20
|
+
...
|
21
|
+
|
22
|
+
# Debug class/module loading without stacks
|
23
|
+
Constance.configure do
|
24
|
+
self.debug = true
|
25
|
+
self.verbose = false
|
26
|
+
end
|
27
|
+
|
28
|
+
module MyRailsApp
|
29
|
+
class Application < Rails::Application
|
30
|
+
...
|
31
|
+
|
32
|
+
You can configure on one line like:
|
33
|
+
|
34
|
+
Constance.debug = true
|
35
|
+
|
36
|
+
Constance.verbose = true
|
37
|
+
|
38
|
+
Constance.proc = lambda{|from_mod, const_name| puts "my lambda was called from_mod=#{from_mod.inspect} const_name=#{const_name.inspect} caller: #{caller.inspect} and I can return any class or constant I want, or call some other class or method. But, for kicks I'll return nil"; nil}
|
39
|
+
|
40
|
+
Constance.caller_search_mapping = {/(\/app\/|\/lib\/)/ => {'Journey' => 'Fantastic::Journey'}} # go through the caller stack to see if was called from a local or gem file that had app or lib in the path
|
41
|
+
|
42
|
+
or using a block:
|
43
|
+
|
44
|
+
Constance.configure do
|
45
|
+
self.debug = true
|
46
|
+
self.verbose = true
|
47
|
+
self.proc = lambda{|from_mod, const_name| puts "my lambda was called from_mod=#{from_mod.inspect} const_name=#{const_name.inspect} caller: #{caller.inspect} and I can return any class or constant I want, or call some other class or method. But, for kicks I'll return nil"; nil}
|
48
|
+
self.caller_search_mapping = {/(\/app\/|\/lib\/)/ => {'Journey' => 'Fantastic::Journey'}} # go through the caller stack to see if was called from a local or gem file that had app or lib in the path
|
49
|
+
end
|
50
|
+
|
51
|
+
### Example
|
52
|
+
|
53
|
+
Non-verbose debug logging:
|
54
|
+
|
55
|
+
Loaded missing constant from_module: Object const_name: :Foo using ActiveSupport::Dependencies. Got: Foo
|
56
|
+
Loaded missing constant from_module: Object const_name: :ApplicationController using ActiveSupport::Dependencies. Got: ApplicationController
|
57
|
+
Loaded missing constant from_module: InheritedResources const_name: :Base using ActiveSupport::Dependencies. Got: InheritedResources::Base
|
58
|
+
Loaded missing constant from_module: Object const_name: :Bar using ActiveSupport::Dependencies. Got: Bar
|
59
|
+
Loaded missing constant from_module: Object const_name: "Foobar" using ActiveSupport::Dependencies. Got: Foobar
|
60
|
+
|
61
|
+
### License
|
62
|
+
|
63
|
+
Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
|
64
|
+
|
65
|
+
[lic]: http://github.com/garysweaver/constance/blob/master/LICENSE
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
#http://nicksda.apotomo.de/2010/10/testing-your-rails-3-engine-sitting-in-a-gem/
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << 'test'
|
6
|
+
t.test_files = FileList['test/**/*_test.rb']
|
7
|
+
t.verbose = true
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "Run tests"
|
11
|
+
task :default => :test
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'constance/config'
|
2
|
+
require 'pp'
|
3
|
+
|
4
|
+
module ActiveSupport
|
5
|
+
module Dependencies
|
6
|
+
alias_method(:load_missing_constant_constance_renamed, :load_missing_constant)
|
7
|
+
undef_method(:load_missing_constant)
|
8
|
+
def load_missing_constant(from_mod, const_name)
|
9
|
+
puts "Loading missing constant from_module: #{from_mod.inspect} const_name: #{const_name.inspect}\ncaller was:\n#{caller.pretty_inspect}" if Constance.debug? && Constance.verbose?
|
10
|
+
result = Constance::Resolver.resolve(from_mod, const_name)
|
11
|
+
if result
|
12
|
+
puts "Loaded missing constant from_module: #{from_mod.inspect} const_name: #{const_name.inspect} using Constance::Resolver. Got: #{result.inspect}" if Constance.debug?
|
13
|
+
else
|
14
|
+
result = load_missing_constant_constance_renamed(from_mod, const_name)
|
15
|
+
puts "Loaded missing constant from_module: #{from_mod.inspect} const_name: #{const_name.inspect} using ActiveSupport::Dependencies. Got: #{result.inspect}}" if Constance.debug?
|
16
|
+
end
|
17
|
+
result
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'constance/config'
|
2
|
+
|
3
|
+
module Constance
|
4
|
+
class Resolver
|
5
|
+
# return a constant or if returns nil, Rails will use ActiveSupport::Dependencies.load_missing_constant
|
6
|
+
def self.resolve(from_mod, const_name)
|
7
|
+
if Constance.caller_search_mapping
|
8
|
+
puts "#{self.class.name} checking caller stack against caller_search_mapping regular expressions #{Constance.caller_search_mapping.inspect}" if Constance.verbose?
|
9
|
+
klass = resolve_by_caller_search_mapping(from_mod, const_name, Constance.caller_search_mapping)
|
10
|
+
puts "#{self.class.name} caller_search_mapping did not resolve" if Constance.verbose? && !klass
|
11
|
+
end
|
12
|
+
if !klass && Constance.proc.is_a?(Proc)
|
13
|
+
puts "#{self.class.name} sending from_mod=#{from_mod} and const_name=#{const_name} into proc" if Constance.verbose?
|
14
|
+
klass = Constance.proc.call(from_mod, const_name)
|
15
|
+
puts "#{self.class.name} proc did not resolve" if Constance.verbose? && !klass
|
16
|
+
end
|
17
|
+
if klass
|
18
|
+
puts "#{self.class.name} setting #{const_name} => #{klass} on ActiveSupport::Dependencies::Reference @store" if Constance.verbose?
|
19
|
+
ActiveSupport::Dependencies::Reference.instance_variable_get(:@store)[const_name] = klass if klass
|
20
|
+
end
|
21
|
+
klass
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.resolve_by_caller_search_mapping(from_mod, const_name, caller_search_mapping)
|
25
|
+
caller.each do |line|
|
26
|
+
caller_search_mapping.keys.each do |search|
|
27
|
+
if search =~ line
|
28
|
+
puts "#{self.class.name} matched #{search} in #{line}" if Constance.verbose?
|
29
|
+
return caller_search_mapping[search][const_name.to_s].try(:constantize)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/constance.rb
ADDED
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: constance
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gary S. Weaver
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-05 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Debug and override Rails 3.x constant loading.
|
15
|
+
email:
|
16
|
+
- garysweaver@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/constance/config.rb
|
22
|
+
- lib/constance/dependencies.rb
|
23
|
+
- lib/constance/resolver.rb
|
24
|
+
- lib/constance/version.rb
|
25
|
+
- lib/constance.rb
|
26
|
+
- Rakefile
|
27
|
+
- README.md
|
28
|
+
homepage: https://github.com/garysweaver/constance
|
29
|
+
licenses:
|
30
|
+
- MIT
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.8.24
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Debug and override Rails 3.x constant loading.
|
53
|
+
test_files: []
|