class Rename
Uses VariationsGenerator
output to first change all the occurences of the model name in the files, then to create new directories and rename files. Then uses MigrationGenerator
to create a migration
Constants
- ACCEPTABLE_FILE_TYPES
- DEFAULT_PATH
Public Class Methods
new(old_name, new_name, ignore_paths: [], path: DEFAULT_PATH)
click to toggle source
# File lib/model_renamer/rename.rb, line 15 def initialize old_name, new_name, ignore_paths: [], path: DEFAULT_PATH @variations_generator = VariationsGenerator.new(old_name, new_name) @ignore_paths = ignore_paths @path = path end
Public Instance Methods
generate_migrations()
click to toggle source
# File lib/model_renamer/rename.rb, line 44 def generate_migrations MigrationGenerator.new(@variations_generator.underscore_variations).create_migration_file end
rename_files_and_directories(outer_path = @path)
click to toggle source
# File lib/model_renamer/rename.rb, line 27 def rename_files_and_directories outer_path = @path Dir["#{outer_path}/*"].reject { |path| ignore_file? path }.each do |inner_path| if File.directory?(inner_path) rename_files_and_directories inner_path else rename_path inner_path end end rename_path outer_path end
rename_in_files()
click to toggle source
# File lib/model_renamer/rename.rb, line 38 def rename_in_files all_filepaths.each do |filepath| replace_all_variations_in_file filepath end end
run()
click to toggle source
# File lib/model_renamer/rename.rb, line 21 def run rename_files_and_directories @path rename_in_files generate_migrations end
Private Instance Methods
acceptable_filetype?(path)
click to toggle source
# File lib/model_renamer/rename.rb, line 70 def acceptable_filetype? path ACCEPTABLE_FILE_TYPES.include? path.split('.').last end
all_filepaths()
click to toggle source
# File lib/model_renamer/rename.rb, line 64 def all_filepaths Find.find(@path.presence || ".").to_a.reject do |path| FileTest.directory?(path) || !acceptable_filetype?(path) || ignore_file?(path) end end
ignore_file?(path)
click to toggle source
# File lib/model_renamer/rename.rb, line 74 def ignore_file? path @ignore_paths.any? do |ignore_path| path.include? ignore_path end end
rename_path(initial_filepath)
click to toggle source
# File lib/model_renamer/rename.rb, line 50 def rename_path initial_filepath current_filepath = initial_filepath variation_pairs.uniq.each do |old_name, new_name| next unless File.basename(current_filepath).include? old_name new_filepath = File.dirname(current_filepath) + "/#{File.basename(current_filepath).gsub(old_name, new_name)}" FileUtils.mv current_filepath, new_filepath current_filepath = new_filepath end end
replace_all_variations_in_file(filepath)
click to toggle source
# File lib/model_renamer/rename.rb, line 80 def replace_all_variations_in_file filepath variation_pairs.each do |variation| replace_in_file filepath, variation[0], variation[1] end end
replace_in_file(filepath, old_string, new_string)
click to toggle source
# File lib/model_renamer/rename.rb, line 86 def replace_in_file filepath, old_string, new_string old_text = File.read(filepath) if old_text.include? old_string new_text = old_text.gsub(old_string, new_string) File.open(filepath, "w") { |file| file.puts new_text } end end
variation_pairs()
click to toggle source
# File lib/model_renamer/rename.rb, line 60 def variation_pairs @variation_pairs ||= @variations_generator.pairs_to_convert end