Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7316456

Browse files
committedDec 17, 2016
Implemented routes in markdown, closes #178
1 parent bced072 commit 7316456

File tree

3 files changed

+107
-33
lines changed

3 files changed

+107
-33
lines changed
 

Diff for: ‎.rubocop_todo.yml

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2016-12-15 16:54:50 +0100 using RuboCop version 0.46.0.
3+
# on 2016-12-17 01:21:04 +0100 using RuboCop version 0.46.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -94,14 +94,14 @@ Lint/UselessAccessModifier:
9494
Exclude:
9595
- 'lib/annotate/annotate_routes.rb'
9696

97-
# Offense count: 16
97+
# Offense count: 17
9898
Metrics/AbcSize:
9999
Max: 144
100100

101101
# Offense count: 3
102102
# Configuration parameters: CountComments.
103103
Metrics/BlockLength:
104-
Max: 135
104+
Max: 134
105105

106106
# Offense count: 2
107107
Metrics/BlockNesting:
@@ -111,17 +111,22 @@ Metrics/BlockNesting:
111111
Metrics/CyclomaticComplexity:
112112
Max: 36
113113

114-
# Offense count: 334
114+
# Offense count: 353
115115
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
116116
# URISchemes: http, https
117117
Metrics/LineLength:
118118
Max: 543
119119

120-
# Offense count: 24
120+
# Offense count: 25
121121
# Configuration parameters: CountComments.
122122
Metrics/MethodLength:
123123
Max: 70
124124

125+
# Offense count: 1
126+
# Configuration parameters: CountComments.
127+
Metrics/ModuleLength:
128+
Max: 116
129+
125130
# Offense count: 7
126131
Metrics/PerceivedComplexity:
127132
Max: 41
@@ -131,7 +136,7 @@ Style/AccessorMethodName:
131136
Exclude:
132137
- 'lib/annotate.rb'
133138

134-
# Offense count: 1
139+
# Offense count: 3
135140
# Cop supports --auto-correct.
136141
Style/AlignArray:
137142
Exclude:
@@ -284,12 +289,13 @@ Style/ExtraSpacing:
284289
- 'spec/integration/rails_4.2.0/Gemfile'
285290
- 'spec/integration/rails_4.2.0/config.ru'
286291

287-
# Offense count: 8
292+
# Offense count: 9
288293
# Configuration parameters: EnforcedStyle, SupportedStyles.
289294
# SupportedStyles: format, sprintf, percent
290295
Style/FormatString:
291296
Exclude:
292297
- 'lib/annotate/annotate_models.rb'
298+
- 'lib/annotate/annotate_routes.rb'
293299

294300
# Offense count: 181
295301
# Cop supports --auto-correct.
@@ -307,7 +313,7 @@ Style/GuardClause:
307313
- 'lib/tasks/migrate.rake'
308314
- 'spec/integration/rails_2.3_with_bundler/config/boot.rb'
309315

310-
# Offense count: 58
316+
# Offense count: 57
311317
# Cop supports --auto-correct.
312318
# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
313319
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
@@ -321,14 +327,13 @@ Style/IfUnlessModifier:
321327
Exclude:
322328
- 'lib/annotate/annotate_models.rb'
323329

324-
# Offense count: 6
330+
# Offense count: 5
325331
# Cop supports --auto-correct.
326332
# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
327333
# SupportedStyles: special_inside_parentheses, consistent, align_brackets
328334
Style/IndentArray:
329335
Exclude:
330336
- 'lib/annotate/annotate_models.rb'
331-
- 'lib/annotate/annotate_routes.rb'
332337

333338
# Offense count: 6
334339
# Cop supports --auto-correct.
@@ -514,7 +519,7 @@ Style/Semicolon:
514519
- 'bin/annotate'
515520
- 'spec/integration/rails_2.3_with_bundler/config/initializers/unified_initializer.rb'
516521

517-
# Offense count: 13
522+
# Offense count: 12
518523
# Cop supports --auto-correct.
519524
# Configuration parameters: EnforcedStyle, SupportedStyles.
520525
# SupportedStyles: space, no_space
@@ -597,7 +602,7 @@ Style/SpaceInsideStringInterpolation:
597602
Exclude:
598603
- 'lib/annotate/annotate_models.rb'
599604

600-
# Offense count: 252
605+
# Offense count: 253
601606
# Cop supports --auto-correct.
602607
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
603608
# SupportedStyles: single_quotes, double_quotes
@@ -648,18 +653,18 @@ Style/TrailingCommaInLiteral:
648653
- 'spec/integration/rails_4.1.1/lib/tasks/auto_annotate_models.rake'
649654
- 'spec/integration/rails_4.2.0/lib/tasks/auto_annotate_models.rake'
650655

651-
# Offense count: 1
656+
# Offense count: 2
652657
# Cop supports --auto-correct.
653658
Style/TrailingWhitespace:
654659
Exclude:
660+
- 'spec/annotate/annotate_routes_spec.rb'
655661
- 'spec/integration/rails_2.3_with_bundler/db/schema.rb'
656662

657-
# Offense count: 3
663+
# Offense count: 2
658664
# Cop supports --auto-correct.
659665
Style/UnneededInterpolation:
660666
Exclude:
661667
- 'bin/annotate'
662-
- 'lib/annotate/annotate_routes.rb'
663668

664669
# Offense count: 8
665670
# Cop supports --auto-correct.

Diff for: ‎lib/annotate/annotate_routes.rb

+46-18
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,61 @@
1818
# Released under the same license as Ruby. No Support. No Warranty.
1919
#
2020
module AnnotateRoutes
21-
PREFIX = '# == Route Map'
21+
PREFIX = '== Route Map'.freeze
22+
PREFIX_MD = '## Route Map'.freeze
23+
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action']
24+
25+
class << self
26+
def content(line, maxs, options = {})
27+
return line.rstrip unless options[:format_markdown]
28+
# info<< "# #{ '-' * ( max_size + md_names_overhead ) } | #{'-' * md_type_allowance} | #{ '-' * 27 }\n"
29+
# info<< sprintf( "# %-#{max_size + md_names_overhead}.#{max_size + md_names_overhead}s | %-#{md_type_allowance}.#{md_type_allowance}s | %s\n", 'Name', 'Type', 'Attributes' )
30+
line.each_with_index.map do |elem, index|
31+
min_length = maxs.map { |arr| arr[index] }.max || 0
32+
33+
sprintf("%-#{min_length}.#{min_length}s", elem)
34+
end.join(' | ')
35+
end
2236

23-
def self.do_annotations(options={})
24-
return unless routes_exists?
37+
def header(options = {})
38+
routes_map = app_routes_map(options)
2539

26-
routes_map = AnnotateRoutes.app_routes_map(options)
40+
out = ["# #{options[:format_markdown] ? PREFIX_MD : PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')]
41+
out += ['#']
42+
return out if routes_map.size.zero?
2743

28-
header = [
29-
"#{PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : ''), '#'
30-
] + routes_map.map { |line| "# #{line}".rstrip }
44+
maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) }
45+
max = maxs.map(&:max).max
3146

32-
existing_text = File.read(routes_file)
47+
if options[:format_markdown]
48+
out += ["# #{content(HEADER_ROW, maxs, options)}"]
49+
out += ["# #{content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options)}"]
50+
else
51+
out += ["# #{content(routes_map[0], maxs, options)}"]
52+
end
3353

34-
if write_contents(existing_text, header, options)
35-
puts "#{routes_file} annotated."
54+
out + routes_map[1..-1].map { |line| "# #{content(options[:format_markdown] ? line.split(' ') : line, maxs, options)}" }
3655
end
37-
end
3856

39-
def self.remove_annotations(options={})
40-
return unless routes_exists?
41-
existing_text = File.read(routes_file)
42-
content, where_header_found = strip_annotations(existing_text)
57+
def do_annotations(options = {})
58+
return unless routes_exists?
59+
existing_text = File.read(routes_file)
4360

44-
content = strip_on_removal(content, where_header_found)
61+
if write_contents(existing_text, header(options), options)
62+
puts "#{routes_file} annotated."
63+
end
64+
end
65+
66+
def remove_annotations(options={})
67+
return unless routes_exists?
68+
existing_text = File.read(routes_file)
69+
content, where_header_found = strip_annotations(existing_text)
4570

46-
if write_contents(existing_text, content, options)
47-
puts "Removed annotations from #{routes_file}."
71+
content = strip_on_removal(content, where_header_found)
72+
73+
if write_contents(existing_text, content, options)
74+
puts "Removed annotations from #{routes_file}."
75+
end
4876
end
4977
end
5078

Diff for: ‎spec/annotate/annotate_routes_spec.rb

+41
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,47 @@ def mock_file(stubs = {})
1717
AnnotateRoutes.do_annotations
1818
end
1919

20+
describe 'Annotate#example' do
21+
before(:each) do
22+
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
23+
24+
expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
25+
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(' Prefix Verb URI Pattern Controller#Action
26+
myaction1 GET /url1(.:format) mycontroller1#action
27+
myaction2 POST /url2(.:format) mycontroller2#action
28+
myaction3 DELETE|GET /url3(.:format) mycontroller3#action')
29+
30+
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
31+
end
32+
33+
it 'annotate normal' do
34+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
35+
expect(@mock_file).to receive(:puts).with("
36+
# == Route Map
37+
#
38+
# Prefix Verb URI Pattern Controller#Action
39+
# myaction1 GET /url1(.:format) mycontroller1#action
40+
# myaction2 POST /url2(.:format) mycontroller2#action
41+
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action\n")
42+
43+
AnnotateRoutes.do_annotations
44+
end
45+
46+
it 'annotate markdown' do
47+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
48+
expect(@mock_file).to receive(:puts).with("
49+
# ## Route Map
50+
#
51+
# Prefix | Verb | URI Pattern | Controller#Action
52+
# --------- | ---------- | --------------- | --------------------
53+
# myaction1 | GET | /url1(.:format) | mycontroller1#action
54+
# myaction2 | POST | /url2(.:format) | mycontroller2#action
55+
# myaction3 | DELETE|GET | /url3(.:format) | mycontroller3#action\n")
56+
57+
AnnotateRoutes.do_annotations(format_markdown: true)
58+
end
59+
end
60+
2061
describe 'When adding' do
2162
before(:each) do
2263
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)

0 commit comments

Comments
 (0)
Please sign in to comment.