Skip to content

Commit 5c14bcb

Browse files
authored
Add position_in_additional_file_patterns to Options and Parser (#31)
Changes pulled from ctran/annotate_models#977. Credit goes to @Benjaminpjacobs.
1 parent d52d6b1 commit 5c14bcb

File tree

7 files changed

+33
-35
lines changed

7 files changed

+33
-35
lines changed

lib/annotate_rb/model_annotator.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
module AnnotateRb
44
module ModelAnnotator
55
autoload :Annotator, 'annotate_rb/model_annotator/annotator'
6-
autoload :Constants, 'annotate_rb/model_annotator/constants'
76
autoload :PatternGetter, 'annotate_rb/model_annotator/pattern_getter'
87
autoload :BadModelFileError, 'annotate_rb/model_annotator/bad_model_file_error'
98
autoload :FileNameResolver, 'annotate_rb/model_annotator/file_name_resolver'

lib/annotate_rb/model_annotator/annotation_decider.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module AnnotateRb
44
module ModelAnnotator
55
# Class that encapsulates the logic to decide whether to annotate a model file and its related files or not.
66
class AnnotationDecider
7+
SKIP_ANNOTATION_PREFIX = '# -\*- SkipSchemaAnnotations'.freeze
8+
79
def initialize(file, options)
810
@file = file
911
@options = options
@@ -51,7 +53,7 @@ def annotate?
5153
def file_contains_skip_annotation
5254
file_string = File.exist?(@file) ? File.read(@file) : ''
5355

54-
if /#{Constants::SKIP_ANNOTATION_PREFIX}.*/ =~ file_string
56+
if /#{SKIP_ANNOTATION_PREFIX}.*/ =~ file_string
5557
true
5658
else
5759
false

lib/annotate_rb/model_annotator/constants.rb

Lines changed: 0 additions & 20 deletions
This file was deleted.

lib/annotate_rb/model_annotator/related_files_list_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def add_related_admin_files
124124
end
125125

126126
def add_additional_file_patterns
127-
position_key = :position_in_additional_file_patterns # Key does not exist
127+
position_key = :position_in_additional_file_patterns
128128
pattern_type = 'additional_file_patterns'
129129

130130
related_files = related_files_for_pattern(pattern_type)

lib/annotate_rb/options.rb

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ def from(options = {}, state = {})
1616

1717
POSITION_OPTIONS = {
1818
position: nil, # ModelAnnotator, RouteAnnotator
19+
position_in_additional_file_patterns: nil, # ModelAnnotator
1920
position_in_class: nil, # ModelAnnotator
20-
position_in_factory: nil, # Unused
21-
position_in_fixture: nil, # Unused
21+
position_in_factory: nil, # ModelAnnotator
22+
position_in_fixture: nil, # ModelAnnotator
2223
position_in_routes: nil, # RouteAnnotator
23-
position_in_serializer: nil, # Unused
24-
position_in_test: nil, # Unused
24+
position_in_serializer: nil, # ModelAnnotator
25+
position_in_test: nil, # ModelAnnotator
2526
}.freeze
2627

2728
FLAG_OPTIONS = {
@@ -87,12 +88,6 @@ def from(options = {}, state = {})
8788

8889
DEFAULT_OPTIONS = {}.merge(POSITION_OPTIONS, FLAG_OPTIONS, OTHER_OPTIONS, PATH_OPTIONS).freeze
8990

90-
POSITION_OPTION_KEYS = [
91-
:position,
92-
:position_in_class,
93-
:position_in_routes,
94-
].freeze
95-
9691
FLAG_OPTION_KEYS = [
9792
:classified_sort,
9893
:exclude_controllers,
@@ -147,7 +142,7 @@ def from(options = {}, state = {})
147142
].freeze
148143

149144
ALL_OPTION_KEYS = [
150-
POSITION_OPTION_KEYS, FLAG_OPTION_KEYS, OTHER_OPTION_KEYS, PATH_OPTION_KEYS
145+
POSITION_OPTIONS.keys, FLAG_OPTION_KEYS, OTHER_OPTION_KEYS, PATH_OPTION_KEYS
151146
].flatten.freeze
152147

153148
POSITION_DEFAULT = 'before'
@@ -175,7 +170,7 @@ def load_defaults
175170
# 1) Use the value if it's defined
176171
# 2) Use value from :position if it's defined
177172
# 3) Use default
178-
POSITION_OPTION_KEYS.each do |key|
173+
POSITION_OPTIONS.keys.each do |key|
179174
@options[key] = Helper.fallback(
180175
@options[key], @options[:position], POSITION_DEFAULT
181176
)

lib/annotate_rb/parser.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ def add_position_options_to_parser(option_parser)
297297
@options[:position_in_serializer] = position_in_serializer
298298
has_set_position['position_in_serializer'] = true
299299
end
300+
301+
option_parser.on('--pa',
302+
'--position-in-additional-file-patterns [before|top|after|bottom]',
303+
ANNOTATION_POSITIONS,
304+
'Place the annotations at the top (before) or the bottom (after) of files captured in additional file patterns') do |position_in_serializer|
305+
@options[:position_in_additional_file_patterns] = position_in_serializer
306+
has_set_position['position_in_additional_file_patterns'] = true
307+
end
300308
end
301309

302310
def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength

spec/lib/annotate_rb/parser_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@ module AnnotateRb # rubocop:disable Metrics/ModuleLength
188188
end
189189
end
190190

191+
%w[--pa --position-in-additional-file-patterns].each do |option|
192+
describe option do
193+
Parser::ANNOTATION_POSITIONS.each do |position|
194+
context "when specifying '#{position}'" do
195+
let(:args) { [option, position] }
196+
197+
it "sets the position_in_additional_file_patterns to '#{position}'" do
198+
expect(result).to include(:position_in_additional_file_patterns => position)
199+
end
200+
end
201+
end
202+
end
203+
end
204+
191205
%w[--w --wrapper].each do |option|
192206
describe option do
193207
let(:wrapper_text) { 'WRAPPER_STR' }

0 commit comments

Comments
 (0)