Class: EasyML::FileRotate

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_ml/support/file_rotate.rb

Instance Method Summary collapse

Constructor Details

#initialize(directory, files_to_keep) ⇒ FileRotate

Returns a new instance of FileRotate.



3
4
5
6
# File 'lib/easy_ml/support/file_rotate.rb', line 3

def initialize(directory, files_to_keep)
  @directory = directory
  @files_to_keep = files_to_keep
end

Instance Method Details

#cleanup(allowed_endings = %w[json])) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/easy_ml/support/file_rotate.rb', line 8

def cleanup(allowed_endings = %w[json])
  return unless @directory.present?

  allowed_patterns = allowed_endings.map { |ending| File.join(@directory, "**", "*#{ending}") }
  files_to_check = allowed_patterns.empty? ? Dir.glob(File.join(@directory, "**/*")) : Dir.glob(allowed_patterns)
  # Filter out directories
  files_to_check = files_to_check.select { |file| File.file?(file) }

  files_to_check.each do |file|
    FileUtils.chown_R(`whoami`.chomp, "staff", file)
    FileUtils.chmod_R(0o777, file)
    File.delete(file) if @files_to_keep.exclude?(file) && File.exist?(file)
  end
end