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
14 15 16 |
# File 'lib/eac_fs/logs/file.rb', line 14 def clean file.truncate(0) if file.exist? end |
#file_size ⇒ Object
18 19 20 |
# File 'lib/eac_fs/logs/file.rb', line 18 def file_size file.file? ? file.size : 0 end |
#pretty_file_size ⇒ Object
22 23 24 |
# File 'lib/eac_fs/logs/file.rb', line 22 def pretty_file_size ::Filesize.from("#{file_size} B").pretty end |
#truncate(length = TRUNCATE_DEFAULT_LENGTH) ⇒ String
28 29 30 31 32 33 34 |
# File 'lib/eac_fs/logs/file.rb', line 28 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
38 39 40 41 42 43 44 |
# File 'lib/eac_fs/logs/file.rb', line 38 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 |