Method: FileEditor#write_to_file

Defined in:
lib/file_editor.rb

#write_to_file(read_handle, write_handle) ⇒ Object

Testable write method, using io handles as arguments to enable the use of String.IO in testing



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/file_editor.rb', line 49

def write_to_file(read_handle, write_handle)
	if is_multiline(@regex.to_s) 
		file_string = read_handle.read #no sense doing a line-by-line multiline
		if (@global)
			write_handle.puts(file_string.gsub(@regex, @substitution_string))
		else
			write_handle.puts(file_string.sub(@regex, @substitution_string))
		end
	else
		if (@global)
			read_handle.each do |line|
				write_handle.puts (line.gsub(@regex, @substitution_string))
			end
		else
			read_handle.each do |line|
				write_handle.puts (line.sub(@regex, @substitution_string))
			end		
		end
	end
end