Module: OpenNamespace
- Defined in:
- lib/open_namespace/version.rb,
lib/open_namespace/class_methods.rb,
lib/open_namespace/open_namespace.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
open_namespace version
'0.4.2'
Class Method Summary collapse
-
.const_lookup(scope, name) ⇒ Object?
Finds the exact constant.
-
.const_path(name) ⇒ String
Maps a constant name to a likely file path.
-
.const_search(scope, file_name) ⇒ Object?
Finds the constant with a name similar to the given file name.
- .included(base) ⇒ Object
Class Method Details
.const_lookup(scope, name) ⇒ Object?
Finds the exact constant.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/open_namespace/open_namespace.rb', line 55 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) ⇒ String
Maps a constant name to a likely file path.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/open_namespace/open_namespace.rb', line 23 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 |
.const_search(scope, file_name) ⇒ Object?
Finds the constant with a name similar to the given file name.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/open_namespace/open_namespace.rb', line 86 def self.const_search(scope,file_name) names = file_name.to_s.split(/[:\/]+/) until names.empty? name = names.shift # strip any dashes or underscores name.tr!('_-','') # perform a case insensitive search const_pattern = /^#{name}$/i # grep for the constant const_name = scope.constants.find { |name| name =~ const_pattern } # the constant search failed return nil unless const_name scope = scope.const_get(const_name) end return scope end |
.included(base) ⇒ Object
6 7 8 |
# File 'lib/open_namespace/open_namespace.rb', line 6 def self.included(base) base.extend ClassMethods end |