class ChangeFile
Class responsible of the logic of making changes process.¶ ↑
Public Class Methods
new(phrase_we_have_now)
click to toggle source
# File phrases_changer.rb, line 137 def initialize phrase_we_have_now @phrase_we_have_now = phrase_we_have_now @change_all = false end
Public Instance Methods
ask_for_several_change_in_file(line_to_change, file_path)
click to toggle source
This method ask you for confirmation if you want to make indicated change. @return Boolean
# File phrases_changer.rb, line 160 def ask_for_several_change_in_file line_to_change, file_path if @change_all puts "\n\e[39m******************************************" puts '* *' puts '* Automatically changes has been started *' puts '* *' puts "\nFile: #{file_path}\n\n" return true end puts "\n\e[39m******************************************" puts '* *' puts '* Follow the instructions below to *' puts '* make changes in file *' puts "\nPhrase:\n #{line_to_change}\nfound in file:\n#{file_path}\n" print "\e[31m\e[1;4m\nDo I have to change this phrase [y/n/all] (default \e[32m\e[1;4mYes\e[31m) ?\n" choise = STDIN.gets.chomp.upcase res = false if choise == 'Y' || choise.length == 0 res = true elsif choise == 'A' res = true @change_all = true end res end
iterate_several_file(file_path)
click to toggle source
Return array which contains lines with searched phrase for indicated file.¶ ↑
@return Array
# File phrases_changer.rb, line 144 def iterate_several_file file_path #Iterate file line by line result_lines_in_file = [] reg = /.*#{@phrase_we_have_now}.*/ file = File.open(file_path, "r") do |f| f.each_line do |line| if line.match?(reg) result_lines_in_file << line end end end result_lines_in_file end
make(line_to_change, phrase_we_have_now, phrase_we_want_to_have, file_path)
click to toggle source
Returns bool value for info if changes made correctly or rather problem occurs. @return Boolean
# File phrases_changer.rb, line 188 def make line_to_change, phrase_we_have_now, phrase_we_want_to_have, file_path verbose = $conf[:verbose] if verbose puts "\e[39m-----------------------------" puts "I'm changing:\n#{phrase_we_have_now}" puts '-----------------------------' puts "to:\n#{phrase_we_want_to_have}" puts '-----------------------------' end #Change every occurence puts "in file:\n#{file_path}" if verbose data = File.read(file_path) puts "\n\e[31m++Old version: \e[30m\n" if verbose puts data + "\n" if verbose line_changed = line_to_change.gsub(phrase_we_have_now, phrase_we_want_to_have) data.sub! line_to_change, line_changed puts "\n\e[32m++New version: \e[30m\n" if verbose puts data + "\n" if verbose puts "\e[31mOld line:\n #{line_to_change}\e[32m\nNew line:\n#{line_changed}\e[30m" #Standard info. verbose = true -> shows all file #Write the file res = false res = File.open(file_path, "w") do |f| f.write(data) end if res return true end false end