Class: Constance::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/constance/resolver.rb

Class Method Summary collapse

Class Method Details

.resolve(from_mod, const_name) ⇒ Object

return a constant or if returns nil, Rails will use ActiveSupport::Dependencies.load_missing_constant



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/constance/resolver.rb', line 6

def self.resolve(from_mod, const_name)
  if Constance.caller_search_mapping
    puts "#{self.class.name} checking caller stack against caller_search_mapping regular expressions #{Constance.caller_search_mapping.inspect}" if Constance.verbose?
    klass = resolve_by_caller_search_mapping(from_mod, const_name, Constance.caller_search_mapping)
    puts "#{self.class.name} caller_search_mapping did not resolve" if Constance.verbose? && !klass
  end
  if !klass && Constance.proc.is_a?(Proc)
    puts "#{self.class.name} sending from_mod=#{from_mod} and const_name=#{const_name} into proc" if Constance.verbose?
    klass = Constance.proc.call(from_mod, const_name)
    puts "#{self.class.name} proc did not resolve" if Constance.verbose? && !klass
  end
  if klass
    puts "#{self.class.name} setting #{const_name} => #{klass} on ActiveSupport::Dependencies::Reference @store" if Constance.verbose?
    ActiveSupport::Dependencies::Reference.instance_variable_get(:@store)[const_name] = klass if klass
  end
  klass
end

.resolve_by_caller_search_mapping(from_mod, const_name, caller_search_mapping) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/constance/resolver.rb', line 24

def self.resolve_by_caller_search_mapping(from_mod, const_name, caller_search_mapping)
  caller.each do |line|
    caller_search_mapping.keys.each do |search|
      if search =~ line
        puts "#{self.class.name} matched #{search} in #{line}" if Constance.verbose?
        return caller_search_mapping[search][const_name.to_s].try(:constantize)
      end
    end
  end
end