Class: Cabriolet::FileEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/file_entry.rb

Overview

Represents a file to be added to an archive

Single responsibility: Encapsulate file metadata and data access. Supports both disk files and memory data, providing unified interface for file operations across all format compressors.

Examples:

Adding a disk file

entry = FileEntry.new(
  source: "/path/to/file.txt",
  archive_path: "docs/file.txt"
)

Adding memory data

entry = FileEntry.new(
  data: "Hello, World!",
  archive_path: "greeting.txt"
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(archive_path:, source: nil, data: nil, **options) ⇒ FileEntry

Initialize a file entry

Parameters:

  • source (String, nil) (defaults to: nil)

    Path to source file on disk

  • data (String, nil) (defaults to: nil)

    File data in memory

  • archive_path (String)

    Path within the archive

  • options (Hash)

    Format-specific options

Raises:



31
32
33
34
35
36
37
38
# File 'lib/cabriolet/file_entry.rb', line 31

def initialize(archive_path:, source: nil, data: nil, **options)
  @source_path = source
  @data = data
  @archive_path = archive_path
  @options = options

  validate!
end

Instance Attribute Details

#archive_pathObject (readonly)

Returns the value of attribute archive_path.



22
23
24
# File 'lib/cabriolet/file_entry.rb', line 22

def archive_path
  @archive_path
end

#dataObject (readonly)

Returns the value of attribute data.



22
23
24
# File 'lib/cabriolet/file_entry.rb', line 22

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'lib/cabriolet/file_entry.rb', line 22

def options
  @options
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



22
23
24
# File 'lib/cabriolet/file_entry.rb', line 22

def source_path
  @source_path
end

Instance Method Details

#attributesInteger

Get file attributes

Returns:

  • (Integer)

    File attributes flags



93
94
95
96
97
98
# File 'lib/cabriolet/file_entry.rb', line 93

def attributes
  return @options[:attributes] if @options[:attributes]
  return Constants::ATTRIB_ARCH if from_memory?

  calculate_disk_attributes
end

#compress?Boolean

Get compression flag from options

Returns:

  • (Boolean)

    Whether to compress this file



103
104
105
# File 'lib/cabriolet/file_entry.rb', line 103

def compress?
  @options.fetch(:compress, true)
end

#from_disk?Boolean

Check if file data is from disk

Returns:

  • (Boolean)

    true if file is on disk



43
44
45
# File 'lib/cabriolet/file_entry.rb', line 43

def from_disk?
  !@source_path.nil?
end

#from_memory?Boolean

Check if file data is in memory

Returns:

  • (Boolean)

    true if data is in memory



50
51
52
# File 'lib/cabriolet/file_entry.rb', line 50

def from_memory?
  !@data.nil?
end

#mtimeTime

Get modification time

Returns:

  • (Time)

    Modification time (current time for memory files)



84
85
86
87
88
# File 'lib/cabriolet/file_entry.rb', line 84

def mtime
  return Time.now if from_memory?

  stat&.mtime || Time.now
end

#read_dataString

Read file data (from disk or memory)

Returns:

  • (String)

    File contents



57
58
59
60
61
# File 'lib/cabriolet/file_entry.rb', line 57

def read_data
  return @data if from_memory?

  File.binread(@source_path)
end

#sizeInteger

Get file size

Returns:

  • (Integer)

    File size in bytes



66
67
68
69
70
# File 'lib/cabriolet/file_entry.rb', line 66

def size
  return @data.bytesize if from_memory?

  File.size(@source_path)
end

#statFile::Stat?

Get file stat (disk files only)

Returns:

  • (File::Stat, nil)

    File stat or nil for memory files



75
76
77
78
79
# File 'lib/cabriolet/file_entry.rb', line 75

def stat
  return nil if from_memory?

  File.stat(@source_path)
end