Sha256: 2522b47eced0e23b249ca985b46e345dba13c252dceec8a18b90f600462c301e

Contents?: true

Size: 1.21 KB

Versions: 8

Compression:

Stored size: 1.21 KB

Contents

require 'dm-core/core_ext/object'

class Module

  def find_const(const_name)
    if const_name[0..1] == '::'
      Object.full_const_get(const_name[2..-1])
    else
      nested_const_lookup(const_name)
    end
  end

  private

  # Doesn't do any caching since constants can change with remove_const
  def nested_const_lookup(const_name)
    unless equal?(Object)
      constants = []

      name.split('::').each do |part|
        const = constants.last || Object
        constants << const.const_get(part)
      end

      parts = const_name.split('::')

      # from most to least specific constant, use each as a base and try
      # to find a constant with the name const_name within them
      constants.reverse_each do |const|
        # return the nested constant if available
        return const if parts.all? do |part|
          const = if RUBY_VERSION >= '1.9.0'
            const.const_defined?(part, false) ? const.const_get(part, false) : nil
          else
            const.const_defined?(part) ? const.const_get(part) : nil
          end
        end
      end
    end

    # no relative constant found, fallback to an absolute lookup and
    # use const_missing if not found
    Object.full_const_get(const_name)
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
dm-core-1.1.0.rc2 lib/dm-core/core_ext/module.rb
dm-core-1.1.0.rc1 lib/dm-core/core_ext/module.rb
dm-core-1.0.2 lib/dm-core/core_ext/module.rb
dm-core-1.0.1 lib/dm-core/core_ext/module.rb
dm-core-1.0.0 lib/dm-core/core_ext/module.rb
dm-core-1.0.0.rc3 lib/dm-core/core_ext/module.rb
dm-core-1.0.0.rc2 lib/dm-core/core_ext/module.rb
dm-core-1.0.0.rc1 lib/dm-core/core_ext/module.rb