Module: Drakkon::Utils::DowncaseNormalize
- Defined in:
- lib/drakkon/lib/utils/rename_normalize.rb
Overview
This downcases all the things
Class Method Summary collapse
-
.files ⇒ Object
Select all local dir files.
-
.process(file) ⇒ Object
Image file name normalization.
- .prompt ⇒ Object
- .run!(_args = nil) ⇒ Object
- .start ⇒ Object
Class Method Details
.files ⇒ Object
Select all local dir files
64 65 66 |
# File 'lib/drakkon/lib/utils/rename_normalize.rb', line 64 def self.files Dir["#{Dir.pwd}/*"] end |
.process(file) ⇒ Object
Image file name normalization. Downcase and replace spaces with underscore, commas, and double underscores
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/drakkon/lib/utils/rename_normalize.rb', line 42 def self.process(file) new_name = File.basename(file).downcase.gsub(' ', '_').gsub('(', '_').gsub(')', '_').gsub('-', '_').gsub(',', '_').gsub('__', '_').gsub( '__', '_' ) path = File.dirname(file) # Remove Final Underscore last char of basename is an underscore ext = File.extname(new_name) basename = File.basename(new_name, ext) if basename[-1] == '_' && basename.length > 1 new_name = basename[0..-2] new_name += ext end # Skip if the same return if new_name == File.basename(file) LogBot.info('Rename Files Normalize', "Old: #{File.basename(file)}, New: #{new_name}") FileUtils.mv(file, "#{path}/#{new_name}") end |
.prompt ⇒ Object
68 69 70 |
# File 'lib/drakkon/lib/utils/rename_normalize.rb', line 68 def self.prompt TTY::Prompt.new(active_color: :cyan, interrupt: :exit) end |
.run!(_args = nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/drakkon/lib/utils/rename_normalize.rb', line 5 def self.run!(_args = nil) puts <<~HELP Usage: Run in the directory you wish to rename files - Downcase - Normalize - To Underscores: - Space - Perentheses - Dash - Remove Double Underscores - Remove Commas - Delete Final Chars - If Underscore Current Directory; #{Dir.pwd.pastel(:yellow)} Files: HELP files.each do |img| img_s = img.gsub("#{Dir.pwd}/", '').pastel(:yellow) puts " - #{img_s}" end puts exit unless prompt.yes? "#{'Are you sure?'.pastel(:bright_yellow)} Rename!? #{'(Destructive)'.pastel(:red)}" start end |
.start ⇒ Object
35 36 37 38 39 |
# File 'lib/drakkon/lib/utils/rename_normalize.rb', line 35 def self.start files.each do |file| process(file) end end |