Module: OpenNamespace::ClassMethods
- Defined in:
- lib/open_namespace/class_methods.rb
Instance Method Summary collapse
-
#const_missing(name) ⇒ Class, Module
protected
Provides transparent access to require_const.
-
#const_search(file_name) ⇒ Object?
Finds the constant with a name similar to the given file name.
-
#namespace_root ⇒ String
The file path that represents the namespace.
-
#namespace_root=(new_path) ⇒ String
Sets the file path of the namespace.
-
#require_const(name) ⇒ Class, ...
Requires the file and finds the newly defined constant.
-
#require_file(name) ⇒ true?
Requires the file with the given name, within the namespace root directory.
Instance Method Details
#const_missing(name) ⇒ Class, Module (protected)
Provides transparent access to require_const.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/open_namespace/class_methods.rb', line 135 def const_missing(name) file_name = OpenNamespace.constant_path(name) if require_file(file_name) # # If const_missing is being called, the constant is not present yet. # Therefor, only check for the constant if the file was # successfully loaded by require_file. # if self.const_defined?(name) # get the exact constant name that was requested return self.const_get(name) end end return super(name) end |
#const_search(file_name) ⇒ Object?
Finds the constant with a name similar to the given file name.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/open_namespace/class_methods.rb', line 94 def const_search(file_name) names = file_name.to_s.split(/[:\/]+/) scope = self 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 |
#namespace_root ⇒ String
The file path that represents the namespace.
9 10 11 |
# File 'lib/open_namespace/class_methods.rb', line 9 def namespace_root @namespace_root ||= OpenNamespace.constant_path(self.name) end |
#namespace_root=(new_path) ⇒ String
Sets the file path of the namespace.
22 23 24 |
# File 'lib/open_namespace/class_methods.rb', line 22 def namespace_root=(new_path) @namespace_root = new_path.to_s end |
#require_const(name) ⇒ Class, ...
Requires the file and finds the newly defined constant.
45 46 47 48 49 |
# File 'lib/open_namespace/class_methods.rb', line 45 def require_const(name) require_file(name) return const_search(name) end |
#require_file(name) ⇒ true?
Requires the file with the given name, within the namespace root directory.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/open_namespace/class_methods.rb', line 67 def require_file(name) name = name.to_s path = File.join(namespace_root,File.(File.join('',name))) begin require path rescue Gem::LoadError => e raise(e) rescue ::LoadError return nil end return true end |