Skip to content

Commit 9a8db1b

Browse files
nard-techdrwl
authored andcommitted
Refactor AnnotateRoutes.routes_file_exist? (#716)
I refactored `AnnotateRoutes.routes_exists?` and methods using this. The points are as follows. * Removing `puts` in `AnnotateRoutes.routes_exists?` * Using `File.exist?` instead of `File.exists?` because `File.exists?` is deprecated * Renaming `AnnotateRoutes.routes_exists?` to `AnnotateRoutes.routes_file_exists?` in order to make the name of method more explanatory
1 parent 966bff0 commit 9a8db1b

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,34 @@ module AnnotateRoutes
2828

2929
class << self
3030
def do_annotations(options = {})
31-
return unless routes_exists?
32-
existing_text = File.read(routes_file)
33-
34-
if rewrite_contents_with_header(existing_text, header(options), options)
35-
puts "#{routes_file} annotated."
31+
if routes_file_exist?
32+
existing_text = File.read(routes_file)
33+
if rewrite_contents_with_header(existing_text, header(options), options)
34+
puts "#{routes_file} annotated."
35+
end
36+
else
37+
puts "Can't find routes.rb"
3638
end
3739
end
3840

3941
def remove_annotations(_options={})
40-
return unless routes_exists?
41-
existing_text = File.read(routes_file)
42-
content, header_position = strip_annotations(existing_text)
43-
new_content = strip_on_removal(content, header_position)
44-
new_text = new_content.join("\n")
45-
46-
if rewrite_contents(existing_text, new_text)
47-
puts "Removed annotations from #{routes_file}."
42+
if routes_file_exist?
43+
existing_text = File.read(routes_file)
44+
content, header_position = strip_annotations(existing_text)
45+
new_content = strip_on_removal(content, header_position)
46+
new_text = new_content.join("\n")
47+
if rewrite_contents(existing_text, new_text)
48+
puts "Removed annotations from #{routes_file}."
49+
end
50+
else
51+
puts "Can't find routes.rb"
4852
end
4953
end
5054

5155
private
5256

53-
def routes_exists?
54-
routes_exists = File.exists?(routes_file)
55-
puts "Can't find routes.rb" unless routes_exists
56-
57-
routes_exists
57+
def routes_file_exist?
58+
File.exist?(routes_file)
5859
end
5960

6061
def routes_file

spec/lib/annotate/annotate_routes_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def magic_comments_list_each
2828
end
2929

3030
it 'should check if routes.rb exists' do
31-
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(false)
31+
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(false)
3232
expect(AnnotateRoutes).to receive(:puts).with("Can't find routes.rb")
3333
AnnotateRoutes.do_annotations
3434
end
@@ -42,7 +42,7 @@ def magic_comments_list_each
4242
end
4343

4444
before(:each) do
45-
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true).at_least(:once)
45+
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).at_least(:once)
4646

4747
expect(File).to receive(:read).with(ROUTE_FILE).and_return("").at_least(:once)
4848

@@ -164,7 +164,7 @@ def magic_comments_list_each
164164

165165
describe 'When adding' do
166166
before(:each) do
167-
expect(File).to receive(:exists?).with(ROUTE_FILE)
167+
expect(File).to receive(:exist?).with(ROUTE_FILE)
168168
.and_return(true).at_least(:once)
169169
expect(AnnotateRoutes).to receive(:`).with('rake routes')
170170
.and_return('').at_least(:once)
@@ -243,7 +243,7 @@ def magic_comments_list_each
243243

244244
describe 'When adding with older Rake versions' do
245245
before(:each) do
246-
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
246+
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
247247
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("(in /bad/line)\ngood line")
248248
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
249249
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
@@ -264,7 +264,7 @@ def magic_comments_list_each
264264

265265
describe 'When adding with newer Rake versions' do
266266
before(:each) do
267-
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
267+
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
268268
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("another good line\ngood line")
269269
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
270270
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
@@ -291,7 +291,7 @@ def magic_comments_list_each
291291

292292
describe 'When removing' do
293293
before(:each) do
294-
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
294+
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
295295
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
296296
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED)
297297
end

0 commit comments

Comments
 (0)