Class: Rutaci::Renamer

Inherits:
Object
  • Object
show all
Defined in:
lib/rutaci/renamer.rb

Overview

renames files using a format string, uses the Getter it is a command executor, so it has tp provide an initializer which takes the options and a method ‘run’ which takes an array of filenames

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Renamer

Returns a new instance of Renamer.



27
28
29
30
31
32
33
34
35
36
# File 'lib/rutaci/renamer.rb', line 27

def initialize(options)
  @options = options
  @getter = Getter.new options
  if @options.interactive
    require 'rutaci/interaction'
    puts "in interactive mode you can edit the new filename before renaming"
    Interaction::print_lineedit_help
    puts
  end
end

Instance Method Details

#rename(file) ⇒ Object

get and return the actual info for a specific file



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rutaci/renamer.rb', line 46

def rename(file)
  puts "current file: \"#{file}\""
  new_name = File.join @options.destination, @getter.get(file).strip
  new_name = Interaction::lineedit "  new name: ", new_name if @options.interactive
  if new_name.empty?
    puts "  can't rename to an empty filename!"
    return
  end
  if @options.dry_run
    puts "  renaming to: \"#{new_name}\" (if we where not doing a dry-run)"
  else
    puts "  renaming to: \"#{new_name}\""
    File.makedirs File.dirname(new_name) # create missing directories
    File.move file, new_name
  end
end

#run(files) ⇒ Object

invoke this executor for an array of filenames



39
40
41
42
43
# File 'lib/rutaci/renamer.rb', line 39

def run(files)
  files.each do |file|
    self.rename(file)
  end
end