Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AnnotateRoutes.remove_annotations #715

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/annotate/annotate_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def remove_annotations(_options={})
existing_text = File.read(routes_file)
content, header_position = strip_annotations(existing_text)
new_content = strip_on_removal(content, header_position)
if rewrite_contents(existing_text, new_content)
new_text = new_content.join("\n")

if rewrite_contents(existing_text, new_text)
puts "Removed annotations from #{routes_file}."
end
end
Expand Down Expand Up @@ -145,18 +147,17 @@ def strip_on_removal(content, header_position)
content.pop while content.last == ''
end

# Make sure we end on a trailing newline.
content << '' unless content.last == ''

# TODO: If the user buried it in the middle, we should probably see about
# TODO: preserving a single line of space between the content above and
# TODO: below...
content
end

# @param [String, Array<String>]
def rewrite_contents(existing_text, new_content)
# Make sure we end on a trailing newline.
new_content << '' unless new_content.last == ''
new_text = new_content.join("\n")

def rewrite_contents(existing_text, new_text)
if existing_text == new_text
puts "#{routes_file} unchanged."
false
Expand Down