Skip to content

Commit 3f0b6b3

Browse files
authored
Refactor lib/annotate.rb (#707)
This change converts .all_options into a constant and moves it into Annotate::Constants. It also changes usages of .all_options.
1 parent a05e458 commit 3f0b6b3

11 files changed

+281
-150
lines changed

Diff for: .rubocop_todo.yml

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2019-09-02 16:23:56 -0700 using RuboCop version 0.68.1.
3+
# on 2019-12-24 20:33:53 -0800 using RuboCop version 0.68.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -51,7 +51,7 @@ Layout/AlignArray:
5151
Exclude:
5252
- 'spec/lib/annotate/annotate_models_spec.rb'
5353

54-
# Offense count: 107
54+
# Offense count: 111
5555
# Cop supports --auto-correct.
5656
# Configuration parameters: EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle.
5757
# SupportedHashRocketStyles: key, separator, table
@@ -74,7 +74,7 @@ Layout/BlockAlignment:
7474
Exclude:
7575
- 'lib/annotate/annotate_models.rb'
7676

77-
# Offense count: 45
77+
# Offense count: 47
7878
# Cop supports --auto-correct.
7979
Layout/ClosingHeredocIndentation:
8080
Exclude:
@@ -87,7 +87,7 @@ Layout/ClosingHeredocIndentation:
8787
- 'spec/integration/standalone.rb'
8888
- 'spec/lib/annotate/annotate_models_spec.rb'
8989

90-
# Offense count: 13
90+
# Offense count: 14
9191
# Cop supports --auto-correct.
9292
Layout/EmptyLineAfterGuardClause:
9393
Exclude:
@@ -173,7 +173,7 @@ Layout/ExtraSpacing:
173173
Layout/IndentFirstHashElement:
174174
EnforcedStyle: consistent
175175

176-
# Offense count: 54
176+
# Offense count: 55
177177
# Cop supports --auto-correct.
178178
# Configuration parameters: EnforcedStyle.
179179
# SupportedStyles: auto_detection, squiggly, active_support, powerpack, unindent
@@ -410,11 +410,11 @@ Lint/ShadowingOuterLocalVariable:
410410
Metrics/AbcSize:
411411
Max: 141
412412

413-
# Offense count: 28
413+
# Offense count: 31
414414
# Configuration parameters: CountComments, ExcludedMethods.
415415
# ExcludedMethods: refine
416416
Metrics/BlockLength:
417-
Max: 259
417+
Max: 268
418418

419419
# Offense count: 1
420420
# Configuration parameters: CountBlocks.
@@ -439,7 +439,7 @@ Naming/AccessorMethodName:
439439
Exclude:
440440
- 'lib/annotate.rb'
441441

442-
# Offense count: 76
442+
# Offense count: 79
443443
# Configuration parameters: Blacklist.
444444
# Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
445445
Naming/HeredocDelimiterNaming:
@@ -572,7 +572,7 @@ Style/FormatStringToken:
572572
Exclude:
573573
- 'lib/annotate/annotate_models.rb'
574574

575-
# Offense count: 186
575+
# Offense count: 189
576576
# Cop supports --auto-correct.
577577
# Configuration parameters: EnforcedStyle.
578578
# SupportedStyles: when_needed, always, never
@@ -633,14 +633,12 @@ Style/MultilineBlockChain:
633633
- 'lib/annotate/annotate_models.rb'
634634
- 'spec/spec_helper.rb'
635635

636-
# Offense count: 5
636+
# Offense count: 3
637637
# Cop supports --auto-correct.
638638
# Configuration parameters: EnforcedStyle.
639639
# SupportedStyles: literals, strict
640640
Style/MutableConstant:
641641
Exclude:
642-
- 'lib/annotate.rb'
643-
- 'lib/annotate/annotate_models.rb'
644642
- 'lib/annotate/annotate_routes.rb'
645643
- 'spec/integration/rails_2.3_with_bundler/config/boot.rb'
646644
- 'spec/integration/rails_2.3_with_bundler/config/environment.rb'
@@ -778,7 +776,7 @@ Style/StderrPuts:
778776
- 'lib/annotate/annotate_models.rb'
779777
- 'spec/integration/rails_2.3_with_bundler/config/boot.rb'
780778

781-
# Offense count: 247
779+
# Offense count: 249
782780
# Cop supports --auto-correct.
783781
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
784782
# SupportedStyles: single_quotes, double_quotes
@@ -830,7 +828,7 @@ Style/UnneededPercentQ:
830828
- 'annotate.gemspec'
831829
- 'spec/integration/rails_2.3_with_bundler/config/boot.rb'
832830

833-
# Offense count: 465
831+
# Offense count: 477
834832
# Cop supports --auto-correct.
835833
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
836834
# URISchemes: http, https

Diff for: lib/annotate.rb

+11-66
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'annotate/annotate_models'
66
require 'annotate/annotate_routes'
77
require 'annotate/constants'
8+
require 'annotate/helpers'
89

910
begin
1011
# ActiveSupport 3.x...
@@ -17,36 +18,6 @@
1718
end
1819

1920
module Annotate
20-
##
21-
# The set of available options to customize the behavior of Annotate.
22-
#
23-
POSITION_OPTIONS = [
24-
:position_in_routes, :position_in_class, :position_in_test,
25-
:position_in_fixture, :position_in_factory, :position,
26-
:position_in_serializer
27-
].freeze
28-
FLAG_OPTIONS = [
29-
:show_indexes, :simple_indexes, :include_version, :exclude_tests,
30-
:exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
31-
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :frozen,
32-
:trace, :timestamp, :exclude_serializers, :classified_sort,
33-
:show_foreign_keys, :show_complete_foreign_keys,
34-
:exclude_scaffolds, :exclude_controllers, :exclude_helpers,
35-
:exclude_sti_subclasses, :ignore_unknown_models, :with_comment
36-
].freeze
37-
OTHER_OPTIONS = [
38-
:additional_file_patterns, :ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close,
39-
:wrapper, :routes, :models, :hide_limit_column_types, :hide_default_column_types,
40-
:ignore_routes, :active_admin
41-
].freeze
42-
PATH_OPTIONS = [
43-
:require, :model_dir, :root_dir
44-
].freeze
45-
46-
def self.all_options
47-
[POSITION_OPTIONS, FLAG_OPTIONS, PATH_OPTIONS, OTHER_OPTIONS]
48-
end
49-
5021
##
5122
# Set default values that can be overridden via environment variables.
5223
#
@@ -56,7 +27,7 @@ def self.set_defaults(options = {})
5627

5728
options = ActiveSupport::HashWithIndifferentAccess.new(options)
5829

59-
all_options.flatten.each do |key|
30+
Constants::ALL_ANNOTATE_OPTIONS.flatten.each do |key|
6031
if options.key?(key)
6132
default_value = if options[key].is_a?(Array)
6233
options[key].join(',')
@@ -74,16 +45,16 @@ def self.set_defaults(options = {})
7445
# TODO: what is the difference between this and set_defaults?
7546
#
7647
def self.setup_options(options = {})
77-
POSITION_OPTIONS.each do |key|
78-
options[key] = fallback(ENV[key.to_s], ENV['position'], 'before')
48+
Constants::POSITION_OPTIONS.each do |key|
49+
options[key] = Annotate::Helpers.fallback(ENV[key.to_s], ENV['position'], 'before')
7950
end
80-
FLAG_OPTIONS.each do |key|
81-
options[key] = true?(ENV[key.to_s])
51+
Constants::FLAG_OPTIONS.each do |key|
52+
options[key] = Annotate::Helpers.true?(ENV[key.to_s])
8253
end
83-
OTHER_OPTIONS.each do |key|
54+
Constants::OTHER_OPTIONS.each do |key|
8455
options[key] = !ENV[key.to_s].blank? ? ENV[key.to_s] : nil
8556
end
86-
PATH_OPTIONS.each do |key|
57+
Constants::PATH_OPTIONS.each do |key|
8758
options[key] = !ENV[key.to_s].blank? ? ENV[key.to_s].split(',') : []
8859
end
8960

@@ -94,29 +65,13 @@ def self.setup_options(options = {})
9465
options[:wrapper_close] ||= options[:wrapper]
9566

9667
# These were added in 2.7.0 but so this is to revert to old behavior by default
97-
options[:exclude_scaffolds] = Annotate.true?(ENV.fetch('exclude_scaffolds', 'true'))
98-
options[:exclude_controllers] = Annotate.true?(ENV.fetch('exclude_controllers', 'true'))
99-
options[:exclude_helpers] = Annotate.true?(ENV.fetch('exclude_helpers', 'true'))
68+
options[:exclude_scaffolds] = Annotate::Helpers.true?(ENV.fetch('exclude_scaffolds', 'true'))
69+
options[:exclude_controllers] = Annotate::Helpers.true?(ENV.fetch('exclude_controllers', 'true'))
70+
options[:exclude_helpers] = Annotate::Helpers.true?(ENV.fetch('exclude_helpers', 'true'))
10071

10172
options
10273
end
10374

104-
def self.reset_options
105-
all_options.flatten.each { |key| ENV[key.to_s] = nil }
106-
end
107-
108-
def self.skip_on_migration?
109-
ENV['ANNOTATE_SKIP_ON_DB_MIGRATE'] =~ Constants::TRUE_RE || ENV['skip_on_db_migrate'] =~ Constants::TRUE_RE
110-
end
111-
112-
def self.include_routes?
113-
ENV['routes'] =~ Constants::TRUE_RE
114-
end
115-
116-
def self.include_models?
117-
ENV['models'] =~ Constants::TRUE_RE
118-
end
119-
12075
def self.loaded_tasks=(val)
12176
@loaded_tasks = val
12277
end
@@ -191,14 +146,4 @@ def self.bootstrap_rake
191146
load_tasks
192147
Rake::Task[:set_annotation_options].invoke
193148
end
194-
195-
def self.fallback(*args)
196-
args.detect { |arg| !arg.blank? }
197-
end
198-
199-
def self.true?(val)
200-
return false if val.blank?
201-
return false unless val =~ Constants::TRUE_RE
202-
true
203-
end
204149
end

Diff for: lib/annotate/constants.rb

+33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
module Annotate
22
module Constants
33
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_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
437
end
538
end

Diff for: lib/annotate/helpers.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
return false if val.blank?
19+
return false unless val =~ Constants::TRUE_RE
20+
21+
true
22+
end
23+
24+
def fallback(*args)
25+
args.detect { |arg| !arg.blank? }
26+
end
27+
28+
def reset_options(options)
29+
options.flatten.each { |key| ENV[key.to_s] = nil }
30+
end
31+
end
32+
end
33+
end

Diff for: lib/tasks/annotate_models.rake

+35-35
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,46 @@ task annotate_models: :environment do
1111
require "#{annotate_lib}/annotate/active_record_patch"
1212

1313
options = {is_rake: true}
14-
ENV['position'] = options[:position] = Annotate.fallback(ENV['position'], 'before')
14+
ENV['position'] = options[:position] = Annotate::Helpers.fallback(ENV['position'], 'before')
1515
options[:additional_file_patterns] = ENV['additional_file_patterns'] ? ENV['additional_file_patterns'].split(',') : []
16-
options[:position_in_class] = Annotate.fallback(ENV['position_in_class'], ENV['position'])
17-
options[:position_in_fixture] = Annotate.fallback(ENV['position_in_fixture'], ENV['position'])
18-
options[:position_in_factory] = Annotate.fallback(ENV['position_in_factory'], ENV['position'])
19-
options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position'])
20-
options[:position_in_serializer] = Annotate.fallback(ENV['position_in_serializer'], ENV['position'])
21-
options[:show_foreign_keys] = Annotate.true?(ENV['show_foreign_keys'])
22-
options[:show_complete_foreign_keys] = Annotate.true?(ENV['show_complete_foreign_keys'])
23-
options[:show_indexes] = Annotate.true?(ENV['show_indexes'])
24-
options[:simple_indexes] = Annotate.true?(ENV['simple_indexes'])
16+
options[:position_in_class] = Annotate::Helpers.fallback(ENV['position_in_class'], ENV['position'])
17+
options[:position_in_fixture] = Annotate::Helpers.fallback(ENV['position_in_fixture'], ENV['position'])
18+
options[:position_in_factory] = Annotate::Helpers.fallback(ENV['position_in_factory'], ENV['position'])
19+
options[:position_in_test] = Annotate::Helpers.fallback(ENV['position_in_test'], ENV['position'])
20+
options[:position_in_serializer] = Annotate::Helpers.fallback(ENV['position_in_serializer'], ENV['position'])
21+
options[:show_foreign_keys] = Annotate::Helpers.true?(ENV['show_foreign_keys'])
22+
options[:show_complete_foreign_keys] = Annotate::Helpers.true?(ENV['show_complete_foreign_keys'])
23+
options[:show_indexes] = Annotate::Helpers.true?(ENV['show_indexes'])
24+
options[:simple_indexes] = Annotate::Helpers.true?(ENV['simple_indexes'])
2525
options[:model_dir] = ENV['model_dir'] ? ENV['model_dir'].split(',') : ['app/models']
2626
options[:root_dir] = ENV['root_dir']
27-
options[:include_version] = Annotate.true?(ENV['include_version'])
27+
options[:include_version] = Annotate::Helpers.true?(ENV['include_version'])
2828
options[:require] = ENV['require'] ? ENV['require'].split(',') : []
29-
options[:exclude_tests] = Annotate.true?(ENV['exclude_tests'])
30-
options[:exclude_factories] = Annotate.true?(ENV['exclude_factories'])
31-
options[:exclude_fixtures] = Annotate.true?(ENV['exclude_fixtures'])
32-
options[:exclude_serializers] = Annotate.true?(ENV['exclude_serializers'])
33-
options[:exclude_scaffolds] = Annotate.true?(ENV['exclude_scaffolds'])
34-
options[:exclude_controllers] = Annotate.true?(ENV.fetch('exclude_controllers', 'true'))
35-
options[:exclude_helpers] = Annotate.true?(ENV.fetch('exclude_helpers', 'true'))
36-
options[:exclude_sti_subclasses] = Annotate.true?(ENV['exclude_sti_subclasses'])
37-
options[:ignore_model_sub_dir] = Annotate.true?(ENV['ignore_model_sub_dir'])
38-
options[:format_bare] = Annotate.true?(ENV['format_bare'])
39-
options[:format_rdoc] = Annotate.true?(ENV['format_rdoc'])
40-
options[:format_markdown] = Annotate.true?(ENV['format_markdown'])
41-
options[:sort] = Annotate.true?(ENV['sort'])
42-
options[:force] = Annotate.true?(ENV['force'])
43-
options[:frozen] = Annotate.true?(ENV['frozen'])
44-
options[:classified_sort] = Annotate.true?(ENV['classified_sort'])
45-
options[:trace] = Annotate.true?(ENV['trace'])
46-
options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper'])
47-
options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper'])
29+
options[:exclude_tests] = Annotate::Helpers.true?(ENV['exclude_tests'])
30+
options[:exclude_factories] = Annotate::Helpers.true?(ENV['exclude_factories'])
31+
options[:exclude_fixtures] = Annotate::Helpers.true?(ENV['exclude_fixtures'])
32+
options[:exclude_serializers] = Annotate::Helpers.true?(ENV['exclude_serializers'])
33+
options[:exclude_scaffolds] = Annotate::Helpers.true?(ENV['exclude_scaffolds'])
34+
options[:exclude_controllers] = Annotate::Helpers.true?(ENV.fetch('exclude_controllers', 'true'))
35+
options[:exclude_helpers] = Annotate::Helpers.true?(ENV.fetch('exclude_helpers', 'true'))
36+
options[:exclude_sti_subclasses] = Annotate::Helpers.true?(ENV['exclude_sti_subclasses'])
37+
options[:ignore_model_sub_dir] = Annotate::Helpers.true?(ENV['ignore_model_sub_dir'])
38+
options[:format_bare] = Annotate::Helpers.true?(ENV['format_bare'])
39+
options[:format_rdoc] = Annotate::Helpers.true?(ENV['format_rdoc'])
40+
options[:format_markdown] = Annotate::Helpers.true?(ENV['format_markdown'])
41+
options[:sort] = Annotate::Helpers.true?(ENV['sort'])
42+
options[:force] = Annotate::Helpers.true?(ENV['force'])
43+
options[:frozen] = Annotate::Helpers.true?(ENV['frozen'])
44+
options[:classified_sort] = Annotate::Helpers.true?(ENV['classified_sort'])
45+
options[:trace] = Annotate::Helpers.true?(ENV['trace'])
46+
options[:wrapper_open] = Annotate::Helpers.fallback(ENV['wrapper_open'], ENV['wrapper'])
47+
options[:wrapper_close] = Annotate::Helpers.fallback(ENV['wrapper_close'], ENV['wrapper'])
4848
options[:ignore_columns] = ENV.fetch('ignore_columns', nil)
4949
options[:ignore_routes] = ENV.fetch('ignore_routes', nil)
50-
options[:hide_limit_column_types] = Annotate.fallback(ENV['hide_limit_column_types'], '')
51-
options[:hide_default_column_types] = Annotate.fallback(ENV['hide_default_column_types'], '')
52-
options[:with_comment] = Annotate.true?(ENV['with_comment'])
53-
options[:ignore_unknown_models] = Annotate.true?(ENV.fetch('ignore_unknown_models', 'false'))
50+
options[:hide_limit_column_types] = Annotate::Helpers.fallback(ENV['hide_limit_column_types'], '')
51+
options[:hide_default_column_types] = Annotate::Helpers.fallback(ENV['hide_default_column_types'], '')
52+
options[:with_comment] = Annotate::Helpers.true?(ENV['with_comment'])
53+
options[:ignore_unknown_models] = Annotate::Helpers.true?(ENV.fetch('ignore_unknown_models', 'false'))
5454

5555
AnnotateModels.do_annotations(options)
5656
end
@@ -64,6 +64,6 @@ task remove_annotation: :environment do
6464
options[:model_dir] = ENV['model_dir']
6565
options[:root_dir] = ENV['root_dir']
6666
options[:require] = ENV['require'] ? ENV['require'].split(',') : []
67-
options[:trace] = Annotate.true?(ENV['trace'])
67+
options[:trace] = Annotate::Helpers.true?(ENV['trace'])
6868
AnnotateModels.remove_annotations(options)
6969
end

0 commit comments

Comments
 (0)