Sha256: 645e7864b8a3e84595576d7ccfb279f536639b39c7fba513b2ea606b57b935cc

Contents?: true

Size: 642 Bytes

Versions: 14

Compression:

Stored size: 642 Bytes

Contents

module Bogus
  class ConvertsNameToClass
    extend Takes

    class CanNotFindClass < RuntimeError; end

    takes :search_modules

    def convert(name)
      class_name = camelize(name)
      klass = nil

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

      raise CanNotFindClass.new("Can not locate class for name: #{name}") unless klass

      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

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
bogus-0.1.7 lib/bogus/fakes/converts_name_to_class.rb
bogus-0.1.6 lib/bogus/fakes/converts_name_to_class.rb
bogus-0.1.5 lib/bogus/fakes/converts_name_to_class.rb
bogus-0.1.4 lib/bogus/fakes/converts_name_to_class.rb
bogus-0.1.3 lib/bogus/converts_name_to_class.rb
bogus-0.1.2 lib/bogus/converts_name_to_class.rb
bogus-0.1.1 lib/bogus/converts_name_to_class.rb
bogus-0.1.0 lib/bogus/converts_name_to_class.rb
bogus-0.0.4 lib/bogus/converts_name_to_class.rb
bogus-0.0.3 lib/bogus/converts_name_to_class.rb
bogus-0.0.3.rc.2 lib/bogus/converts_name_to_class.rb
bogus-0.0.3.rc.1 lib/bogus/converts_name_to_class.rb
bogus-0.0.2 lib/bogus/converts_name_to_class.rb
bogus-0.0.1 lib/bogus/converts_name_to_class.rb