Skip to content

Commit 407ace6

Browse files
nard-techdrwl
authored andcommitted
Refactor Annotate::Parser (#742)
# Summary * Removed unnecessary variables * Renamed variables that have same name as reserved word (`p`) * Other tiny fixes
1 parent 7f8bf46 commit 407ace6

File tree

1 file changed

+119
-67
lines changed

1 file changed

+119
-67
lines changed

lib/annotate/parser.rb

Lines changed: 119 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ def self.parse(args, env = {})
99

1010
attr_reader :args, :options, :env
1111

12+
DEFAULT_OPTIONS = {
13+
target_action: :do_annotations,
14+
exit: false
15+
}.freeze
16+
1217
ANNOTATION_POSITIONS = %w[before top after bottom].freeze
1318
FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze
1419
EXCLUSION_LIST = %w[tests fixtures factories serializers].freeze
1520
FORMAT_TYPES = %w[bare rdoc yard markdown].freeze
1621

1722
def initialize(args, env)
1823
@args = args
19-
@options = default_options
24+
@options = DEFAULT_OPTIONS.dup
2025
@env = env
2126
end
2227

@@ -45,114 +50,150 @@ def commit
4550

4651
def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
4752
has_set_position = {}
48-
positions = ANNOTATION_POSITIONS
4953

5054
option_parser.banner = 'Usage: annotate [options] [model_file]*'
5155

52-
option_parser.on('--additional-file-patterns path1,path2,path3', Array, "Additional file paths or globs to annotate, separated by commas (e.g. `/foo/bar/%model_name%/*.rb,/baz/%model_name%.rb`)") do |additional_file_patterns|
56+
option_parser.on('--additional-file-patterns path1,path2,path3',
57+
Array,
58+
"Additional file paths or globs to annotate, separated by commas (e.g. `/foo/bar/%model_name%/*.rb,/baz/%model_name%.rb`)") do |additional_file_patterns|
5359
ENV['additional_file_patterns'] = additional_file_patterns
5460
end
5561

56-
option_parser.on('-d', '--delete', 'Remove annotations from all model files or the routes.rb file') do
62+
option_parser.on('-d',
63+
'--delete',
64+
'Remove annotations from all model files or the routes.rb file') do
5765
@options[:target_action] = :remove_annotations
5866
end
5967

60-
option_parser.on('-p', '--position [before|top|after|bottom]', positions,
61-
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |p|
62-
env['position'] = p
68+
option_parser.on('-p',
69+
'--position [before|top|after|bottom]',
70+
ANNOTATION_POSITIONS,
71+
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |position|
72+
env['position'] = position
6373

6474
FILE_TYPE_POSITIONS.each do |key|
65-
env[key] = p unless has_set_position[key]
75+
env[key] = position unless has_set_position[key]
6676
end
6777
end
6878

69-
option_parser.on('--pc', '--position-in-class [before|top|after|bottom]', positions,
70-
'Place the annotations at the top (before) or the bottom (after) of the model file') do |p|
71-
env['position_in_class'] = p
79+
option_parser.on('--pc',
80+
'--position-in-class [before|top|after|bottom]',
81+
ANNOTATION_POSITIONS,
82+
'Place the annotations at the top (before) or the bottom (after) of the model file') do |position_in_class|
83+
env['position_in_class'] = position_in_class
7284
has_set_position['position_in_class'] = true
7385
end
7486

75-
option_parser.on('--pf', '--position-in-factory [before|top|after|bottom]', positions,
76-
'Place the annotations at the top (before) or the bottom (after) of any factory files') do |p|
77-
env['position_in_factory'] = p
87+
option_parser.on('--pf',
88+
'--position-in-factory [before|top|after|bottom]',
89+
ANNOTATION_POSITIONS,
90+
'Place the annotations at the top (before) or the bottom (after) of any factory files') do |position_in_factory|
91+
env['position_in_factory'] = position_in_factory
7892
has_set_position['position_in_factory'] = true
7993
end
8094

81-
option_parser.on('--px', '--position-in-fixture [before|top|after|bottom]', positions,
82-
'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |p|
83-
env['position_in_fixture'] = p
95+
option_parser.on('--px',
96+
'--position-in-fixture [before|top|after|bottom]',
97+
ANNOTATION_POSITIONS,
98+
'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |position_in_fixture|
99+
env['position_in_fixture'] = position_in_fixture
84100
has_set_position['position_in_fixture'] = true
85101
end
86102

87-
option_parser.on('--pt', '--position-in-test [before|top|after|bottom]', positions,
88-
'Place the annotations at the top (before) or the bottom (after) of any test files') do |p|
89-
env['position_in_test'] = p
103+
option_parser.on('--pt',
104+
'--position-in-test [before|top|after|bottom]',
105+
ANNOTATION_POSITIONS,
106+
'Place the annotations at the top (before) or the bottom (after) of any test files') do |position_in_test|
107+
env['position_in_test'] = position_in_test
90108
has_set_position['position_in_test'] = true
91109
end
92110

93-
option_parser.on('--pr', '--position-in-routes [before|top|after|bottom]', positions,
94-
'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |p|
95-
env['position_in_routes'] = p
111+
option_parser.on('--pr',
112+
'--position-in-routes [before|top|after|bottom]',
113+
ANNOTATION_POSITIONS,
114+
'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |position_in_routes|
115+
env['position_in_routes'] = position_in_routes
96116
has_set_position['position_in_routes'] = true
97117
end
98118

99-
option_parser.on('--ps', '--position-in-serializer [before|top|after|bottom]', positions,
100-
'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |p|
101-
env['position_in_serializer'] = p
119+
option_parser.on('--ps',
120+
'--position-in-serializer [before|top|after|bottom]',
121+
ANNOTATION_POSITIONS,
122+
'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |position_in_serializer|
123+
env['position_in_serializer'] = position_in_serializer
102124
has_set_position['position_in_serializer'] = true
103125
end
104126

105-
option_parser.on('--w', '--wrapper STR', 'Wrap annotation with the text passed as parameter.',
106-
'If --w option is used, the same text will be used as opening and closing') do |p|
107-
env['wrapper'] = p
127+
option_parser.on('--w',
128+
'--wrapper STR',
129+
'Wrap annotation with the text passed as parameter.',
130+
'If --w option is used, the same text will be used as opening and closing') do |wrapper|
131+
env['wrapper'] = wrapper
108132
end
109133

110-
option_parser.on('--wo', '--wrapper-open STR', 'Annotation wrapper opening.') do |p|
111-
env['wrapper_open'] = p
134+
option_parser.on('--wo',
135+
'--wrapper-open STR',
136+
'Annotation wrapper opening.') do |wrapper_open|
137+
env['wrapper_open'] = wrapper_open
112138
end
113139

114-
option_parser.on('--wc', '--wrapper-close STR', 'Annotation wrapper closing') do |p|
115-
env['wrapper_close'] = p
140+
option_parser.on('--wc',
141+
'--wrapper-close STR',
142+
'Annotation wrapper closing') do |wrapper_close|
143+
env['wrapper_close'] = wrapper_close
116144
end
117145

118-
option_parser.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") do
146+
option_parser.on('-r',
147+
'--routes',
148+
"Annotate routes.rb with the output of 'rake routes'") do
119149
env['routes'] = 'true'
120150
end
121151

122-
option_parser.on('--models', "Annotate ActiveRecord models") do
152+
option_parser.on('--models',
153+
"Annotate ActiveRecord models") do
123154
env['models'] = 'true'
124155
end
125156

126-
option_parser.on('-a', '--active-admin', 'Annotate active_admin models') do
157+
option_parser.on('-a',
158+
'--active-admin',
159+
'Annotate active_admin models') do
127160
env['active_admin'] = 'true'
128161
end
129162

130-
option_parser.on('-v', '--version', 'Show the current version of this gem') do
163+
option_parser.on('-v',
164+
'--version',
165+
'Show the current version of this gem') do
131166
puts "annotate v#{Annotate.version}"
132167
@options[:exit] = true
133168
end
134169

135-
option_parser.on('-m', '--show-migration', 'Include the migration version number in the annotation') do
170+
option_parser.on('-m',
171+
'--show-migration',
172+
'Include the migration version number in the annotation') do
136173
env['include_version'] = 'yes'
137174
end
138175

139-
option_parser.on('-k', '--show-foreign-keys',
176+
option_parser.on('-k',
177+
'--show-foreign-keys',
140178
"List the table's foreign key constraints in the annotation") do
141179
env['show_foreign_keys'] = 'yes'
142180
end
143181

144182
option_parser.on('--ck',
145-
'--complete-foreign-keys', 'Complete foreign key names in the annotation') do
183+
'--complete-foreign-keys',
184+
'Complete foreign key names in the annotation') do
146185
env['show_foreign_keys'] = 'yes'
147186
env['show_complete_foreign_keys'] = 'yes'
148187
end
149188

150-
option_parser.on('-i', '--show-indexes',
189+
option_parser.on('-i',
190+
'--show-indexes',
151191
"List the table's database indexes in the annotation") do
152192
env['show_indexes'] = 'yes'
153193
end
154194

155-
option_parser.on('-s', '--simple-indexes',
195+
option_parser.on('-s',
196+
'--simple-indexes',
156197
"Concat the column's related indexes in the annotation") do
157198
env['simple_indexes'] = 'yes'
158199
end
@@ -168,84 +209,95 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
168209
end
169210

170211
option_parser.on('--ignore-model-subdirects',
171-
"Ignore subdirectories of the models directory") do |_dir|
212+
"Ignore subdirectories of the models directory") do
172213
env['ignore_model_sub_dir'] = 'yes'
173214
end
174215

175216
option_parser.on('--sort',
176-
"Sort columns alphabetically, rather than in creation order") do |_dir|
217+
"Sort columns alphabetically, rather than in creation order") do
177218
env['sort'] = 'yes'
178219
end
179220

180221
option_parser.on('--classified-sort',
181-
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |_dir|
222+
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do
182223
env['classified_sort'] = 'yes'
183224
end
184225

185-
option_parser.on('-R', '--require path',
226+
option_parser.on('-R',
227+
'--require path',
186228
"Additional file to require before loading models, may be used multiple times") do |path|
187229
env['require'] = if env['require'].present?
188-
env['require'] + ",#{path}"
230+
"#{env['require']},#{path}"
189231
else
190232
path
191233
end
192234
end
193235

194-
option_parser.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
236+
option_parser.on('-e',
237+
'--exclude [tests,fixtures,factories,serializers]',
238+
Array,
239+
"Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
195240
exclusions ||= EXCLUSION_LIST
196241
exclusions.each { |exclusion| env["exclude_#{exclusion}"] = 'yes' }
197242
end
198243

199-
option_parser.on('-f', '--format [bare|rdoc|yard|markdown]', FORMAT_TYPES, 'Render Schema Infomation as plain/RDoc/Yard/Markdown') do |fmt|
200-
env["format_#{fmt}"] = 'yes'
244+
option_parser.on('-f',
245+
'--format [bare|rdoc|yard|markdown]',
246+
FORMAT_TYPES,
247+
'Render Schema Infomation as plain/RDoc/Yard/Markdown') do |format_type|
248+
env["format_#{format_type}"] = 'yes'
201249
end
202250

203-
option_parser.on('--force', 'Force new annotations even if there are no changes.') do |_force|
251+
option_parser.on('--force',
252+
'Force new annotations even if there are no changes.') do
204253
env['force'] = 'yes'
205254
end
206255

207-
option_parser.on('--frozen', 'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do
256+
option_parser.on('--frozen',
257+
'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do
208258
env['frozen'] = 'yes'
209259
end
210260

211-
option_parser.on('--timestamp', 'Include timestamp in (routes) annotation') do
261+
option_parser.on('--timestamp',
262+
'Include timestamp in (routes) annotation') do
212263
env['timestamp'] = 'true'
213264
end
214265

215-
option_parser.on('--trace', 'If unable to annotate a file, print the full stack trace, not just the exception message.') do |_value|
266+
option_parser.on('--trace',
267+
'If unable to annotate a file, print the full stack trace, not just the exception message.') do
216268
env['trace'] = 'yes'
217269
end
218270

219-
option_parser.on('-I', '--ignore-columns REGEX', "don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`") do |regex|
271+
option_parser.on('-I',
272+
'--ignore-columns REGEX',
273+
"don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`") do |regex|
220274
env['ignore_columns'] = regex
221275
end
222276

223-
option_parser.on('--ignore-routes REGEX', "don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`") do |regex|
277+
option_parser.on('--ignore-routes REGEX',
278+
"don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`") do |regex|
224279
env['ignore_routes'] = regex
225280
end
226281

227-
option_parser.on('--hide-limit-column-types VALUES', "don't show limit for given column types, separated by commas (i.e., `integer,boolean,text`)") do |values|
282+
option_parser.on('--hide-limit-column-types VALUES',
283+
"don't show limit for given column types, separated by commas (i.e., `integer,boolean,text`)") do |values|
228284
env['hide_limit_column_types'] = values.to_s
229285
end
230286

231-
option_parser.on('--hide-default-column-types VALUES', "don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)") do |values|
287+
option_parser.on('--hide-default-column-types VALUES',
288+
"don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)") do |values|
232289
env['hide_default_column_types'] = values.to_s
233290
end
234291

235-
option_parser.on('--ignore-unknown-models', "don't display warnings for bad model files") do |_values|
292+
option_parser.on('--ignore-unknown-models',
293+
"don't display warnings for bad model files") do
236294
env['ignore_unknown_models'] = 'true'
237295
end
238296

239-
option_parser.on('--with-comment', "include database comments in model annotations") do |_values|
297+
option_parser.on('--with-comment',
298+
"include database comments in model annotations") do
240299
env['with_comment'] = 'true'
241300
end
242301
end
243-
244-
def default_options
245-
{
246-
target_action: :do_annotations,
247-
exit: false
248-
}
249-
end
250302
end
251303
end

0 commit comments

Comments
 (0)