Skip to content

Commit

Permalink
Add position_in_additional_file_patterns to Options and Parser (#31)
Browse files Browse the repository at this point in the history
Changes pulled from ctran/annotate_models#977.
Credit goes to @Benjaminpjacobs.
  • Loading branch information
drwl committed May 23, 2023
1 parent d52d6b1 commit 5c14bcb
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 35 deletions.
1 change: 0 additions & 1 deletion lib/annotate_rb/model_annotator.rb
Expand Up @@ -3,7 +3,6 @@
module AnnotateRb
module ModelAnnotator
autoload :Annotator, 'annotate_rb/model_annotator/annotator'
autoload :Constants, 'annotate_rb/model_annotator/constants'
autoload :PatternGetter, 'annotate_rb/model_annotator/pattern_getter'
autoload :BadModelFileError, 'annotate_rb/model_annotator/bad_model_file_error'
autoload :FileNameResolver, 'annotate_rb/model_annotator/file_name_resolver'
Expand Down
4 changes: 3 additions & 1 deletion lib/annotate_rb/model_annotator/annotation_decider.rb
Expand Up @@ -4,6 +4,8 @@ module AnnotateRb
module ModelAnnotator
# Class that encapsulates the logic to decide whether to annotate a model file and its related files or not.
class AnnotationDecider
SKIP_ANNOTATION_PREFIX = '# -\*- SkipSchemaAnnotations'.freeze

def initialize(file, options)
@file = file
@options = options
Expand Down Expand Up @@ -51,7 +53,7 @@ def annotate?
def file_contains_skip_annotation
file_string = File.exist?(@file) ? File.read(@file) : ''

if /#{Constants::SKIP_ANNOTATION_PREFIX}.*/ =~ file_string
if /#{SKIP_ANNOTATION_PREFIX}.*/ =~ file_string
true
else
false
Expand Down
20 changes: 0 additions & 20 deletions lib/annotate_rb/model_annotator/constants.rb

This file was deleted.

Expand Up @@ -124,7 +124,7 @@ def add_related_admin_files
end

def add_additional_file_patterns
position_key = :position_in_additional_file_patterns # Key does not exist
position_key = :position_in_additional_file_patterns
pattern_type = 'additional_file_patterns'

related_files = related_files_for_pattern(pattern_type)
Expand Down
19 changes: 7 additions & 12 deletions lib/annotate_rb/options.rb
Expand Up @@ -16,12 +16,13 @@ def from(options = {}, state = {})

POSITION_OPTIONS = {
position: nil, # ModelAnnotator, RouteAnnotator
position_in_additional_file_patterns: nil, # ModelAnnotator
position_in_class: nil, # ModelAnnotator
position_in_factory: nil, # Unused
position_in_fixture: nil, # Unused
position_in_factory: nil, # ModelAnnotator
position_in_fixture: nil, # ModelAnnotator
position_in_routes: nil, # RouteAnnotator
position_in_serializer: nil, # Unused
position_in_test: nil, # Unused
position_in_serializer: nil, # ModelAnnotator
position_in_test: nil, # ModelAnnotator
}.freeze

FLAG_OPTIONS = {
Expand Down Expand Up @@ -87,12 +88,6 @@ def from(options = {}, state = {})

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

POSITION_OPTION_KEYS = [
:position,
:position_in_class,
:position_in_routes,
].freeze

FLAG_OPTION_KEYS = [
:classified_sort,
:exclude_controllers,
Expand Down Expand Up @@ -147,7 +142,7 @@ def from(options = {}, state = {})
].freeze

ALL_OPTION_KEYS = [
POSITION_OPTION_KEYS, FLAG_OPTION_KEYS, OTHER_OPTION_KEYS, PATH_OPTION_KEYS
POSITION_OPTIONS.keys, FLAG_OPTION_KEYS, OTHER_OPTION_KEYS, PATH_OPTION_KEYS
].flatten.freeze

POSITION_DEFAULT = 'before'
Expand Down Expand Up @@ -175,7 +170,7 @@ def load_defaults
# 1) Use the value if it's defined
# 2) Use value from :position if it's defined
# 3) Use default
POSITION_OPTION_KEYS.each do |key|
POSITION_OPTIONS.keys.each do |key|
@options[key] = Helper.fallback(
@options[key], @options[:position], POSITION_DEFAULT
)
Expand Down
8 changes: 8 additions & 0 deletions lib/annotate_rb/parser.rb
Expand Up @@ -297,6 +297,14 @@ def add_position_options_to_parser(option_parser)
@options[:position_in_serializer] = position_in_serializer
has_set_position['position_in_serializer'] = true
end

option_parser.on('--pa',
'--position-in-additional-file-patterns [before|top|after|bottom]',
ANNOTATION_POSITIONS,
'Place the annotations at the top (before) or the bottom (after) of files captured in additional file patterns') do |position_in_serializer|
@options[:position_in_additional_file_patterns] = position_in_serializer
has_set_position['position_in_additional_file_patterns'] = true
end
end

def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/annotate_rb/parser_spec.rb
Expand Up @@ -188,6 +188,20 @@ module AnnotateRb # rubocop:disable Metrics/ModuleLength
end
end

%w[--pa --position-in-additional-file-patterns].each do |option|
describe option do
Parser::ANNOTATION_POSITIONS.each do |position|
context "when specifying '#{position}'" do
let(:args) { [option, position] }

it "sets the position_in_additional_file_patterns to '#{position}'" do
expect(result).to include(:position_in_additional_file_patterns => position)
end
end
end
end
end

%w[--w --wrapper].each do |option|
describe option do
let(:wrapper_text) { 'WRAPPER_STR' }
Expand Down

0 comments on commit 5c14bcb

Please sign in to comment.