Class: EacFs::Logs::File
Constant Summary collapse
- TRUNCATE_DEFAULT_LENGTH =
1000
- TRUNCATE_APPEND_TEXT =
'(...) '
Instance Method Summary collapse
- #clean ⇒ Object
- #file_size ⇒ Object
- #pretty_file_size ⇒ Object
- #truncate(length = TRUNCATE_DEFAULT_LENGTH) ⇒ String
- #truncate_with_label(length = TRUNCATE_DEFAULT_LENGTH) ⇒ String
Instance Method Details
#clean ⇒ Object
16 17 18 |
# File 'lib/eac_fs/logs/file.rb', line 16 def clean file.truncate(0) if file.exist? end |
#file_size ⇒ Object
20 21 22 |
# File 'lib/eac_fs/logs/file.rb', line 20 def file_size file.file? ? file.size : 0 end |
#pretty_file_size ⇒ Object
24 25 26 |
# File 'lib/eac_fs/logs/file.rb', line 24 def pretty_file_size ::Filesize.from("#{file_size} B").pretty end |
#truncate(length = TRUNCATE_DEFAULT_LENGTH) ⇒ String
30 31 32 33 34 35 36 |
# File 'lib/eac_fs/logs/file.rb', line 30 def truncate(length = TRUNCATE_DEFAULT_LENGTH) content = file.file? ? file.read.strip : '' return content if content.length <= TRUNCATE_DEFAULT_LENGTH TRUNCATE_APPEND_TEXT + content[content.length - length + TRUNCATE_APPEND_TEXT.length, length - TRUNCATE_APPEND_TEXT.length] end |
#truncate_with_label(length = TRUNCATE_DEFAULT_LENGTH) ⇒ String
40 41 42 43 44 45 46 |
# File 'lib/eac_fs/logs/file.rb', line 40 def truncate_with_label(length = TRUNCATE_DEFAULT_LENGTH) header = [label, file, pretty_file_size].join(' / ') return ">>> #{header} (Not found) <<<" unless file.file? content = truncate(length) content.blank? ? ">>> #{header} (Blank) <<<" : ">>> #{header}\n#{content}\n<<< #{header}\n" end |