Module: Leftovers::Autoloader
- Included in:
- Leftovers, AST, ConfigLoader, FileCollector, FileCollector::NodeProcessor, MatcherBuilders, Matchers, Precompilers, Precompilers::YAML, ProcessorBuilders, Processors
- Defined in:
- lib/leftovers/autoloader.rb
Overview
zero dependency zeitwerk
Constant Summary collapse
- ALL_CAPS_NAMES =
%w{ast cli version erb json yaml}.freeze
Class Method Summary collapse
- .class_from_path(path) ⇒ Object
- .dir_path_from_class(klass) ⇒ Object
- .glob_children(klass) ⇒ Object
- .included(klass) ⇒ Object
- .root ⇒ Object
Class Method Details
.class_from_path(path) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/leftovers/autoloader.rb', line 14 def self.class_from_path(path) name = ::File.basename(path).delete_suffix('.rb') if ALL_CAPS_NAMES.include?(name) name.upcase else name.gsub(/(?:^|_)(\w)/, &:upcase).delete('_') end end |
.dir_path_from_class(klass) ⇒ Object
23 24 25 26 |
# File 'lib/leftovers/autoloader.rb', line 23 def self.dir_path_from_class(klass) klass.name.gsub(/::/, '/') .gsub(/(?<=[a-z])([A-Z])/, '_\1').downcase end |
.glob_children(klass) ⇒ Object
28 29 30 |
# File 'lib/leftovers/autoloader.rb', line 28 def self.glob_children(klass) "#{root}/#{dir_path_from_class(klass)}/*.rb" end |
.included(klass) ⇒ Object
8 9 10 11 12 |
# File 'lib/leftovers/autoloader.rb', line 8 def self.included(klass) ::Dir[glob_children(klass)].each_entry do |path| klass.autoload(class_from_path(path), path) end end |
.root ⇒ Object
32 33 34 |
# File 'lib/leftovers/autoloader.rb', line 32 def self.root ::File.dirname(__dir__) end |