Class: Kitkat::FileInfo
- Inherits:
-
Object
- Object
- Kitkat::FileInfo
- Defined in:
- lib/kitkat/file_info.rb
Overview
File-level operations.
Constant Summary collapse
- BLANK =
''
- MIME_TYPE_SEPARATOR =
'/'
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #bytesize ⇒ Object
-
#digest ⇒ Object
Important note: Calling this on a directory will result in a blank string.
- #full_mime_type ⇒ Object
-
#initialize(path, root: BLANK) ⇒ FileInfo
constructor
A new instance of FileInfo.
- #last_modified_at ⇒ Object
- #mime_subtype ⇒ Object
- #mime_type ⇒ Object
- #relative_path ⇒ Object
Constructor Details
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/kitkat/file_info.rb', line 9 def path @path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
9 10 11 |
# File 'lib/kitkat/file_info.rb', line 9 def root @root end |
Instance Method Details
#bytesize ⇒ Object
44 45 46 |
# File 'lib/kitkat/file_info.rb', line 44 def bytesize File.size(path) end |
#digest ⇒ Object
Important note: Calling this on a directory will result in a blank string.
36 37 38 |
# File 'lib/kitkat/file_info.rb', line 36 def digest File.directory?(path) ? BLANK : Digest::SHA256.file(path).hexdigest end |
#full_mime_type ⇒ Object
20 21 22 23 24 25 |
# File 'lib/kitkat/file_info.rb', line 20 def full_mime_type @full_mime_type ||= IO.popen( ['file', '--brief', '--mime-type', path], in: :close, err: :close ) { |io| io.read.chomp }.to_s end |
#last_modified_at ⇒ Object
40 41 42 |
# File 'lib/kitkat/file_info.rb', line 40 def last_modified_at File.mtime(path).utc end |
#mime_subtype ⇒ Object
31 32 33 |
# File 'lib/kitkat/file_info.rb', line 31 def mime_subtype full_mime_type.split(MIME_TYPE_SEPARATOR).last end |
#mime_type ⇒ Object
27 28 29 |
# File 'lib/kitkat/file_info.rb', line 27 def mime_type full_mime_type.split(MIME_TYPE_SEPARATOR).first end |
#relative_path ⇒ Object
16 17 18 |
# File 'lib/kitkat/file_info.rb', line 16 def relative_path path.delete_prefix(root).delete_prefix(File::SEPARATOR) end |