constance 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -0
- data/lib/constance/dependencies.rb +6 -2
- data/lib/constance/resolver.rb +13 -1
- data/lib/constance/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -48,6 +48,10 @@ or using a block:
|
|
48
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
49
|
end
|
50
50
|
|
51
|
+
### Replacing existing constants
|
52
|
+
|
53
|
+
Constance::Resolver.will_replace(Journey)
|
54
|
+
|
51
55
|
### Example
|
52
56
|
|
53
57
|
Non-verbose debug logging:
|
@@ -9,10 +9,14 @@ module ActiveSupport
|
|
9
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
10
|
result = Constance::Resolver.resolve(from_mod, const_name)
|
11
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?
|
12
|
+
puts "Loaded missing constant from_module: #{from_mod.inspect} const_name: #{const_name.inspect} using Constance::Resolver. Got: #{result.inspect}" if result && Constance.debug?
|
13
13
|
else
|
14
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?
|
15
|
+
puts "Loaded missing constant from_module: #{from_mod.inspect} const_name: #{const_name.inspect} using ActiveSupport::Dependencies. Got: #{result.inspect}}" if result && Constance.debug?
|
16
|
+
end
|
17
|
+
unless result
|
18
|
+
result = Constance::Resolver::Store[const_name.to_sym]
|
19
|
+
puts "Loaded missing constant from_module: #{from_mod.inspect} const_name: #{const_name.inspect} using Constance::Resolver. Got: #{result.inspect}" if result && Constance.debug?
|
16
20
|
end
|
17
21
|
result
|
18
22
|
end
|
data/lib/constance/resolver.rb
CHANGED
@@ -2,6 +2,8 @@ require 'constance/config'
|
|
2
2
|
|
3
3
|
module Constance
|
4
4
|
class Resolver
|
5
|
+
Store = {}
|
6
|
+
|
5
7
|
# return a constant or if returns nil, Rails will use ActiveSupport::Dependencies.load_missing_constant
|
6
8
|
def self.resolve(from_mod, const_name)
|
7
9
|
if Constance.caller_search_mapping
|
@@ -14,7 +16,8 @@ module Constance
|
|
14
16
|
klass = Constance.proc.call(from_mod, const_name)
|
15
17
|
puts "#{self.class.name} proc did not resolve" if Constance.verbose? && !klass
|
16
18
|
end
|
17
|
-
if
|
19
|
+
# if got something and didn't ask to replace it
|
20
|
+
if klass && !Store[const_name.to_sym]
|
18
21
|
puts "#{self.class.name} setting #{const_name} => #{klass} on ActiveSupport::Dependencies::Reference @store" if Constance.verbose?
|
19
22
|
ActiveSupport::Dependencies::Reference.instance_variable_get(:@store)[const_name] = klass if klass
|
20
23
|
end
|
@@ -31,5 +34,14 @@ module Constance
|
|
31
34
|
end
|
32
35
|
end
|
33
36
|
end
|
37
|
+
|
38
|
+
def self.will_replace(const)
|
39
|
+
Store[const.to_s.to_sym] = const
|
40
|
+
begin
|
41
|
+
Object.send(:remove_const, const)
|
42
|
+
rescue
|
43
|
+
puts "#{self.class.name} removed #{const.to_s.to_sym} with:\n#{$!.message}\n#{$!.backtrace}" if Constance.verbose?
|
44
|
+
end
|
45
|
+
end
|
34
46
|
end
|
35
47
|
end
|
data/lib/constance/version.rb
CHANGED