Class: Utopia::ImportMap::Builder
- Inherits:
-
Object
- Object
- Utopia::ImportMap::Builder
- Defined in:
- lib/utopia/import_map.rb
Overview
Builder class for constructing import maps with scoped base URIs.
The builder supports nested with(base:) blocks where each base is resolved
relative to its parent base, following RFC 3986 URL resolution rules.
Class Method Summary collapse
-
.build(import_map, **options, &block) ⇒ Object
Configure an import map using a scoped builder.
Instance Method Summary collapse
-
#import(specifier, value, integrity: nil) ⇒ Object
Add an import mapping with the current base URI.
-
#initialize(import_map, base: nil) ⇒ Builder
constructor
Initialize a scoped builder for an import map.
-
#scope(scope_prefix, imports) ⇒ Object
Add a scope mapping.
-
#with(base:, &block) ⇒ Object
Create a nested scope with a different base URI.
Constructor Details
#initialize(import_map, base: nil) ⇒ Builder
Initialize a scoped builder for an import map.
99 100 101 102 |
# File 'lib/utopia/import_map.rb', line 99 def initialize(import_map, base: nil) @import_map = import_map @base = Protocol::URL[base] end |
Class Method Details
.build(import_map, **options, &block) ⇒ Object
Configure an import map using a scoped builder.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/utopia/import_map.rb', line 84 def self.build(import_map, **, &block) builder = self.new(import_map, **) if block.arity == 1 yield(builder) else builder.instance_eval(&block) end return builder end |
Instance Method Details
#import(specifier, value, integrity: nil) ⇒ Object
Add an import mapping with the current base URI.
If a base is set, the value is resolved relative to that base following RFC 3986. Absolute URLs (scheme://...) are preserved as-is when used as values.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/utopia/import_map.rb', line 118 def import(specifier, value, integrity: nil) resolved_value = if @base value_url = Protocol::URL[value] # Combine base with value (@base + value_url).to_s else value end @import_map.import(specifier, resolved_value, integrity: integrity) self end |
#scope(scope_prefix, imports) ⇒ Object
Add a scope mapping.
Scopes allow different import resolutions for different parts of your application.
170 171 172 173 |
# File 'lib/utopia/import_map.rb', line 170 def scope(scope_prefix, imports) @import_map.scope(scope_prefix, imports) self end |
#with(base:, &block) ⇒ Object
Create a nested scope with a different base URI.
The new base is resolved relative to the current base. This allows for hierarchical organization of imports from different sources.
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/utopia/import_map.rb', line 149 def with(base:, &block) # Resolve the new base relative to the current base resolved_base = if @base @base + Protocol::URL[base] else base end self.class.build(@import_map, base: resolved_base, &block) end |