Sha256: 8c299b4e12b24dc7f5e4f1e55d8c569e288661562071f36776e47bdad2dac4f8

Contents?: true

Size: 1.12 KB

Versions: 115

Compression:

Stored size: 1.12 KB

Contents

module Tins
  module DeepConstGet
    if ::Object.method(:const_defined?).arity == 1
      # :nocov:
      # We do not create coverage on 1.8
      def self.const_defined_in?(modul, constant)
        modul.const_defined?(constant)
      end
      # :nocov:
    else
      def self.const_defined_in?(modul, constant)
        modul.const_defined?(constant, false)
      end
    end

    def self.deep_const_get(path, start_module = Object)
      path.to_s.split('::').inject(start_module) do |p, c|
        case
        when c.empty?
          if start_module == Object
            Object
          else
            raise ArgumentError, "top level constants cannot be reached from"\
              " start module #{start_module.inspect}"
          end
        when const_defined_in?(p, c)
          p.const_get(c)
        else
          begin
            p.const_missing(c)
          rescue NameError => e
            raise ArgumentError, "can't get const #{path}: #{e}"
          end
        end
      end
    end

    def deep_const_get(path, start_module = Object)
      ::Tins::DeepConstGet.deep_const_get(path, start_module)
    end
  end
end

Version data entries

115 entries across 101 versions & 8 rubygems

Version Path
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/tins-1.26.0/lib/tins/deep_const_get.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tins-1.26.0/lib/tins/deep_const_get.rb
tins-1.28.0 lib/tins/deep_const_get.rb
tins-1.27.1 lib/tins/deep_const_get.rb
tins-1.27.0 lib/tins/deep_const_get.rb
tdiary-5.1.4 vendor/bundle/ruby/2.7.0/gems/tins-1.26.0/lib/tins/deep_const_get.rb
tins-1.26.0 lib/tins/deep_const_get.rb
tdiary-5.1.3 vendor/bundle/ruby/2.7.0/gems/tins-1.25.0/lib/tins/deep_const_get.rb
tdiary-5.1.3 vendor/bundle/ruby/2.6.0/gems/tins-1.25.0/lib/tins/deep_const_get.rb
tdiary-5.1.2 vendor/bundle/ruby/2.7.0/gems/tins-1.25.0/lib/tins/deep_const_get.rb
tins-1.25.0 lib/tins/deep_const_get.rb
tdiary-5.1.1 vendor/bundle/ruby/2.7.0/gems/tins-1.24.1/lib/tins/deep_const_get.rb
tins-1.24.1 lib/tins/deep_const_get.rb
tins-1.24.0 lib/tins/deep_const_get.rb
tins-1.23.0 lib/tins/deep_const_get.rb
tdiary-5.1.0 vendor/bundle/gems/tins-1.22.2/lib/tins/deep_const_get.rb
tins-1.22.2 lib/tins/deep_const_get.rb
tins-1.22.1 lib/tins/deep_const_get.rb
tins-1.22.0 lib/tins/deep_const_get.rb
tins-1.21.1 lib/tins/deep_const_get.rb