class MassRenamer::Driver

Public Class Methods

new(opts) click to toggle source
# File lib/mass_renamer/driver.rb, line 108
def initialize opts
  @opts = opts
  validate_environment!
end

Public Instance Methods

do_renames!(renames) click to toggle source
# File lib/mass_renamer/driver.rb, line 123
def do_renames! renames
  fd = Filesystem_Driver.new(
    ask: @opts[:ask],
    dry: @opts[:dry],
    verbose: @opts[:verbose],
    no_delete: @opts[:no_delete],
    force: @opts[:force]
  )

  renames.each do |from, to|
    from = File.expand_path from
    to.map!(&File.method(:expand_path))

    begin
      case to.length
      when 0
        fd.remove! from
      when 1
        to = to.first
        next if from == to
        fd.move! from, to
      else
        to.each { |dst| fd.copy! from, dst unless from == dst }
        fd.remove! from unless to.include? from
      end
    rescue
      next if @opts[:keep_going]
      STDERR.puts "Something failed, stopping."
      exit 1
    end
  end
end
invoke_editor(files) click to toggle source
# File lib/mass_renamer/driver.rb, line 156
def invoke_editor files
  tmpfile = Tempfile.new
  begin
    tmpfile.write generate_file_to_edit(files)
    tmpfile.close
    unless system(@opts[:editor], tmpfile.path)
      raise "Cannot open editor (not in path?). Fix it."
    end
    tmpfile.open
    parse_renames(tmpfile.read)
  ensure
    tmpfile.close
    tmpfile.unlink
  end
end
rename!() click to toggle source
# File lib/mass_renamer/driver.rb, line 113
def rename!
  files = gather_files(
    @opts[:dir],
    recursive: @opts[:recursive],
    filter: @opts[:filter]
  )
  renames = invoke_editor files
  do_renames! renames
end
validate_environment!() click to toggle source
# File lib/mass_renamer/driver.rb, line 172
def validate_environment!
  raise IOError, "Directory does not exist." unless File.exist? @opts[:dir]
end