Module: Sourcerer::Util::GemUri

Defined in:
lib/sourcerer/util/gem_uri.rb

Overview

Resolve gem:// URIs to absolute filesystem paths.

Not required internally; callers must require this file explicitly.

Constant Summary collapse

GEM_URI_PATTERN =
%r{\Agem://([^/]+)/(.+)\z}

Class Method Summary collapse

Class Method Details

.resolve(path) ⇒ String

Resolve a gem:// URI to an absolute path within the named gem's directory. Returns path unchanged if it is not a gem:// URI.

URI format: gem:/// Example: gem://schemagraphy/lib/schemagraphy/cfgyml/templates/foo.liquid

Parameters:

  • path (String)

    A gem:// URI or any other string.

Returns:

  • (String)

    The resolved absolute path, or the original string if not a gem:// URI.

Raises:

  • (ArgumentError)

    If the gem:// URI is malformed.

  • (LoadError)

    If the referenced gem is not loaded.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sourcerer/util/gem_uri.rb', line 21

def self.resolve path
  return path unless path.is_a?(String) && path.start_with?('gem://')

  match = GEM_URI_PATTERN.match(path)
  raise ArgumentError, "Invalid gem:// URI: #{path}" unless match

  spec = Gem.loaded_specs[match[1]]
  raise LoadError, "Gem '#{match[1]}' not loaded (referenced in gem:// URI: #{path})" unless spec

  File.join(spec.gem_dir, match[2])
end