Module: Familia::Autoloader

Included in:
Features
Defined in:
lib/familia/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

Class Method Details

.autoload_files(patterns, exclude: [], log_prefix: 'Autoloader') ⇒ Object

Autoloads Ruby files matching the given patterns.

Parameters:

  • patterns (String, Array<String>)

    file patterns to match (supports Dir.glob patterns)

  • exclude (Array<String>) (defaults to: [])

    basenames to exclude from loading

  • log_prefix (String) (defaults to: 'Autoloader')

    prefix for debug logging messages



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/familia/autoloader.rb', line 14

def self.autoload_files(patterns, exclude: [], log_prefix: 'Autoloader')
  patterns = Array(patterns)

  patterns.each do |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.expand_path(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.

Parameters:

  • base (Module)

    the module including this autoloader



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/familia/autoloader.rb', line 36

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)
  features_dir = File.join(base_path, 'features')

  Familia.ld "[DEBUG] Autoloader loading features from #{features_dir}"

  return unless Dir.exist?(features_dir)

  # Use the shared autoload_files method
  autoload_files(
    File.join(features_dir, '*.rb'),
    log_prefix: 'Autoloader'
  )
end