Class: Capsium::Package::Manifest

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/capsium/package/manifest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Manifest

Returns a new instance of Manifest.



17
18
19
20
21
22
23
24
25
26
# File 'lib/capsium/package/manifest.rb', line 17

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(content: generate_manifest)
            end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



13
14
15
# File 'lib/capsium/package/manifest.rb', line 13

def config
  @config
end

#content_pathObject

Returns the value of attribute content_path.



13
14
15
# File 'lib/capsium/package/manifest.rb', line 13

def content_path
  @content_path
end

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/capsium/package/manifest.rb', line 13

def path
  @path
end

Instance Method Details

#content_file_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/capsium/package/manifest.rb', line 58

def content_file_exists?(path)
  File.exist?(path_to_content_file(path))
end

#generate_manifestObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/capsium/package/manifest.rb', line 28

def generate_manifest
  files = Dir[File.join(@content_path, "**", "*")].reject do |f|
    File.directory?(f)
  end

  files.sort.map do |file_path|
    ManifestConfigItem.new(
      file: relative_path(file_path),
      mime: mime_from_path(file_path),
    )
  end
end

#lookup(filename) ⇒ Object



41
42
43
44
45
# File 'lib/capsium/package/manifest.rb', line 41

def lookup(filename)
  @config.content.detect do |data_item|
    data_item.file == filename
  end
end

#path_to_content_file(path) ⇒ Object

Raises:

  • (TypeError)


52
53
54
55
56
# File 'lib/capsium/package/manifest.rb', line 52

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) ⇒ Object



62
63
64
# File 'lib/capsium/package/manifest.rb', line 62

def relative_path(path)
  Pathname.new(path).relative_path_from(File.dirname(@path)).to_s
end

#save_to_file(output_path = @path) ⇒ Object



47
48
49
50
# File 'lib/capsium/package/manifest.rb', line 47

def save_to_file(output_path = @path)
  @config.content.sort_by!(&:file)
  File.write(output_path, to_json)
end