Class: SpreenClean::Application
- Inherits:
-
Object
- Object
- SpreenClean::Application
- 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
-
#dirname ⇒ String
readonly
: String.
-
#io ⇒ IO
readonly
: IO.
-
#mode ⇒ String
readonly
: String.
-
#pattern ⇒ String
readonly
: String.
Class Method Summary collapse
Instance Method Summary collapse
- #absolute_dirname ⇒ String
- #announce_empty ⇒ void
- #announce_finish ⇒ void
- #announce_start ⇒ void
- #clean_files ⇒ void
- #exec_mode ⇒ String
-
#files ⇒ Array[String]
Matched lazily so validation can refuse a dirname before any globbing happens.
-
#initialize(dirname: '.', pattern: '*', mode: 'd', io: $stdout) ⇒ Application
constructor
A new instance of Application.
- #output(message) ⇒ void
- #run ⇒ void
-
#validate_dirname! ⇒ void
Refuses filesystem roots (
/,C:/, ...) so a strayfile-cleancan never sweep an entire drive. - #validate_mode! ⇒ void
Constructor Details
#initialize(dirname: '.', pattern: '*', mode: 'd', io: $stdout) ⇒ Application
Returns a new instance of Application.
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
#dirname ⇒ String (readonly)
: String
74 75 76 |
# File 'lib/spreen_clean/application.rb', line 74 def dirname @dirname end |
#io ⇒ IO (readonly)
: IO
77 78 79 |
# File 'lib/spreen_clean/application.rb', line 77 def io @io end |
#mode ⇒ String (readonly)
: String
76 77 78 |
# File 'lib/spreen_clean/application.rb', line 76 def mode @mode end |
#pattern ⇒ String (readonly)
: 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.
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_dirname ⇒ String
80 81 82 |
# File 'lib/spreen_clean/application.rb', line 80 def absolute_dirname @absolute_dirname ||= File.absolute_path(dirname) end |
#announce_empty ⇒ void
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_finish ⇒ void
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_start ⇒ void
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_files ⇒ void
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_mode ⇒ String
108 109 110 |
# File 'lib/spreen_clean/application.rb', line 108 def exec_mode @exec_mode ||= mode == 'e' ? 'EXECUTION' : 'DRY RUN' end |
#files ⇒ Array[String]
Matched lazily so validation can refuse a dirname before any globbing happens.
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.
114 115 116 |
# File 'lib/spreen_clean/application.rb', line 114 def output() io.puts end |
#run ⇒ void
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 |