class PhrasesChanger

This class manage of process: searches and iterate files, save changes to logs.

Regarding to the logic of making changes in files is reposnible class ChangeFile located in the same file.

Constants

DIRECTORY_PATH
LOG_PATH

Public Class Methods

new(phrase_we_have_now, phrase_we_want_to_have) click to toggle source
# File phrases_changer.rb, line 40
def initialize phrase_we_have_now, phrase_we_want_to_have
  @phrase_we_have_now = phrase_we_have_now
  @phrase_we_want_to_have = phrase_we_want_to_have
  main
end

Public Instance Methods

append_to_log(log_hash, file_before, file_after) click to toggle source

@return void

# File phrases_changer.rb, line 108
def append_to_log log_hash, file_before, file_after
  raise "log_hash is not a Hash!" unless log_hash.is_a?(Hash)
  data = "\n\n+++++++++++++++++++++++++++++++++ #{Time.now.to_s} ++++++++++++++++++++++++++++++++++\n"
  data += "#{log_hash[:file_path]}\n"
  data += "BEFORE: \n" + file_before + "\n----------------------------------------------\nAFTER:\n" + file_after
  File.open(File.join(LOG_PATH, adjust_log_name(log_hash[:phrase_we_have_now])), "a") do |f|
    f.write(data)
  end
end
file_content(file_path) click to toggle source

@return String

# File phrases_changer.rb, line 119
def file_content file_path
  data = File.read(file_path)
end
main() click to toggle source

@return void

# File phrases_changer.rb, line 48
def main
  files_list = search_in_project @phrase_we_have_now
  objChangeFile = ChangeFile.new @phrase_we_have_now
  files_list.each do |file_path|
    file_before = file_content file_path
    log_hash = { file_path: file_path, phrase_we_have_now: @phrase_we_have_now }
    several_file_phrases = objChangeFile.iterate_several_file file_path
    several_file_phrases.each do |line_to_change|
      let_me_change = objChangeFile.ask_for_several_change_in_file line_to_change, file_path
      if let_me_change
        make_check = objChangeFile.make line_to_change, @phrase_we_have_now, @phrase_we_want_to_have, file_path
        raise "The problem with 'make' function occurs!" unless make_check
        puts "\n\e[39mThe phrase \e[31m'#{@phrase_we_have_now}' \e[39mis currently changed to \e[32m'#{@phrase_we_want_to_have}'\e[34m in file:\e[30m"
        puts "#{file_path}\n\n"
      else
        puts "No file has been changed\n\n"
      end
    end
    #If you will not stop the script and finish the changes, you will have log with all changes.
    #Otherwise log file will not created.
    puts "\e[39m*                                        *"
    puts '*            Process complete            *'
    puts '*    You can see changes in log file     *'
    puts "******************************************\n"
    puts "Log file path: #{File.join(LOG_PATH, adjust_log_name(@phrase_we_have_now))}\n\n\n\n\n\n\n"
    file_after = file_content file_path
    append_to_log log_hash, file_before, file_after
  end
end
search_in_project(phrase_we_have_now) click to toggle source

Return files where occurs: phrase_we_have_now

@return Array

# File phrases_changer.rb, line 80
def search_in_project phrase_we_have_now
  result_files_with_phrase = []
  path_to_files = File.join(DIRECTORY_PATH, '**/*.rb')
  files_to_check = []
  Dir.glob(path_to_files) do |rb_file|
    files_to_check << rb_file
  end
  raise "files_to_check is empty !" if files_to_check.length == 0
  #Looking for files where occurs: phrase_we_have_now
  files_to_check.each do |one_file|
    file = File.open(one_file, "r") do |f|
      f.each_line do |line|
        reg = /.*#{@phrase_we_have_now}.*/
        if line.match?(reg)
          result_files_with_phrase << one_file
        end
      end
    end
  end
  if result_files_with_phrase.length == 0
    puts "\n\e[31m\e[1;4mThe phrase: '#{@phrase_we_have_now}' not found in files.\e[31m"
    exit
  end
  result_files_with_phrase.uniq.sort
end

Private Instance Methods

adjust_log_name(name) click to toggle source

@return String

# File phrases_changer.rb, line 126
def adjust_log_name name
  change = name.strip.gsub!(':', '_')
  return change if change
  return name
end