Skip to content

Commit b11928e

Browse files
committed
Replacing old block with new one.
1 parent 2eb9714 commit b11928e

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

lib/annotate/annotate_models.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,17 @@ def annotate_one_file(file_name, info_block, position, options={})
241241
# end
242242
# =======
243243

244-
# Strip the old schema info, and insert new schema info.
245-
old_content.sub!(encoding, '')
246-
old_content.sub!(PATTERN, '')
247-
248-
new_content = options[position].to_s == 'after' ?
249-
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
250-
(encoding_header + info_block + "\n" + old_content)
244+
if PATTERN.match(old_content)
245+
new_content = old_content.sub(PATTERN, info_block)
246+
else
247+
# Strip the old schema info, and insert new schema info.
248+
old_content.sub!(encoding, '')
249+
old_content.sub!(PATTERN, '')
250+
251+
new_content = options[position].to_s == 'after' ?
252+
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
253+
(encoding_header + info_block + "\n" + old_content)
254+
end
251255

252256
File.open(file_name, "wb") { |f| f.puts new_content }
253257
return true

0 commit comments

Comments
 (0)