Class: Klenod::Runtime::Bundle
- Inherits:
-
Object
- Object
- Klenod::Runtime::Bundle
- Defined in:
- lib/klenod/runtime/bundle.rb
Instance Attribute Summary collapse
-
#assets ⇒ Object
readonly
Returns the value of attribute assets.
-
#entrypoints ⇒ Object
readonly
Returns the value of attribute entrypoints.
-
#modules ⇒ Object
readonly
Returns the value of attribute modules.
-
#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
- #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) ⇒ 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) ⇒ Bundle
Returns a new instance of Bundle.
26 27 28 29 30 31 32 |
# File 'lib/klenod/runtime/bundle.rb', line 26 def initialize(entrypoints, modules, assets, source_root: nil) @entrypoints = entrypoints @modules = modules @assets = assets @source_root = source_root @mods = {} end |
Instance Attribute Details
#assets ⇒ Object (readonly)
Returns the value of attribute assets.
16 17 18 |
# File 'lib/klenod/runtime/bundle.rb', line 16 def assets @assets end |
#entrypoints ⇒ Object (readonly)
Returns the value of attribute entrypoints.
16 17 18 |
# File 'lib/klenod/runtime/bundle.rb', line 16 def entrypoints @entrypoints end |
#modules ⇒ Object (readonly)
Returns the value of attribute modules.
16 17 18 |
# File 'lib/klenod/runtime/bundle.rb', line 16 def modules @modules end |
#source_root ⇒ Object
Returns the value of attribute source_root.
16 17 18 |
# File 'lib/klenod/runtime/bundle.rb', line 16 def source_root @source_root end |
Class Method Details
.load(source, source_root: nil) ⇒ Object
18 19 20 |
# File 'lib/klenod/runtime/bundle.rb', line 18 def self.load(source, source_root: nil) BundleFormat.load(source, source_root: source_root) end |
.load_file(path, source_root: nil) ⇒ Object
22 23 24 |
# File 'lib/klenod/runtime/bundle.rb', line 22 def self.load_file(path, source_root: nil) load(path, source_root: source_root) end |
Instance Method Details
#asset(output_path) ⇒ Object
84 85 86 |
# File 'lib/klenod/runtime/bundle.rb', line 84 def asset(output_path) @assets.fetch(output_path) end |
#asset_references_for_module(module_ref, type: nil, content_type: nil, recursive: true) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/klenod/runtime/bundle.rb', line 97 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 |
#assets_for(logical_name) ⇒ Object
88 89 90 |
# File 'lib/klenod/runtime/bundle.rb', line 88 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
92 93 94 95 |
# File 'lib/klenod/runtime/bundle.rb', line 92 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
112 113 114 115 116 |
# File 'lib/klenod/runtime/bundle.rb', line 112 def each_asset(&block) return enum_for(:each_asset) unless block @assets.each_value(&block) end |
#exports(entrypoint = nil) ⇒ Object
76 77 78 |
# File 'lib/klenod/runtime/bundle.rb', line 76 def exports(entrypoint = nil) load(entrypoint).const_get(:Exports) end |
#load(entrypoint = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/klenod/runtime/bundle.rb', line 34 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
50 51 52 53 |
# File 'lib/klenod/runtime/bundle.rb', line 50 def load_entrypoints entrypoint_ids = entrypoints.respond_to?(:values) ? entrypoints.values : entrypoints entrypoint_ids.map { |entrypoint| load(entrypoint) } end |
#marshal_dump ⇒ Object
123 124 125 |
# File 'lib/klenod/runtime/bundle.rb', line 123 def marshal_dump [@entrypoints, @modules, @assets, @source_root] end |
#marshal_load(data) ⇒ Object
127 128 129 130 |
# File 'lib/klenod/runtime/bundle.rb', line 127 def marshal_load(data) @entrypoints, @modules, @assets, @source_root = data @mods = {} end |
#mod(id) ⇒ Object
80 81 82 |
# File 'lib/klenod/runtime/bundle.rb', line 80 def mod(id) @mods.fetch(id.to_s) end |
#module_id_for(module_ref) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/klenod/runtime/bundle.rb', line 132 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
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/klenod/runtime/bundle.rb', line 55 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
66 67 68 69 70 71 72 73 74 |
# File 'lib/klenod/runtime/bundle.rb', line 66 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 |