Module: Poro::Util::ModuleFinder
- Defined in:
- lib/poro/util/module_finder.rb
Class Method Summary collapse
-
.find(arg, relative_root = Module, strict = false) ⇒ Object
Finds the given module by string.
Class Method Details
.find(arg, relative_root = Module, strict = false) ⇒ Object
Finds the given module by string.
Arguments:
- arg
-
The module/class name to find.
- relative_root
-
If given, tries to find the requested class/module within this module/class. May be a string.
- strict
-
Normally–mostly like Ruby–a top-level class will be returned at any step if no module/class matches in the namespace it is searching. If this is true, then a last check is made to see if the returned module/class is actually inside of the relative root.
If given a class, it returns it directly. TODO: Make it look it up if relative_root is not Module or Object.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/poro/util/module_finder.rb', line 19 def self.find(arg, relative_root=Module, strict=false) # If the argument is a kind of class, just return right away. return arg if arg.kind_of?(Module) || arg.kind_of?(Class) # Now we need to treat it as a string: arg = arg.to_s raise NameError, "Could not find a module or class from nothing." if arg.nil? || arg.empty? # First, define the recursive function. recursive_resolve = lambda do |curr_mod, names| head, *rest = names.to_a if( head.nil? && rest.empty? ) curr_mod elsif( !(head.to_s.empty?) && curr_mod.respond_to?(:const_defined?) && curr_mod.const_defined?(head) ) recursive_resolve.call( curr_mod.const_get(head), rest) else raise NameError, "Could not find a module or class from #{arg.inspect}" end end # If starting with ::, then the constant is aboslutely referenced. relative_root = Module if arg[0,2]=='::' # Split into names start_index = arg[0,2]=='::' ? 2 : 0 mod_names = arg[start_index..-1].split('::') # Now get the module or class. relative_root = self.send(__method__, relative_root) mod = recursive_resolve.call(relative_root, mod_names) # Now, if we are strict, verify it is of the type we are looking for. if !(relative_root==Module || relative_root==Object) && !(mod.name.include?(relative_root.name)) = "Could not find a module or class #{mod.name.inspect} inside of #{relative_root.name.inspect}" if( strict ) raise NameError, else STDERR.puts "WARNING: #{}, top level mod/class used instead." end end # Return return mod end |