Class: EacFs::Logs::File

Inherits:
Object show all
Defined in:
lib/eac_fs/logs/file.rb

Constant Summary collapse

TRUNCATE_DEFAULT_LENGTH =
1000
TRUNCATE_APPEND_TEXT =
'(...) '

Instance Method Summary collapse

Instance Method Details

#cleanObject



16
17
18
# File 'lib/eac_fs/logs/file.rb', line 16

def clean
  file.truncate(0) if file.exist?
end

#file_sizeObject



20
21
22
# File 'lib/eac_fs/logs/file.rb', line 20

def file_size
  file.file? ? file.size : 0
end

#pretty_file_sizeObject



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

Parameters:

  • length (Integer) (defaults to: TRUNCATE_DEFAULT_LENGTH)

Returns:

  • (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

Parameters:

  • length (Integer) (defaults to: TRUNCATE_DEFAULT_LENGTH)

Returns:

  • (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