class FileRenamer::PathProcessor
Attributes
counter[RW]
directory[R]
extension[R]
name[R]
name_alterer[R]
paths[R]
prefix[R]
Public Class Methods
new(args)
click to toggle source
# File lib/path_processor.rb, line 21 def initialize(args) params = args[:corrected_params] || ParamsCorrector.new.corrected_params(args[:params]) @extension = params[:ext] @name = params[:name] @directory = params[:dir] @prefix = params[:prefix] @name_alterer = args.fetch(:name_alterer, NameAlterer.new) @paths = paths @counter = 0 end
run!(args = {})
click to toggle source
# File lib/path_processor.rb, line 17 def self.run!(args = {}) self.new(args).rename_files! end
Public Instance Methods
rename_files!()
click to toggle source
# File lib/path_processor.rb, line 34 def rename_files! paths.each do |path| process_path(path) if should_be_renamed?(path) end end
Private Instance Methods
is_file?(path)
click to toggle source
# File lib/path_processor.rb, line 74 def is_file?(path) !File.directory?(path) end
matches_pattern?(path)
click to toggle source
# File lib/path_processor.rb, line 70 def matches_pattern?(path) sliced_filename(path).match?(Regexp.new("^#{prefix}.*#{extension}$")) end
process_path(path)
click to toggle source
# File lib/path_processor.rb, line 42 def process_path(path) old_filename = sliced_filename(path) renamed_filename = name_alterer.renamed_filename({ filename: old_filename, number: counter, new_name: name }) renamed_path = renamed_path(path, old_filename, renamed_filename) rename_file!(path, renamed_path) self.counter += 1 end
rename_file!(path, renamed_path)
click to toggle source
# File lib/path_processor.rb, line 54 def rename_file!(path, renamed_path) File.rename(path, renamed_path) end
renamed_path(path, filename, new_filename)
click to toggle source
# File lib/path_processor.rb, line 58 def renamed_path(path, filename, new_filename) path.gsub(filename, new_filename) end
should_be_renamed?(path)
click to toggle source
# File lib/path_processor.rb, line 62 def should_be_renamed?(path) matches_pattern?(path) && is_file?(path) end
sliced_filename(path)
click to toggle source
# File lib/path_processor.rb, line 78 def sliced_filename(path) path.slice(%r{([^/|\\]+$)}) end