Class: AIA::Adapter::GemActivator

Inherits:
Object
  • Object
show all
Defined in:
lib/aia/adapter/gem_activator.rb

Class Method Summary collapse

Class Method Details

.activate_gem_for_require(lib) ⇒ Object

Activate a gem and add its lib path to $LOAD_PATH This bypasses Bundler’s restrictions on loading non-bundled gems



9
10
11
12
13
14
15
16
17
# File 'lib/aia/adapter/gem_activator.rb', line 9

def self.activate_gem_for_require(lib)
  return if Gem.try_activate(lib)

  gem_path = find_gem_path(lib)
  if gem_path
    lib_path = File.join(gem_path, 'lib')
    $LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)
  end
end

.find_gem_path(gem_name) ⇒ Object

Find gem path by searching gem directories directly This bypasses Bundler’s restrictions



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/aia/adapter/gem_activator.rb', line 21

def self.find_gem_path(gem_name)
  gem_dirs = Gem.path.flat_map do |base|
    gems_dir = File.join(base, 'gems')
    next [] unless File.directory?(gems_dir)

    Dir.glob(File.join(gems_dir, "#{gem_name}-*")).select do |path|
      File.directory?(path) && File.basename(path).match?(/^#{Regexp.escape(gem_name)}-[\d.]+/)
    end
  end

  # Return the most recent version
  gem_dirs.sort.last
end

.trigger_tool_loading(lib) ⇒ Object

Some tool libraries use lazy loading (e.g., Zeitwerk) and need explicit triggering to load tool classes into ObjectSpace



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aia/adapter/gem_activator.rb', line 37

def self.trigger_tool_loading(lib)
  # Convert lib name to constant (e.g., 'shared_tools' -> SharedTools)
  const_name = lib.split(/[_-]/).map(&:capitalize).join

  begin
    mod = Object.const_get(const_name)

    # Try common methods that libraries use to load tools
    if mod.respond_to?(:load_all_tools)
      mod.load_all_tools
    elsif mod.respond_to?(:tools)
      # Calling .tools often triggers lazy loading
      mod.tools
    end
  rescue NameError
    # Constant doesn't exist, library might use different naming
  end
end