Sha256: cf008cf389aa182aed528fd41aa9509b78a67d82ebf298017c25284975a02d2b

Contents?: true

Size: 1.42 KB

Versions: 142

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacFs
  class Logs
    class File
      enable_simple_cache
      common_constructor :label

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

      delegate :remove, to: :file

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

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

      def pretty_file_size
        ::Filesize.from("#{file_size} B").pretty
      end

      # @param length [Integer]
      # @return [String]
      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

      # @param length [Integer]
      # @return [String]
      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

      protected

      # @return [EacRubyUtils::Fs::Temp::File
      def file_uncached
        ::EacRubyUtils::Fs::Temp.file
      end
    end
  end
end

Version data entries

142 entries across 142 versions & 3 rubygems

Version Path
eac_fs-0.18.1 lib/eac_fs/logs/file.rb
eac_fs-0.18.0 lib/eac_fs/logs/file.rb
eac_fs-0.17.0 lib/eac_fs/logs/file.rb
eac_tools-0.76.0 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.75.2 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.75.1 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.75.0 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.74.1 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.74.0 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.73.0 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.72.0 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.70.1 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.70.0 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.69.1 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.69.0 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.68.0 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.67.1 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.67.0 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.66.0 sub/eac_fs/lib/eac_fs/logs/file.rb
eac_tools-0.65.1 sub/eac_fs/lib/eac_fs/logs/file.rb