Class: SpreenClean::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/spreen_clean/application.rb,
sig/generated/spreen_clean/application.rbs

Overview

Deletes files in a directory tree matching a glob pattern, with a dry-run mode that prints what would be removed without touching the filesystem.

Defined Under Namespace

Classes: InvalidModeError, RootDirnameError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirname: '.', pattern: '*', mode: 'd', io: $stdout) ⇒ Application

Returns a new instance of Application.

Parameters:

  • dirname: (String) (defaults to: '.')
  • pattern: (String) (defaults to: '*')
  • mode: (String) (defaults to: 'd')
  • io: (IO) (defaults to: $stdout)


30
31
32
33
34
35
# File 'lib/spreen_clean/application.rb', line 30

def initialize(dirname: '.', pattern: '*', mode: 'd', io: $stdout)
  @dirname = dirname
  @pattern = pattern
  @mode    = mode
  @io      = io
end

Instance Attribute Details

#dirnameString (readonly)

: String

Returns:

  • (String)


74
75
76
# File 'lib/spreen_clean/application.rb', line 74

def dirname
  @dirname
end

#ioIO (readonly)

: IO

Returns:

  • (IO)


77
78
79
# File 'lib/spreen_clean/application.rb', line 77

def io
  @io
end

#modeString (readonly)

: String

Returns:

  • (String)


76
77
78
# File 'lib/spreen_clean/application.rb', line 76

def mode
  @mode
end

#patternString (readonly)

: String

Returns:

  • (String)


75
76
77
# File 'lib/spreen_clean/application.rb', line 75

def pattern
  @pattern
end

Class Method Details

.run(dirname: '.', pattern: '*', mode: 'd', io: $stdout) ⇒ void

This method returns an undefined value.

Parameters:

  • dirname: (String) (defaults to: '.')
  • pattern: (String) (defaults to: '*')
  • mode: (String) (defaults to: 'd')
  • io: (IO) (defaults to: $stdout)


18
19
20
21
22
23
# File 'lib/spreen_clean/application.rb', line 18

def self.run(dirname: '.', pattern: '*', mode: 'd', io: $stdout)
  instance = new(dirname:, pattern:, mode:, io:)
  instance.validate_mode!
  instance.validate_dirname!
  instance.run
end

Instance Method Details

#absolute_dirnameString

Returns:

  • (String)


80
81
82
# File 'lib/spreen_clean/application.rb', line 80

def absolute_dirname
  @absolute_dirname ||= File.absolute_path(dirname)
end

#announce_emptyvoid

This method returns an undefined value.



85
86
87
# File 'lib/spreen_clean/application.rb', line 85

def announce_empty
  output "========== [#{exec_mode}] No #{pattern} Remains =========="
end

#announce_finishvoid

This method returns an undefined value.



102
103
104
105
# File 'lib/spreen_clean/application.rb', line 102

def announce_finish
  output "========== [#{exec_mode}] Cleaned #{pattern} =========="
  output "========== [#{exec_mode}] Total Cleaned File Count: #{files.length} =========="
end

#announce_startvoid

This method returns an undefined value.



90
91
92
93
# File 'lib/spreen_clean/application.rb', line 90

def announce_start
  output "========== [#{exec_mode}] Total File Count to Clean: #{files.length} =========="
  output "========== [#{exec_mode}] Start Cleaning #{pattern} =========="
end

#clean_filesvoid

This method returns an undefined value.



96
97
98
99
# File 'lib/spreen_clean/application.rb', line 96

def clean_files
  files.each { |file| output "========== [#{exec_mode}] Cleaning #{file} ==========" }
  FileUtils.rm_rf(files) if mode == 'e'
end

#exec_modeString

Returns:

  • (String)


108
109
110
# File 'lib/spreen_clean/application.rb', line 108

def exec_mode
  @exec_mode ||= mode == 'e' ? 'EXECUTION' : 'DRY RUN'
end

#filesArray[String]

Matched lazily so validation can refuse a dirname before any globbing happens.

Returns:

  • (Array[String])


58
59
60
# File 'lib/spreen_clean/application.rb', line 58

def files
  @files ||= Dir.glob(File.join(dirname, '**', pattern))
end

#output(message) ⇒ void

This method returns an undefined value.

Parameters:

  • message (String)


114
115
116
# File 'lib/spreen_clean/application.rb', line 114

def output(message)
  io.puts message
end

#runvoid

This method returns an undefined value.



63
64
65
66
67
68
69
70
# File 'lib/spreen_clean/application.rb', line 63

def run
  output "Target dirname is #{absolute_dirname}"
  return announce_empty if files.empty?

  announce_start
  clean_files
  announce_finish
end

#validate_dirname!void

This method returns an undefined value.

Refuses filesystem roots (/, C:/, ...) so a stray file-clean can never sweep an entire drive.



50
51
52
53
54
# File 'lib/spreen_clean/application.rb', line 50

def validate_dirname!
  return unless File.dirname(absolute_dirname) == absolute_dirname

  raise RootDirnameError, "#{absolute_dirname} is a filesystem root. Provide a narrower dirname."
end

#validate_mode!void

This method returns an undefined value.



38
39
40
41
42
43
44
45
# File 'lib/spreen_clean/application.rb', line 38

def validate_mode!
  case mode
  when 'd', 'e'
    nil
  else
    raise InvalidModeError, "#{mode} is invalid mode. Provide either `d`(default) or `e`."
  end
end