Class: Capsium::Package::Manifest
- Inherits:
-
Object
- Object
- Capsium::Package::Manifest
- Extended by:
- Forwardable
- Defined in:
- sig/capsium/package/manifest.rbs,
lib/capsium/package/manifest.rb
Overview
Loads, generates and writes manifest.json (ARCHITECTURE.md section 3).
Instance Attribute Summary collapse
-
#config ⇒ ManifestConfig
readonly
Returns the value of attribute config.
-
#content_path ⇒ String
readonly
Returns the value of attribute content_path.
-
#path ⇒ String
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #content_file_exists?(path) ⇒ Boolean
-
#generate_manifest ⇒ Hash[String, Resource]
Auto-generation: scan content/ recursively, detect MIME types, default visibility "exported", deterministic (sorted) output.
-
#initialize(path) ⇒ Manifest
constructor
A new instance of Manifest.
- #lookup(path) ⇒ Resource?
- #path_to_content_file(path) ⇒ Pathname
- #relative_path(path) ⇒ String
- #resources ⇒ Hash[String, Resource]
- #save_to_file(output_path = @path) ⇒ void
- #to_hash ⇒ Hash[String, untyped]
- #to_json(*_args) ⇒ String
- #type_for(path) ⇒ String?
Constructor Details
#initialize(path) ⇒ Manifest
Returns a new instance of Manifest.
16 17 18 19 20 21 22 23 24 |
# File 'lib/capsium/package/manifest.rb', line 16 def initialize(path) @path = path @content_path = File.join(File.dirname(@path), Package::CONTENT_DIR) @config = if File.exist?(path) ManifestConfig.from_json(File.read(path)) else ManifestConfig.new(resources: generate_manifest) end end |
Instance Attribute Details
#config ⇒ ManifestConfig (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/capsium/package/manifest.rb', line 12 def config @config end |
#content_path ⇒ String (readonly)
Returns the value of attribute content_path.
12 13 14 |
# File 'lib/capsium/package/manifest.rb', line 12 def content_path @content_path end |
#path ⇒ String (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/capsium/package/manifest.rb', line 12 def path @path end |
Instance Method Details
#content_file_exists?(path) ⇒ Boolean
63 64 65 |
# File 'lib/capsium/package/manifest.rb', line 63 def content_file_exists?(path) File.exist?(path_to_content_file(path)) end |
#generate_manifest ⇒ Hash[String, Resource]
Auto-generation: scan content/ recursively, detect MIME types, default visibility "exported", deterministic (sorted) output.
29 30 31 32 33 34 |
# File 'lib/capsium/package/manifest.rb', line 29 def generate_manifest content_files.sort.to_h do |file_path| [relative_path(file_path), Resource.new(type: mime_from_path(file_path), visibility: "exported")] end end |
#lookup(path) ⇒ Resource?
40 41 42 |
# File 'lib/capsium/package/manifest.rb', line 40 def lookup(path) resources[path] end |
#path_to_content_file(path) ⇒ Pathname
57 58 59 60 61 |
# File 'lib/capsium/package/manifest.rb', line 57 def path_to_content_file(path) raise TypeError, "Path cannot be nil" if path.nil? Pathname.new(File.dirname(@path)).join(path) end |
#relative_path(path) ⇒ String
67 68 69 |
# File 'lib/capsium/package/manifest.rb', line 67 def relative_path(path) Pathname.new(path).relative_path_from(File.dirname(@path)).to_s end |
#resources ⇒ Hash[String, Resource]
36 37 38 |
# File 'lib/capsium/package/manifest.rb', line 36 def resources @config.resources end |
#save_to_file(output_path = @path) ⇒ void
This method returns an undefined value.
53 54 55 |
# File 'lib/capsium/package/manifest.rb', line 53 def save_to_file(output_path = @path) File.write(output_path, to_json) end |
#to_hash ⇒ Hash[String, untyped]
23 |
# File 'sig/capsium/package/manifest.rbs', line 23
def to_hash: () -> Hash[String, untyped]
|
#to_json(*_args) ⇒ String
49 50 51 |
# File 'lib/capsium/package/manifest.rb', line 49 def to_json(*_args) @config.to_json end |
#type_for(path) ⇒ String?
44 45 46 47 |
# File 'lib/capsium/package/manifest.rb', line 44 def type_for(path) resource = lookup(path) resource&.type end |