Class: EacRubyUtils::Fs::Logs

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_ruby_utils/fs/logs.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#[](label) ⇒ Object


13
14
15
# File 'lib/eac_ruby_utils/fs/logs.rb', line 13

def [](label)
  log_set.fetch(sanitize_label(label))
end

#add(label) ⇒ Object


17
18
19
20
21
# File 'lib/eac_ruby_utils/fs/logs.rb', line 17

def add(label)
  log_set[sanitize_label(label)] = ::EacRubyUtils::Fs::Temp.file

  self
end

#remove(label) ⇒ Object


29
30
31
32
# File 'lib/eac_ruby_utils/fs/logs.rb', line 29

def remove(label)
  log_set.fetch(sanitize_label(label)).remove
  log_set.delete(sanitize_label(label))
end

#remove_allObject


23
24
25
26
27
# File 'lib/eac_ruby_utils/fs/logs.rb', line 23

def remove_all
  log_set.each_key { |label| remove(label) }

  self
end

#truncate(label, length = TRUNCATE_DEFAULT_LENGTH) ⇒ Object


34
35
36
37
38
39
40
# File 'lib/eac_ruby_utils/fs/logs.rb', line 34

def truncate(label, length = TRUNCATE_DEFAULT_LENGTH)
  content = self[label].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_all(length = TRUNCATE_DEFAULT_LENGTH) ⇒ Object


42
43
44
45
46
47
48
49
50
# File 'lib/eac_ruby_utils/fs/logs.rb', line 42

def truncate_all(length = TRUNCATE_DEFAULT_LENGTH)
  s = "Files: #{log_set.length}\n"
  log_set.each do |label, path|
    x = truncate(label, length)
    y = [label, path, ::Filesize.from("#{path.size} B").pretty].join(' / ')
    s += x.blank? ? ">>> #{y} (Blank) <<<" : ">>> #{y}\n#{x}\n<<< #{y}\n"
  end
  s
end