module OpenNamespace

Constants

VERSION

open_namespace version

Public Class Methods

const_lookup(scope,name) click to toggle source

Finds the exact constant.

@param [Module, Class] scope

The scope to begin searching within.

@param [String] name

The name of the constant.

@return [Object, nil]

The exact constant or `nil` if the constant could not be found.

@since 0.4.0

@api semipublic

# File lib/open_namespace/open_namespace.rb, line 53
def self.const_lookup(scope,name)
  names = name.split('::')

  until names.empty?
    begin
      scope = scope.const_get(names.shift)
    rescue NameError
      return nil
    end
  end

  return scope
end
const_path(name) click to toggle source

Maps a constant name to a likely file path.

@param [String, Symbol] name

The constant name.

@return [String]

The file path that the constant is likely to be defined within.

@since 0.3.0

@api semipublic

# File lib/open_namespace/open_namespace.rb, line 21
def self.const_path(name)
  path = name.to_s.dup

  # back-ported from extlib's String#to_const_path
  path.gsub!(/::/,'/')

  # back-ported from extlib's String#snake_case
  unless path.match(/\A[A-Z]+\z/)
    path.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    path.gsub!(/([a-z])([A-Z])/, '\1_\2')
  end

  path.downcase!
  return path
end
included(base) click to toggle source
# File lib/open_namespace/open_namespace.rb, line 4
def self.included(base)
  base.extend ClassMethods
end