class Deblank::Application
The main program. It’s run! method is called if the script is run from the command line. It parses the command line arguments and renames the files.
Constants
- ERRORCODE
Public Class Methods
new()
click to toggle source
# File lib/deblank.rb, line 186 def initialize begin options = Optionparser.parse!(ARGV) rescue => e usage_fail(e.message) end @files = options[:files] @simulate = options[:simulate] @converter = NameConverter.new end
Public Instance Methods
run!()
click to toggle source
The main program.
# File lib/deblank.rb, line 198 def run! message = "This is a dry run, files will not be renamed." warn "#{message}\n#{'-' * message.size}\n" if @simulate @files.each do |filename| next unless file_exist?(filename) next unless invalid?(filename) new_filename = @converter.convert(filename) secure_rename(filename, new_filename) end end
Private Instance Methods
confirm(question)
click to toggle source
Asks for yes or no (y/n).
question
- string to be printed
Returns true
if the answer is yes.
# File lib/deblank.rb, line 245 def confirm(question) loop do $stderr.print "#{question} [y/n] " reply = $stdin.gets.chomp.downcase # $stdin avoids gets/ARGV problem return reply == 'y' if /\A[yn]\Z/ =~ reply warn "Please answer `y' or `n'." end end
file_exist?(filename)
click to toggle source
# File lib/deblank.rb, line 217 def file_exist?(filename) fail_message = "There is no file `#{filename}'." File.exist?(filename) or skip_warn(fail_message) end
invalid?(filename)
click to toggle source
# File lib/deblank.rb, line 223 def invalid?(filename) fail_message = "`#{filename}' already is a valid filename." @converter.invalid?(filename) or skip_warn(fail_message) end
overwrite?(filename)
click to toggle source
# File lib/deblank.rb, line 236 def overwrite?(filename) confirm("File `#{filename}' already exists. Overwrite?") end
secure_rename(old_filename, new_filename)
click to toggle source
# File lib/deblank.rb, line 229 def secure_rename(old_filename, new_filename) return if File.exist?(new_filename) && !overwrite?(new_filename) warn "Moving from `#{old_filename}' to `#{new_filename}'." File.rename(old_filename, new_filename) unless @simulate end
skip_warn(message)
click to toggle source
# File lib/deblank.rb, line 213 def skip_warn(message) warn "#{message} (Skipped.)" end
usage_fail(message)
click to toggle source
Prints an error message and a short help information, then exits.
# File lib/deblank.rb, line 255 def usage_fail(message) warn "#{PROGNAME}: #{message}" warn "Use `#{PROGNAME} --help' for valid options." exit ERRORCODE[:usage] end