Module: Easyload::SingletonExtensions
- Defined in:
- lib/easyload/singleton_extensions.rb
Overview
The singleton methods that are defined for a module that includes Easyload.
Instance Attribute Summary collapse
-
#easyload_root ⇒ Object
readonly
The root path that easyload should use when loading a child constant for this module.
Instance Method Summary collapse
-
#const_missing(sym) ⇒ Object
The meat of easyloading happens here.
-
#easyload_from(root_path) ⇒ Object
Sets easyload_root.
-
#easyload_path_component_for_sym(sym) ⇒ Object
Converts a CamelCased symbol into a path component.
Instance Attribute Details
#easyload_root ⇒ Object (readonly)
The root path that easyload should use when loading a child constant for this module.
Defaults to the path component form of the class/module’s name
8 9 10 |
# File 'lib/easyload/singleton_extensions.rb', line 8 def easyload_root @easyload_root end |
Instance Method Details
#const_missing(sym) ⇒ Object
The meat of easyloading happens here.
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 63 64 65 66 67 68 69 70 |
# File 'lib/easyload/singleton_extensions.rb', line 33 def const_missing(sym) if not self.instance_variable_defined? :@easyload_root $stderr.puts "You must call easyload_from() before you can easyload #{self}::#{sym}" return super(sym) end path_component = self.easyload_path_component_for_sym(sym) easyload_path = File.join(@easyload_root, "#{path_component}.rb") # Search for the file to include $LOAD_PATH.each do |load_root| full_load_path = File.join(load_root, easyload_path) if File.exists? full_load_path self.module_eval(File.read(full_load_path)) # Did we get our target constant? if self.const_defined? sym target_const = self.const_get(sym) class << target_const include SingletonExtensions end target_const.easyload_from(File.join(self.easyload_root, path_component)) return self.const_get(sym) # Warn but still break the load process. We don't want to support ambiguous load # paths. else $stderr.puts "Attempted to easyload #{sym} from '#{full_load_path}', but it doesn't appear to exist in that source file." end break end end return super(sym) end |
#easyload_from(root_path) ⇒ Object
Sets easyload_root.
11 12 13 |
# File 'lib/easyload/singleton_extensions.rb', line 11 def easyload_from(root_path) @easyload_root = root_path.to_s end |
#easyload_path_component_for_sym(sym) ⇒ Object
Converts a CamelCased symbol into a path component.
-
A
Singlesymbol becomessingle. -
MultiWordSymbolsbecomemulti_word_symbols. -
ACRONYMSare treated like words:acronyms. -
ABCFoois considered to be a mix of acronyms and words:abc_foo.
21 22 23 24 25 26 27 28 |
# File 'lib/easyload/singleton_extensions.rb', line 21 def easyload_path_component_for_sym(sym) path = sym.to_s.dup path.gsub!(/([A-Z]+)([A-Z][a-z]+)/, '\1_\2_') path.gsub!(/([A-Z][a-z]+)/, '\1_') path.gsub!(/_+/, '_') path.chomp!('_') path.downcase! end |