Class: Cabriolet::FileEntry
- Inherits:
-
Object
- Object
- Cabriolet::FileEntry
- 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.
Instance Attribute Summary collapse
-
#archive_path ⇒ Object
readonly
Returns the value of attribute archive_path.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
Instance Method Summary collapse
-
#attributes ⇒ Integer
Get file attributes.
-
#compress? ⇒ Boolean
Get compression flag from options.
-
#from_disk? ⇒ Boolean
Check if file data is from disk.
-
#from_memory? ⇒ Boolean
Check if file data is in memory.
-
#initialize(archive_path:, source: nil, data: nil, **options) ⇒ FileEntry
constructor
Initialize a file entry.
-
#mtime ⇒ Time
Get modification time.
-
#read_data ⇒ String
Read file data (from disk or memory).
-
#size ⇒ Integer
Get file size.
-
#stat ⇒ File::Stat?
Get file stat (disk files only).
Constructor Details
#initialize(archive_path:, source: nil, data: nil, **options) ⇒ FileEntry
Initialize a file entry
31 32 33 34 35 36 37 38 |
# File 'lib/cabriolet/file_entry.rb', line 31 def initialize(archive_path:, source: nil, data: nil, **) @source_path = source @data = data @archive_path = archive_path = validate! end |
Instance Attribute Details
#archive_path ⇒ Object (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 |
#data ⇒ Object (readonly)
Returns the value of attribute data.
22 23 24 |
# File 'lib/cabriolet/file_entry.rb', line 22 def data @data end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
22 23 24 |
# File 'lib/cabriolet/file_entry.rb', line 22 def end |
#source_path ⇒ Object (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
#attributes ⇒ Integer
Get file attributes
93 94 95 96 97 98 |
# File 'lib/cabriolet/file_entry.rb', line 93 def attributes return [:attributes] if [:attributes] return Constants::ATTRIB_ARCH if from_memory? calculate_disk_attributes end |
#compress? ⇒ Boolean
Get compression flag from options
103 104 105 |
# File 'lib/cabriolet/file_entry.rb', line 103 def compress? .fetch(:compress, true) end |
#from_disk? ⇒ Boolean
Check if file data is from 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
50 51 52 |
# File 'lib/cabriolet/file_entry.rb', line 50 def from_memory? !@data.nil? end |
#mtime ⇒ Time
Get modification time
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_data ⇒ String
Read file data (from disk or memory)
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 |
#size ⇒ Integer
Get file size
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 |
#stat ⇒ File::Stat?
Get file stat (disk files only)
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 |