Class: Klenod::Runtime::Bundle
- Inherits:
-
Object
- Object
- Klenod::Runtime::Bundle
- Defined in:
- lib/klenod/runtime/bundle.rb
Instance Attribute Summary collapse
-
#asset_origin ⇒ Object
readonly
Returns the value of attribute asset_origin.
-
#assets ⇒ Object
readonly
Returns the value of attribute assets.
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#entrypoints ⇒ Object
readonly
Returns the value of attribute entrypoints.
-
#modules ⇒ Object
readonly
Returns the value of attribute modules.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#source_root ⇒ Object
Returns the value of attribute source_root.
Class Method Summary collapse
Instance Method Summary collapse
- #asset(output_path) ⇒ Object
- #asset_references_for_module(module_ref, type: nil, content_type: nil, recursive: true) ⇒ Object
- #asset_url(asset_or_output_path) ⇒ Object
- #assets_for(logical_name) ⇒ Object
- #assets_for_module(module_ref, type: nil, content_type: nil, recursive: true) ⇒ Object
- #each_asset(&block) ⇒ Object
- #exports(entrypoint = nil) ⇒ Object
-
#initialize(entrypoints, modules, assets, source_root: nil, base: AssetUrl::DEFAULT_BASE, namespace: Module.new) ⇒ Bundle
constructor
A new instance of Bundle.
- #load(entrypoint = nil) ⇒ Object
- #load_entrypoints ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(data) ⇒ Object
- #mod(id) ⇒ Object
- #module_id_for(module_ref) ⇒ Object
- #preload(module_ref = nil) ⇒ Object
- #preload_entrypoints ⇒ Object
Constructor Details
#initialize(entrypoints, modules, assets, source_root: nil, base: AssetUrl::DEFAULT_BASE, namespace: Module.new) ⇒ Bundle
Returns a new instance of Bundle.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/klenod/runtime/bundle.rb', line 39 def initialize(entrypoints, modules, assets, source_root: nil, base: AssetUrl::DEFAULT_BASE, namespace: Module.new) @entrypoints = entrypoints @modules = modules @source_root = source_root @base = AssetUrl.normalize(base) @asset_origin = AssetUrl.origin(@base) @assets = bind_asset_urls(assets) @namespace = namespace @mods = {} end |
Instance Attribute Details
#asset_origin ⇒ Object (readonly)
Returns the value of attribute asset_origin.
29 30 31 |
# File 'lib/klenod/runtime/bundle.rb', line 29 def asset_origin @asset_origin end |
#assets ⇒ Object (readonly)
Returns the value of attribute assets.
29 30 31 |
# File 'lib/klenod/runtime/bundle.rb', line 29 def assets @assets end |
#base ⇒ Object (readonly)
Returns the value of attribute base.
29 30 31 |
# File 'lib/klenod/runtime/bundle.rb', line 29 def base @base end |
#entrypoints ⇒ Object (readonly)
Returns the value of attribute entrypoints.
29 30 31 |
# File 'lib/klenod/runtime/bundle.rb', line 29 def entrypoints @entrypoints end |
#modules ⇒ Object (readonly)
Returns the value of attribute modules.
29 30 31 |
# File 'lib/klenod/runtime/bundle.rb', line 29 def modules @modules end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
29 30 31 |
# File 'lib/klenod/runtime/bundle.rb', line 29 def namespace @namespace end |
#source_root ⇒ Object
Returns the value of attribute source_root.
29 30 31 |
# File 'lib/klenod/runtime/bundle.rb', line 29 def source_root @source_root end |
Class Method Details
.load(source, source_root: nil) ⇒ Object
31 32 33 |
# File 'lib/klenod/runtime/bundle.rb', line 31 def self.load(source, source_root: nil) BundleFormat.load(source, source_root: source_root) end |
.load_file(path, source_root: nil) ⇒ Object
35 36 37 |
# File 'lib/klenod/runtime/bundle.rb', line 35 def self.load_file(path, source_root: nil) load(path, source_root: source_root) end |
Instance Method Details
#asset(output_path) ⇒ Object
100 101 102 |
# File 'lib/klenod/runtime/bundle.rb', line 100 def asset(output_path) @assets.fetch(output_path) end |
#asset_references_for_module(module_ref, type: nil, content_type: nil, recursive: true) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/klenod/runtime/bundle.rb', line 124 def asset_references_for_module(module_ref, type: nil, content_type: nil, recursive: true) seen_assets = {} module_ids_for_assets(module_ref, recursive: recursive) .each_with_index .flat_map do |module_id, index| assets_for_runtime_module(module_id).filter_map do |asset| next unless asset_matches?(asset, type: type, content_type: content_type) next if seen_assets.key?(asset.output_path) seen_assets[asset.output_path] = true AssetReference.new(index:, asset:) end end end |
#asset_url(asset_or_output_path) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/klenod/runtime/bundle.rb', line 104 def asset_url(asset_or_output_path) if asset_or_output_path.respond_to?(:output_path) asset = @assets.fetch(asset_or_output_path.output_path, asset_or_output_path) return asset.url || AssetUrl.join(base, asset.output_path) end @assets.fetch(asset_or_output_path).url rescue KeyError AssetUrl.join(base, asset_or_output_path) end |
#assets_for(logical_name) ⇒ Object
115 116 117 |
# File 'lib/klenod/runtime/bundle.rb', line 115 def assets_for(logical_name) @assets.values.select { |asset| asset.logical_name == logical_name.to_s } end |
#assets_for_module(module_ref, type: nil, content_type: nil, recursive: true) ⇒ Object
119 120 121 122 |
# File 'lib/klenod/runtime/bundle.rb', line 119 def assets_for_module(module_ref, type: nil, content_type: nil, recursive: true) asset_references_for_module(module_ref, type: type, content_type: content_type, recursive: recursive) .map(&:asset) end |
#each_asset(&block) ⇒ Object
139 140 141 142 143 |
# File 'lib/klenod/runtime/bundle.rb', line 139 def each_asset(&block) return enum_for(:each_asset) unless block @assets.each_value(&block) end |
#exports(entrypoint = nil) ⇒ Object
92 93 94 |
# File 'lib/klenod/runtime/bundle.rb', line 92 def exports(entrypoint = nil) load(entrypoint).const_get(:Exports) end |
#load(entrypoint = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/klenod/runtime/bundle.rb', line 50 def load(entrypoint = nil) id = if entrypoint entrypoint elsif entrypoints.respond_to?(:values) entrypoints.values.first else entrypoints.first end id = id.to_s id = entrypoints.fetch(id, id) if entrypoints.respond_to?(:fetch) instantiate(id) end |
#load_entrypoints ⇒ Object
66 67 68 69 |
# File 'lib/klenod/runtime/bundle.rb', line 66 def load_entrypoints entrypoint_ids = entrypoints.respond_to?(:values) ? entrypoints.values : entrypoints entrypoint_ids.map { |entrypoint| load(entrypoint) } end |
#marshal_dump ⇒ Object
150 151 152 |
# File 'lib/klenod/runtime/bundle.rb', line 150 def marshal_dump [@entrypoints, @modules, @assets, @source_root, @base] end |
#marshal_load(data) ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/klenod/runtime/bundle.rb', line 154 def marshal_load(data) @entrypoints, @modules, assets, @source_root, base = data @base = AssetUrl.normalize(base) @asset_origin = AssetUrl.origin(@base) @assets = bind_asset_urls(assets) @namespace = Module.new @mods = {} end |
#mod(id) ⇒ Object
96 97 98 |
# File 'lib/klenod/runtime/bundle.rb', line 96 def mod(id) @mods.fetch(id.to_s) end |
#module_id_for(module_ref) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/klenod/runtime/bundle.rb', line 163 def module_id_for(module_ref) id = module_ref.respond_to?(:path) ? module_ref.path : module_ref.to_s id = entrypoints.fetch(id, id) if entrypoints.respond_to?(:fetch) return id if @modules.key?(id) canonical_module_id = module_id_for_canonical_ref(id) return canonical_module_id if canonical_module_id if (relative_id = module_id_for_absolute_ref(id)) return relative_id end raise KeyError, "No module in bundle for #{module_ref.inspect}" end |
#preload(module_ref = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/klenod/runtime/bundle.rb', line 71 def preload(module_ref = nil) module_ids = if module_ref reachable_module_ids(module_ref) else @modules.keys end module_ids.map { |module_id| instantiate(module_id) } end |
#preload_entrypoints ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/klenod/runtime/bundle.rb', line 82 def preload_entrypoints entrypoint_ids = entrypoints.respond_to?(:values) ? entrypoints.values : entrypoints seen = Set.new module_ids = entrypoint_ids .flat_map { |entrypoint| reachable_module_ids(entrypoint) } .select { |module_id| seen.add?(module_id) } module_ids.map { |module_id| instantiate(module_id) } end |