class RubyEdit::Writer
Constants
- LINE_REGEX
Public Instance Methods
write()
click to toggle source
# File lib/ruby_edit/writer.rb, line 11 def write File.open(RubyEdit::SOURCE_FILE_LOCATION, 'rb') do |file| file.each_line { |line| find_and_replace_line line } end end
Private Instance Methods
find_and_replace_line(new_line)
click to toggle source
# File lib/ruby_edit/writer.rb, line 19 def find_and_replace_line(new_line) @temp_file = Tempfile.new('temp_file') split_file_and_line(new_line) @new_line = new_line.sub LINE_REGEX, '' begin replace_line replace_file ensure @temp_file.close @temp_file.unlink end end
replace_file()
click to toggle source
Replaces the old file, with the new edited file
# File lib/ruby_edit/writer.rb, line 49 def replace_file @temp_file.close FileUtils.mv(@temp_file.path, @file_name) end
replace_line()
click to toggle source
Replaces the line in the original file, with the line from the sourcefile
# File lib/ruby_edit/writer.rb, line 39 def replace_line File.open(@file_name, 'r+') do |file| file.each_with_index do |line, index| output = index + 1 == @line_number.to_i ? @new_line : line @temp_file.puts output end end end
split_file_and_line(line)
click to toggle source
# File lib/ruby_edit/writer.rb, line 32 def split_file_and_line(line) @file_name, @line_number = line.match(LINE_REGEX)[0].split(':') rescue NoMethodError false end