@@ -39,7 +39,7 @@ module AnnotateModels
39
39
}
40
40
} . freeze
41
41
42
- MAGIC_COMMENT_MATCHER = Regexp . new ( /(^#\s *encoding:.*(?:\n |r\n ))|(^# coding:.*(?:\n |\r \n ))|(^# -\* - coding:.*(?:\n |\r \n ))|(^# -\* - encoding\s ?:.*(?:\n |\r \n ))|(^#\s *frozen_string_literal:.+(?:\n |\r \n ))|(^# -\* - frozen_string_literal\s *:.+-\* -(?:\n |\r \n ))/ ) . freeze
42
+ MAGIC_COMMENT_MATCHER = /(^#\s *encoding:.*(?:\n |r\n ))|(^# coding:.*(?:\n |\r \n ))|(^# -\* - coding:.*(?:\n |\r \n ))|(^# -\* - encoding\s ?:.*(?:\n |\r \n ))|(^#\s *frozen_string_literal:.+(?:\n |\r \n ))|(^# -\* - frozen_string_literal\s *:.+-\* -(?:\n |\r \n ))/
43
43
44
44
class << self
45
45
def annotate_pattern ( options = { } )
@@ -155,8 +155,8 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
155
155
with_comments_column = with_comments_column? ( klass , options )
156
156
157
157
# Precalculate Values
158
- cols_meta = cols . map do |col |
159
- col_comment = with_comments || with_comments_column ? col . comment &.gsub ( / \n / , "\\ n" ) : nil
158
+ cols_meta = cols . to_h do |col |
159
+ col_comment = with_comments || with_comments_column ? col . comment &.gsub ( " \n " , "\\ n" ) : nil
160
160
col_type = get_col_type ( col )
161
161
attrs = get_attributes ( col , col_type , klass , options )
162
162
col_name = if with_comments && col_comment
@@ -166,7 +166,7 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
166
166
end
167
167
simple_formatted_attrs = attrs . join ( ", " )
168
168
[ col . name , { col_type : col_type , attrs : attrs , col_name : col_name , simple_formatted_attrs : simple_formatted_attrs , col_comment : col_comment } ]
169
- end . to_h
169
+ end
170
170
171
171
# Output annotation
172
172
bare_max_attrs_length = cols_meta . map { |_ , m | m [ :simple_formatted_attrs ] . length } . max
@@ -179,15 +179,15 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
179
179
col_comment = cols_meta [ col . name ] [ :col_comment ]
180
180
181
181
if options [ :format_rdoc ]
182
- info << sprintf ( "# %-#{ max_size } .#{ max_size } s<tt>%s</tt>" , "*#{ col_name } *::" , attrs . unshift ( col_type ) . join ( ", " ) ) . rstrip + "\n "
182
+ info << ( sprintf ( "# %-#{ max_size } .#{ max_size } s<tt>%s</tt>" , "*#{ col_name } *::" , attrs . unshift ( col_type ) . join ( ", " ) ) . rstrip + "\n " )
183
183
elsif options [ :format_yard ]
184
- info << sprintf ( "# @!attribute #{ col_name } " ) + "\n "
184
+ info << ( "# @!attribute #{ col_name } " + "\n " )
185
185
ruby_class = col . respond_to? ( :array ) && col . array ? "Array<#{ map_col_type_to_ruby_classes ( col_type ) } >" : map_col_type_to_ruby_classes ( col_type )
186
- info << sprintf ( "# @return [#{ ruby_class } ]" ) + "\n "
186
+ info << ( "# @return [#{ ruby_class } ]" + "\n " )
187
187
elsif options [ :format_markdown ]
188
188
name_remainder = max_size - col_name . length - non_ascii_length ( col_name )
189
189
type_remainder = ( md_type_allowance - 2 ) - col_type . length
190
- info << ( sprintf ( "# **`%s`**%#{ name_remainder } s | `%s`%#{ type_remainder } s | `%s`" , col_name , " " , col_type , " " , attrs . join ( ", " ) . rstrip ) ) . gsub ( '``' , ' ' ) . rstrip + "\n "
190
+ info << ( ( sprintf ( "# **`%s`**%#{ name_remainder } s | `%s`%#{ type_remainder } s | `%s`" , col_name , " " , col_type , " " , attrs . join ( ", " ) . rstrip ) ) . gsub ( '``' , ' ' ) . rstrip + "\n " )
191
191
elsif with_comments_column
192
192
info << format_default ( col_name , max_size , col_type , bare_type_allowance , simple_formatted_attrs , bare_max_attrs_length , col_comment )
193
193
else
@@ -423,7 +423,7 @@ def get_check_constraint_info(klass, options = {})
423
423
# :position_in_*<Symbol>:: where to place the annotated section in fixture or model file,
424
424
# :before, :top, :after or :bottom. Default is :before.
425
425
#
426
- def annotate_one_file ( file_name , info_block , position , options = { } )
426
+ def annotate_one_file? ( file_name , info_block , position , options = { } )
427
427
return false unless File . exist? ( file_name )
428
428
old_content = File . read ( file_name )
429
429
return false if old_content =~ /#{ SKIP_ANNOTATION_PREFIX } .*\n /
@@ -486,7 +486,7 @@ def magic_comments_as_string(content)
486
486
end
487
487
end
488
488
489
- def remove_annotation_of_file ( file_name , options = { } )
489
+ def remove_annotation_of_file? ( file_name , options = { } )
490
490
if File . exist? ( file_name )
491
491
content = File . read ( file_name )
492
492
return false if content =~ /#{ SKIP_ANNOTATION_PREFIX } .*\n /
@@ -542,7 +542,7 @@ def annotate(klass, file, header, options = {})
542
542
model_file_name = File . join ( file )
543
543
annotated = [ ]
544
544
545
- if annotate_one_file ( model_file_name , info , :position_in_class , options_with_position ( options , :position_in_class ) )
545
+ if annotate_one_file? ( model_file_name , info , :position_in_class , options_with_position ( options , :position_in_class ) )
546
546
annotated << model_file_name
547
547
end
548
548
@@ -563,7 +563,7 @@ def annotate(klass, file, header, options = {})
563
563
. map { |f | expand_glob_into_files ( f ) }
564
564
. flatten
565
565
. each do |f |
566
- if annotate_one_file ( f , info , position_key , options_with_position ( options , position_key ) )
566
+ if annotate_one_file? ( f , info , position_key , options_with_position ( options , position_key ) )
567
567
annotated << f
568
568
end
569
569
end
@@ -694,7 +694,7 @@ def parse_options(options = {})
694
694
end
695
695
696
696
def split_model_dir ( option_value )
697
- option_value = option_value . is_a? ( Array ) ? option_value : option_value . split ( ',' )
697
+ option_value = option_value . split ( ',' ) unless option_value . is_a? ( Array )
698
698
option_value . map ( &:strip ) . reject ( &:empty? )
699
699
end
700
700
@@ -762,13 +762,13 @@ def remove_annotations(options = {})
762
762
model_name = klass . name . underscore
763
763
table_name = klass . table_name
764
764
model_file_name = file
765
- deannotated_klass = true if remove_annotation_of_file ( model_file_name , options )
765
+ deannotated_klass = true if remove_annotation_of_file? ( model_file_name , options )
766
766
767
767
get_patterns ( options , matched_types ( options ) )
768
768
. map { |f | resolve_filename ( f , model_name , table_name ) }
769
769
. each do |f |
770
770
if File . exist? ( f )
771
- remove_annotation_of_file ( f , options )
771
+ remove_annotation_of_file? ( f , options )
772
772
deannotated_klass = true
773
773
end
774
774
end
0 commit comments