Module: Acmesmith::Utils::Finder

Defined in:
lib/acmesmith/utils/finder.rb

Defined Under Namespace

Classes: NotFound

Class Method Summary collapse

Class Method Details

.find(const, prefix, name, error: true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/acmesmith/utils/finder.rb', line 6

def self.find(const, prefix, name, error: true)
  retried = false
  constant_name = name.to_s.gsub(/\A.|_./) { |s| s[-1].upcase }

  begin
    const.const_get constant_name, false
  rescue NameError
    unless retried
      begin
        require "#{prefix}/#{name}"
      rescue LoadError
      end

      retried = true
      retry
    end

    if error
      raise NotFound, "Couldn't find #{name.inspect} for #{const}"
    else
      nil
    end
  end
end