-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtarget_file.rb
More file actions
52 lines (38 loc) · 966 Bytes
/
target_file.rb
File metadata and controls
52 lines (38 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'fileutils'
require 'tempfile'
require './find_x_by_syntax.rb'
module Rails4UpgradeSyntax
class TargetFile
include Rails4UpgradeSyntax::FindXBySyntax
def initialize(file)
@changed = 0
@file = file
end
def update
puts "processing #{@file}..."
temp_file = Tempfile.new("tempfile_#{Time.now.to_i}")
File.open(@file, 'r') do |f|
f.each_line do |line|
new_line = process_line(line)
@changed += 1 unless new_line == line
temp_file.puts new_line
end
temp_file.close
end
FileUtils.mv(temp_file.path, @file)
temp_file.unlink
yield(@changed) if @changed > 0
end
private
def process_line(line)
process_list.inject(line){|k, m| method(m).call(k)}
end
def process_list
[
:process_find_by,
:process_find_or_initialize_by,
:process_find_or_create_by,
]
end
end
end