Skip to content

Commit 4a55748

Browse files
committed
Fix linter warnings
1 parent eb3eaf0 commit 4a55748

File tree

12 files changed

+92
-88
lines changed

12 files changed

+92
-88
lines changed

.rubocop.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ require:
77

88
AllCops:
99
Exclude:
10-
- 'vendor/**/*'
11-
- 'spec/fixtures/**/*'
12-
- 'tmp/**/*'
13-
- 'spec/integration/**/*'
10+
- "vendor/**/*"
11+
- "spec/fixtures/**/*"
12+
- "tmp/**/*"
13+
- "spec/integration/**/*"
1414
NewCops: enable
1515

1616
Metrics/BlockLength:
1717
Exclude:
18-
- 'spec/**/*.rb'
18+
- "spec/**/*.rb"
19+
Style/FetchEnvVar:
20+
Enabled: false
21+
Metrics/ClassLength:
22+
Enabled: false
23+
RSpec/RemoveConst:
24+
Enabled: false

annotate.gemspec

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ Gem::Specification.new do |s|
1818
s.homepage = 'https://github.com/ctran/annotate_models'
1919
s.licenses = ['Ruby']
2020
s.require_paths = ['lib']
21-
s.rubygems_version = '2.1.11'
2221
s.summary = 'Annotates Rails Models, routes, fixtures, and others based on the database schema.'
2322

24-
s.specification_version = 4 if s.respond_to? :specification_version
25-
s.add_runtime_dependency(%q<rake>, '>= 10.4', '< 14.0')
26-
s.add_runtime_dependency(%q<activerecord>, ['>= 3.2', '< 9.0'])
23+
s.add_dependency(%q<rake>, '>= 10.4', '< 14.0')
24+
s.add_dependency(%q<activerecord>, ['>= 3.2', '< 9.0'])
2725

2826
s.metadata = {
2927
"bug_tracker_uri" => "https://github.com/ctran/annotate_models/issues/",
30-
"source_code_uri" => "https://github.com/ctran/annotate_models.git"
28+
"source_code_uri" => "https://github.com/ctran/annotate_models.git",
29+
'rubygems_mfa_required' => 'true'
3130
}
3231
end

lib/annotate.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def self.set_defaults(options = {})
3535
end
3636

3737
default_value = ENV[key.to_s] unless ENV[key.to_s].blank?
38-
ENV[key.to_s] = default_value.nil? ? nil : default_value.to_s
38+
ENV[key.to_s] = default_value&.to_s
3939
end
4040
end
4141

@@ -89,7 +89,7 @@ def self.eager_load(options)
8989
if Rails.version.split('.').first.to_i < 3
9090
Rails.configuration.eager_load_paths.each do |load_path|
9191
matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
92-
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
92+
Dir.glob("#{load_path}/**/*.rb").each do |file|
9393
require_dependency file.sub(matcher, '\1')
9494
end
9595
end

lib/annotate/annotate_models.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module AnnotateModels
3939
}
4040
}.freeze
4141

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))/
4343

4444
class << self
4545
def annotate_pattern(options = {})
@@ -155,8 +155,8 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
155155
with_comments_column = with_comments_column?(klass, options)
156156

157157
# 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
160160
col_type = get_col_type(col)
161161
attrs = get_attributes(col, col_type, klass, options)
162162
col_name = if with_comments && col_comment
@@ -166,7 +166,7 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
166166
end
167167
simple_formatted_attrs = attrs.join(", ")
168168
[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
170170

171171
# Output annotation
172172
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
179179
col_comment = cols_meta[col.name][:col_comment]
180180

181181
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")
183183
elsif options[:format_yard]
184-
info << sprintf("# @!attribute #{col_name}") + "\n"
184+
info << ("# @!attribute #{col_name}" + "\n")
185185
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")
187187
elsif options[:format_markdown]
188188
name_remainder = max_size - col_name.length - non_ascii_length(col_name)
189189
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")
191191
elsif with_comments_column
192192
info << format_default(col_name, max_size, col_type, bare_type_allowance, simple_formatted_attrs, bare_max_attrs_length, col_comment)
193193
else
@@ -423,7 +423,7 @@ def get_check_constraint_info(klass, options = {})
423423
# :position_in_*<Symbol>:: where to place the annotated section in fixture or model file,
424424
# :before, :top, :after or :bottom. Default is :before.
425425
#
426-
def annotate_one_file(file_name, info_block, position, options = {})
426+
def annotate_one_file?(file_name, info_block, position, options = {})
427427
return false unless File.exist?(file_name)
428428
old_content = File.read(file_name)
429429
return false if old_content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/
@@ -486,7 +486,7 @@ def magic_comments_as_string(content)
486486
end
487487
end
488488

489-
def remove_annotation_of_file(file_name, options = {})
489+
def remove_annotation_of_file?(file_name, options = {})
490490
if File.exist?(file_name)
491491
content = File.read(file_name)
492492
return false if content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/
@@ -542,7 +542,7 @@ def annotate(klass, file, header, options = {})
542542
model_file_name = File.join(file)
543543
annotated = []
544544

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))
546546
annotated << model_file_name
547547
end
548548

@@ -563,7 +563,7 @@ def annotate(klass, file, header, options = {})
563563
.map { |f| expand_glob_into_files(f) }
564564
.flatten
565565
.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))
567567
annotated << f
568568
end
569569
end
@@ -694,7 +694,7 @@ def parse_options(options = {})
694694
end
695695

696696
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)
698698
option_value.map(&:strip).reject(&:empty?)
699699
end
700700

@@ -762,13 +762,13 @@ def remove_annotations(options = {})
762762
model_name = klass.name.underscore
763763
table_name = klass.table_name
764764
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)
766766

767767
get_patterns(options, matched_types(options))
768768
.map { |f| resolve_filename(f, model_name, table_name) }
769769
.each do |f|
770770
if File.exist?(f)
771-
remove_annotation_of_file(f, options)
771+
remove_annotation_of_file?(f, options)
772772
deannotated_klass = true
773773
end
774774
end

lib/annotate/annotate_routes.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
# Released under the same license as Ruby. No Support. No Warranty.
1919
#
2020

21-
require_relative './annotate_routes/helpers'
22-
require_relative './annotate_routes/header_generator'
21+
require_relative 'annotate_routes/helpers'
22+
require_relative 'annotate_routes/header_generator'
2323

2424
module AnnotateRoutes
2525
class << self
@@ -95,7 +95,7 @@ def rewrite_contents(existing_text, new_text, frozen)
9595
def annotate_routes(header, content, header_position, options = {})
9696
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
9797
if %w(before top).include?(options[:position_in_routes])
98-
header = header << '' if content.first != ''
98+
header <<= '' if content.first != ''
9999
magic_comments_map << '' if magic_comments_map.any?
100100
new_content = magic_comments_map + header + content
101101
else

lib/annotate/annotate_routes/header_generator.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative './helpers'
1+
require_relative 'helpers'
22

33
module AnnotateRoutes
44
class HeaderGenerator
@@ -16,7 +16,7 @@ def generate(options = {})
1616
private
1717

1818
def routes_map(options)
19-
result = `rake routes`.chomp("\n").split(/\n/, -1)
19+
result = `rake routes`.chomp("\n").split("\n", -1)
2020

2121
# In old versions of Rake, the first line of output was the cwd. Not so
2222
# much in newer ones. We ditch that line if it exists, and if not, we
@@ -29,7 +29,7 @@ def routes_map(options)
2929
# Skip routes which match given regex
3030
# Note: it matches the complete line (route_name, path, controller/action)
3131
if regexp_for_ignoring_routes
32-
result.reject { |line| line =~ regexp_for_ignoring_routes }
32+
result.grep_v(regexp_for_ignoring_routes)
3333
else
3434
result
3535
end
@@ -44,18 +44,16 @@ def initialize(options, routes_map)
4444
def generate
4545
magic_comments_map, contents_without_magic_comments = Helpers.extract_magic_comments_from_array(routes_map)
4646

47-
out = []
48-
49-
magic_comments_map.each do |magic_comment|
50-
out << magic_comment
47+
out = magic_comments_map.map do |magic_comment|
48+
magic_comment
5149
end
5250
out << '' if magic_comments_map.any?
5351

5452
out << comment(options[:wrapper_open]) if options[:wrapper_open]
5553

56-
out << comment(markdown? ? PREFIX_MD : PREFIX) + timestamp_if_required
54+
out << (comment(markdown? ? PREFIX_MD : PREFIX) + timestamp_if_required)
5755
out << comment
58-
return out if contents_without_magic_comments.size.zero?
56+
return out if contents_without_magic_comments.empty?
5957

6058
maxs = [HEADER_ROW.map(&:size)] + contents_without_magic_comments[1..-1].map { |line| line.split.map(&:size) }
6159

lib/annotate/annotate_routes/helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module AnnotateRoutes
22
module Helpers
3-
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
3+
MAGIC_COMMENT_MATCHER = /(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/
44

55
class << self
66
# TODO: write the method doc using ruby rdoc formats
@@ -15,7 +15,7 @@ def strip_annotations(content)
1515
mode = :content
1616
header_position = 0
1717

18-
content.split(/\n/, -1).each_with_index do |line, line_number|
18+
content.split("\n", -1).each_with_index do |line, line_number|
1919
if mode == :header && line !~ /\s*#/
2020
mode = :content
2121
real_content << line unless line.blank?

lib/annotate/constants.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Annotate
22
module Constants
3-
TRUE_RE = /^(true|t|yes|y|1)$/i.freeze
3+
TRUE_RE = /^(true|t|yes|y|1)$/i
44

55
##
66
# The set of available options to customize the behavior of Annotate.

lib/annotate/parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Annotate
44
# Class for handling command line arguments
5-
class Parser # rubocop:disable Metrics/ClassLength
5+
class Parser
66
def self.parse(args, env = {})
77
new(args, env).parse
88
end

0 commit comments

Comments
 (0)