annotate 3.0.3 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,65 +19,62 @@
19
19
  #
20
20
  # Released under the same license as Ruby. No Support. No Warranty.
21
21
  #
22
+
23
+ require_relative './annotate_routes/helpers'
24
+
22
25
  module AnnotateRoutes
23
26
  PREFIX = '== Route Map'.freeze
24
27
  PREFIX_MD = '## Route Map'.freeze
25
- HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action']
28
+ HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].freeze
26
29
 
27
30
  class << self
28
31
  def do_annotations(options = {})
29
- return unless routes_exists?
30
- existing_text = File.read(routes_file)
31
-
32
- if rewrite_contents_with_header(existing_text, header(options), options)
33
- puts "#{routes_file} annotated."
32
+ if routes_file_exist?
33
+ existing_text = File.read(routes_file)
34
+ content, header_position = Helpers.strip_annotations(existing_text)
35
+ new_content = annotate_routes(header(options), content, header_position, options)
36
+ new_text = new_content.join("\n")
37
+
38
+ if rewrite_contents(existing_text, new_text)
39
+ puts "#{routes_file} was annotated."
40
+ else
41
+ puts "#{routes_file} was not changed."
42
+ end
43
+ else
44
+ puts "#{routes_file} could not be found."
34
45
  end
35
46
  end
36
47
 
37
48
  def remove_annotations(_options={})
38
- return unless routes_exists?
39
- existing_text = File.read(routes_file)
40
- content, where_header_found = strip_annotations(existing_text)
41
- new_content = strip_on_removal(content, where_header_found)
42
- if rewrite_contents(existing_text, new_content)
43
- puts "Removed annotations from #{routes_file}."
49
+ if routes_file_exist?
50
+ existing_text = File.read(routes_file)
51
+ content, header_position = Helpers.strip_annotations(existing_text)
52
+ new_content = strip_on_removal(content, header_position)
53
+ new_text = new_content.join("\n")
54
+ if rewrite_contents(existing_text, new_text)
55
+ puts "Annotations were removed from #{routes_file}."
56
+ else
57
+ puts "#{routes_file} was not changed (Annotation did not exist)."
58
+ end
59
+ else
60
+ puts "#{routes_file} could not be found."
44
61
  end
45
62
  end
46
63
 
47
64
  private
48
65
 
49
- def routes_exists?
50
- routes_exists = File.exists?(routes_file)
51
- puts "Can't find routes.rb" unless routes_exists
52
-
53
- routes_exists
66
+ def routes_file_exist?
67
+ File.exist?(routes_file)
54
68
  end
55
69
 
56
70
  def routes_file
57
71
  @routes_rb ||= File.join('config', 'routes.rb')
58
72
  end
59
73
 
60
- def rewrite_contents_with_header(existing_text, header, options = {})
61
- content, where_header_found = strip_annotations(existing_text)
62
- new_content = annotate_routes(header, content, where_header_found, options)
63
-
64
- # Make sure we end on a trailing newline.
65
- new_content << '' unless new_content.last == ''
66
- new_text = new_content.join("\n")
67
-
68
- if existing_text == new_text
69
- puts "#{routes_file} unchanged."
70
- false
71
- else
72
- File.open(routes_file, 'wb') { |f| f.puts(new_text) }
73
- true
74
- end
75
- end
76
-
77
74
  def header(options = {})
78
75
  routes_map = app_routes_map(options)
79
76
 
80
- magic_comments_map, routes_map = extract_magic_comments_from_array(routes_map)
77
+ magic_comments_map, routes_map = Helpers.extract_magic_comments_from_array(routes_map)
81
78
 
82
79
  out = []
83
80
 
@@ -86,10 +83,10 @@ module AnnotateRoutes
86
83
  end
87
84
  out << '' if magic_comments_map.any?
88
85
 
89
- out += ["# #{options[:wrapper_open]}"] if options[:wrapper_open]
86
+ out << comment(options[:wrapper_open]) if options[:wrapper_open]
90
87
 
91
- out += ["# #{options[:format_markdown] ? PREFIX_MD : PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')]
92
- out += ['#']
88
+ out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')
89
+ out << comment
93
90
  return out if routes_map.size.zero?
94
91
 
95
92
  maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) }
@@ -97,67 +94,44 @@ module AnnotateRoutes
97
94
  if options[:format_markdown]
98
95
  max = maxs.map(&:max).compact.max
99
96
 
100
- out += ["# #{content(HEADER_ROW, maxs, options)}"]
101
- out += ["# #{content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options)}"]
97
+ out << comment(content(HEADER_ROW, maxs, options))
98
+ out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options))
102
99
  else
103
- out += ["# #{content(routes_map[0], maxs, options)}"]
100
+ out << comment(content(routes_map[0], maxs, options))
104
101
  end
105
102
 
106
- out += routes_map[1..-1].map { |line| "# #{content(options[:format_markdown] ? line.split(' ') : line, maxs, options)}" }
107
- out += ["# #{options[:wrapper_close]}"] if options[:wrapper_close]
103
+ out += routes_map[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs, options)) }
104
+ out << comment(options[:wrapper_close]) if options[:wrapper_close]
108
105
 
109
106
  out
110
107
  end
111
108
 
112
- # TODO: write the method doc using ruby rdoc formats
113
- # where_header_found => This will either be :before, :after, or
114
- # a number. If the number is > 0, the
115
- # annotation was found somewhere in the
116
- # middle of the file. If the number is
117
- # zero, no annotation was found.
118
- def strip_annotations(content)
119
- real_content = []
120
- mode = :content
121
- header_found_at = 0
122
-
123
- content.split(/\n/, -1).each_with_index do |line, line_number|
124
- if mode == :header && line !~ /\s*#/
125
- mode = :content
126
- real_content << line unless line.blank?
127
- elsif mode == :content
128
- if line =~ /^\s*#\s*== Route.*$/
129
- header_found_at = line_number + 1 # index start's at 0
130
- mode = :header
131
- else
132
- real_content << line
133
- end
134
- end
109
+ def comment(row = '')
110
+ if row == ''
111
+ '#'
112
+ else
113
+ "# #{row}"
135
114
  end
136
-
137
- where_header_found(real_content, header_found_at)
138
115
  end
139
116
 
140
- def strip_on_removal(content, where_header_found)
141
- if where_header_found == :before
117
+ def strip_on_removal(content, header_position)
118
+ if header_position == :before
142
119
  content.shift while content.first == ''
143
- elsif where_header_found == :after
120
+ elsif header_position == :after
144
121
  content.pop while content.last == ''
145
122
  end
146
123
 
124
+ # Make sure we end on a trailing newline.
125
+ content << '' unless content.last == ''
126
+
147
127
  # TODO: If the user buried it in the middle, we should probably see about
148
128
  # TODO: preserving a single line of space between the content above and
149
129
  # TODO: below...
150
130
  content
151
131
  end
152
132
 
153
- # @param [String, Array<String>]
154
- def rewrite_contents(existing_text, new_content)
155
- # Make sure we end on a trailing newline.
156
- new_content << '' unless new_content.last == ''
157
- new_text = new_content.join("\n")
158
-
133
+ def rewrite_contents(existing_text, new_text)
159
134
  if existing_text == new_text
160
- puts "#{routes_file} unchanged."
161
135
  false
162
136
  else
163
137
  File.open(routes_file, 'wb') { |f| f.puts(new_text) }
@@ -165,8 +139,8 @@ module AnnotateRoutes
165
139
  end
166
140
  end
167
141
 
168
- def annotate_routes(header, content, where_header_found, options = {})
169
- magic_comments_map, content = extract_magic_comments_from_array(content)
142
+ def annotate_routes(header, content, header_position, options = {})
143
+ magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
170
144
  if %w(before top).include?(options[:position_in_routes])
171
145
  header = header << '' if content.first != ''
172
146
  magic_comments_map << '' if magic_comments_map.any?
@@ -178,11 +152,14 @@ module AnnotateRoutes
178
152
 
179
153
  # We're moving something from the top of the file to the bottom, so ditch
180
154
  # the spacer we put in the first time around.
181
- content.shift if where_header_found == :before && content.first == ''
155
+ content.shift if header_position == :before && content.first == ''
182
156
 
183
157
  new_content = magic_comments_map + content + header
184
158
  end
185
159
 
160
+ # Make sure we end on a trailing newline.
161
+ new_content << '' unless new_content.last == ''
162
+
186
163
  new_content
187
164
  end
188
165
 
@@ -203,24 +180,6 @@ module AnnotateRoutes
203
180
  routes_map
204
181
  end
205
182
 
206
- # @param [Array<String>] content
207
- # @return [Array<String>] all found magic comments
208
- # @return [Array<String>] content without magic comments
209
- def extract_magic_comments_from_array(content_array)
210
- magic_comments = []
211
- new_content = []
212
-
213
- content_array.map do |row|
214
- if row =~ magic_comment_matcher
215
- magic_comments << row.strip
216
- else
217
- new_content << row
218
- end
219
- end
220
-
221
- [magic_comments, new_content]
222
- end
223
-
224
183
  def content(line, maxs, options = {})
225
184
  return line.rstrip unless options[:format_markdown]
226
185
 
@@ -230,22 +189,5 @@ module AnnotateRoutes
230
189
  sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
231
190
  end.join(' | ')
232
191
  end
233
-
234
- def where_header_found(real_content, header_found_at)
235
- # By default assume the annotation was found in the middle of the file
236
-
237
- # ... unless we have evidence it was at the beginning ...
238
- return real_content, :before if header_found_at == 1
239
-
240
- # ... or that it was at the end.
241
- return real_content, :after if header_found_at >= real_content.count
242
-
243
- # and the default
244
- return real_content, header_found_at
245
- end
246
-
247
- def magic_comment_matcher
248
- Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/)
249
- end
250
192
  end
251
193
  end
@@ -0,0 +1,69 @@
1
+ module AnnotateRoutes
2
+ module Helpers
3
+ MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
4
+
5
+ class << self
6
+ # TODO: write the method doc using ruby rdoc formats
7
+ # This method returns an array of 'real_content' and 'header_position'.
8
+ # 'header_position' will either be :before, :after, or
9
+ # a number. If the number is > 0, the
10
+ # annotation was found somewhere in the
11
+ # middle of the file. If the number is
12
+ # zero, no annotation was found.
13
+ def strip_annotations(content)
14
+ real_content = []
15
+ mode = :content
16
+ header_position = 0
17
+
18
+ content.split(/\n/, -1).each_with_index do |line, line_number|
19
+ if mode == :header && line !~ /\s*#/
20
+ mode = :content
21
+ real_content << line unless line.blank?
22
+ elsif mode == :content
23
+ if line =~ /^\s*#\s*== Route.*$/
24
+ header_position = line_number + 1 # index start's at 0
25
+ mode = :header
26
+ else
27
+ real_content << line
28
+ end
29
+ end
30
+ end
31
+
32
+ real_content_and_header_position(real_content, header_position)
33
+ end
34
+
35
+ # @param [Array<String>] content
36
+ # @return [Array<String>] all found magic comments
37
+ # @return [Array<String>] content without magic comments
38
+ def extract_magic_comments_from_array(content_array)
39
+ magic_comments = []
40
+ new_content = []
41
+
42
+ content_array.each do |row|
43
+ if row =~ MAGIC_COMMENT_MATCHER
44
+ magic_comments << row.strip
45
+ else
46
+ new_content << row
47
+ end
48
+ end
49
+
50
+ [magic_comments, new_content]
51
+ end
52
+
53
+ private
54
+
55
+ def real_content_and_header_position(real_content, header_position)
56
+ # By default assume the annotation was found in the middle of the file
57
+
58
+ # ... unless we have evidence it was at the beginning ...
59
+ return real_content, :before if header_position == 1
60
+
61
+ # ... or that it was at the end.
62
+ return real_content, :after if header_position >= real_content.count
63
+
64
+ # and the default
65
+ return real_content, header_position
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,5 +1,38 @@
1
1
  module Annotate
2
2
  module Constants
3
3
  TRUE_RE = /^(true|t|yes|y|1)$/i.freeze
4
+
5
+ ##
6
+ # The set of available options to customize the behavior of Annotate.
7
+ #
8
+ POSITION_OPTIONS = [
9
+ :position_in_routes, :position_in_class, :position_in_test,
10
+ :position_in_fixture, :position_in_factory, :position,
11
+ :position_in_serializer
12
+ ].freeze
13
+
14
+ FLAG_OPTIONS = [
15
+ :show_indexes, :simple_indexes, :include_version, :exclude_tests,
16
+ :exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
17
+ :format_bare, :format_rdoc, :format_yard, :format_markdown, :sort, :force, :frozen,
18
+ :trace, :timestamp, :exclude_serializers, :classified_sort,
19
+ :show_foreign_keys, :show_complete_foreign_keys,
20
+ :exclude_scaffolds, :exclude_controllers, :exclude_helpers,
21
+ :exclude_sti_subclasses, :ignore_unknown_models, :with_comment
22
+ ].freeze
23
+
24
+ OTHER_OPTIONS = [
25
+ :additional_file_patterns, :ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close,
26
+ :wrapper, :routes, :models, :hide_limit_column_types, :hide_default_column_types,
27
+ :ignore_routes, :active_admin
28
+ ].freeze
29
+
30
+ PATH_OPTIONS = [
31
+ :require, :model_dir, :root_dir
32
+ ].freeze
33
+
34
+ ALL_ANNOTATE_OPTIONS = [
35
+ POSITION_OPTIONS, FLAG_OPTIONS, OTHER_OPTIONS, PATH_OPTIONS
36
+ ].freeze
4
37
  end
5
38
  end
@@ -0,0 +1,30 @@
1
+ module Annotate
2
+ # Class for holding helper methods. Done to make lib/annotate.rb less bloated.
3
+ class Helpers
4
+ class << self
5
+ def skip_on_migration?
6
+ ENV['ANNOTATE_SKIP_ON_DB_MIGRATE'] =~ Constants::TRUE_RE || ENV['skip_on_db_migrate'] =~ Constants::TRUE_RE
7
+ end
8
+
9
+ def include_routes?
10
+ ENV['routes'] =~ Constants::TRUE_RE
11
+ end
12
+
13
+ def include_models?
14
+ ENV['models'] =~ Constants::TRUE_RE
15
+ end
16
+
17
+ def true?(val)
18
+ val.present? && Constants::TRUE_RE.match?(val)
19
+ end
20
+
21
+ def fallback(*args)
22
+ args.detect(&:present?)
23
+ end
24
+
25
+ def reset_options(options)
26
+ options.flatten.each { |key| ENV[key.to_s] = nil }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -9,14 +9,19 @@ module Annotate
9
9
 
10
10
  attr_reader :args, :options, :env
11
11
 
12
+ DEFAULT_OPTIONS = {
13
+ target_action: :do_annotations,
14
+ exit: false
15
+ }.freeze
16
+
12
17
  ANNOTATION_POSITIONS = %w[before top after bottom].freeze
13
18
  FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze
14
19
  EXCLUSION_LIST = %w[tests fixtures factories serializers].freeze
15
- FORMAT_TYPES = %w[bare rdoc markdown].freeze
20
+ FORMAT_TYPES = %w[bare rdoc yard markdown].freeze
16
21
 
17
22
  def initialize(args, env)
18
23
  @args = args
19
- @options = default_options
24
+ @options = DEFAULT_OPTIONS.dup
20
25
  @env = env
21
26
  end
22
27
 
@@ -31,128 +36,164 @@ module Annotate
31
36
 
32
37
  private
33
38
 
34
- def commit
35
- env.each_pair do |key, value|
36
- ENV[key] = value
37
- end
38
- end
39
-
40
39
  def parser
41
40
  OptionParser.new do |option_parser|
42
41
  add_options_to_parser(option_parser)
43
42
  end
44
43
  end
45
44
 
45
+ def commit
46
+ env.each_pair do |key, value|
47
+ ENV[key] = value
48
+ end
49
+ end
50
+
46
51
  def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
47
52
  has_set_position = {}
48
- positions = ANNOTATION_POSITIONS
49
53
 
50
54
  option_parser.banner = 'Usage: annotate [options] [model_file]*'
51
55
 
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|
53
59
  ENV['additional_file_patterns'] = additional_file_patterns
54
60
  end
55
61
 
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
57
65
  @options[:target_action] = :remove_annotations
58
66
  end
59
67
 
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
63
73
 
64
74
  FILE_TYPE_POSITIONS.each do |key|
65
- env[key] = p unless has_set_position[key]
75
+ env[key] = position unless has_set_position[key]
66
76
  end
67
77
  end
68
78
 
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
72
84
  has_set_position['position_in_class'] = true
73
85
  end
74
86
 
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
78
92
  has_set_position['position_in_factory'] = true
79
93
  end
80
94
 
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
84
100
  has_set_position['position_in_fixture'] = true
85
101
  end
86
102
 
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
90
108
  has_set_position['position_in_test'] = true
91
109
  end
92
110
 
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
96
116
  has_set_position['position_in_routes'] = true
97
117
  end
98
118
 
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
102
124
  has_set_position['position_in_serializer'] = true
103
125
  end
104
126
 
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
108
132
  end
109
133
 
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
112
138
  end
113
139
 
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
116
144
  end
117
145
 
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
119
149
  env['routes'] = 'true'
120
150
  end
121
151
 
122
- option_parser.on('--models', "Annotate ActiveRecord models") do
152
+ option_parser.on('--models',
153
+ "Annotate ActiveRecord models") do
123
154
  env['models'] = 'true'
124
155
  end
125
156
 
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
127
160
  env['active_admin'] = 'true'
128
161
  end
129
162
 
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
131
166
  puts "annotate v#{Annotate.version}"
132
167
  @options[:exit] = true
133
168
  end
134
169
 
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
136
173
  env['include_version'] = 'yes'
137
174
  end
138
175
 
139
- option_parser.on('-k', '--show-foreign-keys',
176
+ option_parser.on('-k',
177
+ '--show-foreign-keys',
140
178
  "List the table's foreign key constraints in the annotation") do
141
179
  env['show_foreign_keys'] = 'yes'
142
180
  end
143
181
 
144
182
  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
146
185
  env['show_foreign_keys'] = 'yes'
147
186
  env['show_complete_foreign_keys'] = 'yes'
148
187
  end
149
188
 
150
- option_parser.on('-i', '--show-indexes',
189
+ option_parser.on('-i',
190
+ '--show-indexes',
151
191
  "List the table's database indexes in the annotation") do
152
192
  env['show_indexes'] = 'yes'
153
193
  end
154
194
 
155
- option_parser.on('-s', '--simple-indexes',
195
+ option_parser.on('-s',
196
+ '--simple-indexes',
156
197
  "Concat the column's related indexes in the annotation") do
157
198
  env['simple_indexes'] = 'yes'
158
199
  end
@@ -168,84 +209,95 @@ module Annotate
168
209
  end
169
210
 
170
211
  option_parser.on('--ignore-model-subdirects',
171
- "Ignore subdirectories of the models directory") do |_dir|
212
+ "Ignore subdirectories of the models directory") do
172
213
  env['ignore_model_sub_dir'] = 'yes'
173
214
  end
174
215
 
175
216
  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
177
218
  env['sort'] = 'yes'
178
219
  end
179
220
 
180
221
  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
182
223
  env['classified_sort'] = 'yes'
183
224
  end
184
225
 
185
- option_parser.on('-R', '--require path',
226
+ option_parser.on('-R',
227
+ '--require path',
186
228
  "Additional file to require before loading models, may be used multiple times") do |path|
187
- env['require'] = if !env['require'].blank?
188
- env['require'] + ",#{path}"
229
+ env['require'] = if env['require'].present?
230
+ "#{env['require']},#{path}"
189
231
  else
190
232
  path
191
233
  end
192
234
  end
193
235
 
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|
195
240
  exclusions ||= EXCLUSION_LIST
196
241
  exclusions.each { |exclusion| env["exclude_#{exclusion}"] = 'yes' }
197
242
  end
198
243
 
199
- option_parser.on('-f', '--format [bare|rdoc|markdown]', FORMAT_TYPES, 'Render Schema Infomation as plain/RDoc/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'
201
249
  end
202
250
 
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
204
253
  env['force'] = 'yes'
205
254
  end
206
255
 
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
208
258
  env['frozen'] = 'yes'
209
259
  end
210
260
 
211
- option_parser.on('--timestamp', 'Include timestamp in (routes) annotation') do
261
+ option_parser.on('--timestamp',
262
+ 'Include timestamp in (routes) annotation') do
212
263
  env['timestamp'] = 'true'
213
264
  end
214
265
 
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
216
268
  env['trace'] = 'yes'
217
269
  end
218
270
 
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|
220
274
  env['ignore_columns'] = regex
221
275
  end
222
276
 
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|
224
279
  env['ignore_routes'] = regex
225
280
  end
226
281
 
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|
228
284
  env['hide_limit_column_types'] = values.to_s
229
285
  end
230
286
 
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|
232
289
  env['hide_default_column_types'] = values.to_s
233
290
  end
234
291
 
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
236
294
  env['ignore_unknown_models'] = 'true'
237
295
  end
238
296
 
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
240
299
  env['with_comment'] = 'true'
241
300
  end
242
301
  end
243
-
244
- def default_options
245
- {
246
- target_action: :do_annotations,
247
- exit: false
248
- }
249
- end
250
302
  end
251
303
  end