Module: Familia::Features::Autoloader
- Included in:
- Familia::Features
- Defined in:
- lib/familia/features/autoloader.rb
Overview
Provides autoloading functionality for Ruby files based on patterns and conventions.
Used by the Features module at library startup to load feature files, and available as a utility for other modules requiring file autoloading capabilities.
Class Method Summary collapse
-
.autoload_files(patterns, exclude: [], log_prefix: 'Autoloader') ⇒ Object
Autoloads Ruby files matching the given patterns.
-
.included(base) ⇒ Object
Autoloads feature files when this module is included.
Class Method Details
.autoload_files(patterns, exclude: [], log_prefix: 'Autoloader') ⇒ Object
Autoloads Ruby files matching the given patterns.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/familia/features/autoloader.rb', line 44 def self.autoload_files(patterns, exclude: [], log_prefix: 'Autoloader') patterns = Array(patterns) patterns.each do |pattern| Familia.ld "[#{log_prefix}] Autoloader loading features from #{pattern}" Dir.glob(pattern).each do |file_path| basename = File.basename(file_path) # Skip excluded files next if exclude.include?(basename) Familia.trace :FEATURE, nil, "[#{log_prefix}] Loading #{file_path}", caller(1..1) if Familia.debug? require File.(file_path) end end end |
.included(base) ⇒ Object
Autoloads feature files when this module is included.
Discovers and loads all Ruby files in the features/ directory relative to the including module’s location. Typically used by Familia::Features.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/familia/features/autoloader.rb', line 17 def self.included(base) # Get the directory where the including module is defined # This should be lib/familia for the Features module base_path = File.dirname(caller_locations(1, 1).first.path) model_name = base.name.snake_case dir_patterns = [ File.join(base_path, 'features', '*.rb'), File.join(base_path, model_name, 'features', '*.rb'), File.join(base_path, model_name, 'features.rb'), ] # Ensure the Features module exists within the base module unless base.const_defined?(:Features) || model_name.eql?('features') base.const_set(:Features, Module.new) end # Use the shared autoload_files method autoload_files(dir_patterns, log_prefix: "Autoloader[#{model_name}]") end |