Class: Opencdd::Cddal::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/opencdd/cddal/resolver.rb

Overview

Resolves a CDDAL module specifier to a canonical key and source text. The canonical key (absolute path or normalized URL) is used by the Builder to deduplicate imports — the same module imported twice loads only once.

Resolution rules, in priority order:

1. URL (http://, https://, file://) — fetched via the
 configured Fetcher. URL is the canonical key.
2. Absolute filesystem path — read directly. Expanded path
 is the canonical key.
3. Relative path (starts with ./ or ../) — resolved against
 the importing file's directory, falling back to base_path.
4. Bare name — searched in search_path entries, then
 base_path. First match wins.

When strict: is false (default), URL fetch failures and missing paths emit a warning and return [nil, nil] so the Builder can skip them. When strict: is true, raises Opencdd::Cddal::ImportError on any resolution failure.

Constant Summary collapse

DEFAULT_SEARCH_PATH =
[Pathname.pwd].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path: nil, search_path: nil, fetcher: nil, strict: false, quiet: false) ⇒ Resolver

Returns a new instance of Resolver.



38
39
40
41
42
43
44
45
# File 'lib/opencdd/cddal/resolver.rb', line 38

def initialize(base_path: nil, search_path: nil, fetcher: nil,
               strict: false, quiet: false)
  @base_path   = base_path ? Pathname.new(base_path) : Pathname.pwd
  @search_path = Array(search_path).map { |p| Pathname.new(p.to_s) }
  @fetcher     = fetcher || Opencdd::Cddal::Fetcher::NetHttp.new
  @strict      = strict
  @quiet       = quiet
end

Instance Attribute Details

#base_pathObject (readonly)

When strict: is false (default), URL fetch failures and missing paths emit a warning and return [nil, nil] so the Builder can skip them. When strict: is true, raises Opencdd::Cddal::ImportError on any resolution failure. Pass quiet: true to suppress the non-strict warning (used by tests that intentionally exercise unreachable URLs).



36
37
38
# File 'lib/opencdd/cddal/resolver.rb', line 36

def base_path
  @base_path
end

#fetcherObject (readonly)

When strict: is false (default), URL fetch failures and missing paths emit a warning and return [nil, nil] so the Builder can skip them. When strict: is true, raises Opencdd::Cddal::ImportError on any resolution failure. Pass quiet: true to suppress the non-strict warning (used by tests that intentionally exercise unreachable URLs).



36
37
38
# File 'lib/opencdd/cddal/resolver.rb', line 36

def fetcher
  @fetcher
end

#quietObject (readonly)

When strict: is false (default), URL fetch failures and missing paths emit a warning and return [nil, nil] so the Builder can skip them. When strict: is true, raises Opencdd::Cddal::ImportError on any resolution failure. Pass quiet: true to suppress the non-strict warning (used by tests that intentionally exercise unreachable URLs).



36
37
38
# File 'lib/opencdd/cddal/resolver.rb', line 36

def quiet
  @quiet
end

#search_pathObject (readonly)

When strict: is false (default), URL fetch failures and missing paths emit a warning and return [nil, nil] so the Builder can skip them. When strict: is true, raises Opencdd::Cddal::ImportError on any resolution failure. Pass quiet: true to suppress the non-strict warning (used by tests that intentionally exercise unreachable URLs).



36
37
38
# File 'lib/opencdd/cddal/resolver.rb', line 36

def search_path
  @search_path
end

#strictObject (readonly)

When strict: is false (default), URL fetch failures and missing paths emit a warning and return [nil, nil] so the Builder can skip them. When strict: is true, raises Opencdd::Cddal::ImportError on any resolution failure. Pass quiet: true to suppress the non-strict warning (used by tests that intentionally exercise unreachable URLs).



36
37
38
# File 'lib/opencdd/cddal/resolver.rb', line 36

def strict
  @strict
end

Instance Method Details

#resolve(specifier, importing_file: nil) ⇒ Object

Returns a tuple [canonical_key, source_text]. Returns [nil, nil] in non-strict mode when resolution fails.



49
50
51
52
53
54
55
56
57
58
# File 'lib/opencdd/cddal/resolver.rb', line 49

def resolve(specifier, importing_file: nil)
  specifier = specifier.to_s
  return resolve_url(specifier) if url?(specifier)

  resolve_path(specifier, importing_file)
rescue Opencdd::Cddal::ImportError => e
  raise if @strict
  warn "CDDAL: #{e.message}" unless @quiet
  [nil, nil]
end