File: class_name_resolver.rb

package info (click to toggle)
ruby-dependor 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 264 kB
  • sloc: ruby: 557; makefile: 2; sh: 1
file content (30 lines) | stat: -rw-r--r-- 614 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Dependor
  class ClassNameResolver
    attr_reader :search_modules

    def initialize(search_modules)
      @search_modules = search_modules
    end

    def for_name(name)
      class_name = camelize(name)
      modules = search_modules + [Object]
      klass = nil

      modules.each do |mod|
        klass = mod.const_get(class_name) rescue nil
        break if klass
      end

      klass
    end

    private

    def camelize(symbol)
      string = symbol.to_s
      string = string.gsub(/_\w/) { |match| match[1].upcase }
      return string.gsub(/^\w/) { |match| match.upcase }
    end
  end
end