module FoldersRenamer

Constants

CustomError
VERSION

Public Class Methods

rename(options) click to toggle source
# File lib/folders_renamer/folders_renamer.rb, line 4
def rename(options)
  base_dir   = File.expand_path(options[:base_dir])
  sep_string = options[:sep_string]
  commit     = options[:commit]

  unless File.directory?(base_dir) && File.readable?(base_dir)
    fail "#{base_dir} is not a valid directory or not readable!"
  end

  find_depth_first(Pathname(base_dir)) do |path|
    if path.directory?
      new_path = path.dirname + Pathname(FilenameCleaner.sanitize(path.basename.to_s, sep_string))
      if new_path != path && !File.exist?(new_path)
        puts "FYI: rename from: #{path}"
        puts "FYI: rename to  : #{new_path}"
        path.rename(new_path) if commit
      else
        puts "FYI: skip folder: #{path}"
      end
    end
  end
  unless commit
    puts "---------------------------------------------------------------------"
    puts "This is the dry run only, use --commit to make your changes permanent"
    puts "---------------------------------------------------------------------"
  end
end

Private Class Methods

find_depth_first(pathname) { |path| ... } click to toggle source
# File lib/folders_renamer/folders_renamer.rb, line 34
def find_depth_first(pathname)
  acc = []
  pathname.find { |file| acc << file }
  acc.reverse!
  acc.each { |path| yield path }
end