Skip to content

Commit 1fec9c9

Browse files
committed
[rail_inspector] Unify interface for files
Its difficult to test some of these classes because they load files very internally. This commit does some refactoring to unify file lookup with the goal being a future commit will extract most of this up a bunch of layers.
1 parent 7409209 commit 1fec9c9

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

tools/rail_inspector/lib/rail_inspector/configuring.rb

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,42 @@
66

77
module RailInspector
88
class Configuring
9-
class CachedParser
10-
def initialize
11-
@cache = {}
9+
class Files
10+
class Proxy
11+
def initialize(pathname)
12+
@pathname = pathname
13+
end
14+
15+
def parse
16+
@parse ||= Prism.parse_file(@pathname.to_s).value
17+
end
18+
19+
def read
20+
@pathname.read
21+
end
22+
23+
def write(string)
24+
@pathname.write(string)
25+
end
26+
27+
def to_s
28+
@pathname.to_s
29+
end
1230
end
1331

14-
def call(path)
15-
@cache[path] ||= Prism.parse_file(path.to_s).value
32+
def initialize(root)
33+
@root = Pathname.new(root)
34+
@files = {}
1635
end
17-
end
1836

19-
DOC_PATH = "guides/source/configuring.md"
20-
APPLICATION_CONFIGURATION_PATH =
21-
"railties/lib/rails/application/configuration.rb"
22-
NEW_FRAMEWORK_DEFAULTS_PATH =
23-
"railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_%{version}.rb.tt"
37+
def []=(name, path)
38+
@files[name] = Proxy.new(@root.join(path))
39+
end
40+
41+
def method_missing(name, ...)
42+
@files[name] || super
43+
end
44+
end
2445

2546
class Doc
2647
attr_accessor :general_config, :versioned_defaults
@@ -45,12 +66,19 @@ def to_s
4566
end
4667
end
4768

48-
attr_reader :errors, :parser
69+
attr_reader :errors, :files
4970

5071
def initialize(rails_path)
5172
@errors = []
52-
@parser = CachedParser.new
53-
@rails_path = Pathname.new(rails_path)
73+
@files = Files.new(rails_path)
74+
75+
@files[:application_configuration] = "railties/lib/rails/application/configuration.rb"
76+
@files[:doc_path] = "guides/source/configuring.md"
77+
@files[:rails_version] = "RAILS_VERSION"
78+
79+
@files[:new_framework_defaults] = "railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_%{version}.rb.tt" % {
80+
version: rails_version.tr(".", "_")
81+
}
5482
end
5583

5684
def check
@@ -60,27 +88,15 @@ def check
6088
end
6189

6290
def doc
63-
@doc ||=
64-
begin
65-
content = File.read(doc_path)
66-
Configuring::Doc.new(content)
67-
end
68-
end
69-
70-
def parse(relative_path)
71-
parser.call(@rails_path.join(relative_path))
72-
end
73-
74-
def read(relative_path)
75-
File.read(@rails_path.join(relative_path))
91+
@doc ||= Configuring::Doc.new(files.doc_path.read)
7692
end
7793

7894
def rails_version
79-
@rails_version ||= File.read(@rails_path.join("RAILS_VERSION")).to_f.to_s
95+
@rails_version ||= files.rails_version.read.to_f.to_s
8096
end
8197

8298
def write!
83-
File.write(doc_path, doc.to_s)
99+
files.doc_path.write(doc.to_s)
84100
end
85101

86102
def error_message
@@ -90,10 +106,5 @@ def error_message
90106
"Make sure new configurations are added to configuring.md#rails-general-configuration in alphabetical order.\n" +
91107
"Errors may be autocorrectable with the --autocorrect flag"
92108
end
93-
94-
private
95-
def doc_path
96-
@rails_path.join(DOC_PATH)
97-
end
98109
end
99110
end

tools/rail_inspector/lib/rail_inspector/configuring/check/framework_defaults.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ def add_error(config)
3737
end
3838

3939
def defaults_file_content
40-
@defaults_file_content ||= checker.read(new_framework_defaults_path)
41-
end
42-
43-
def new_framework_defaults_path
44-
NEW_FRAMEWORK_DEFAULTS_PATH %
45-
{ version: checker.rails_version.tr(".", "_") }
40+
@defaults_file_content ||= checker.files.new_framework_defaults.read
4641
end
4742
end
4843

@@ -66,7 +61,7 @@ def check
6661

6762
private
6863
def app_config_tree
69-
checker.parse(APPLICATION_CONFIGURATION_PATH)
64+
checker.files.application_configuration.parse
7065
end
7166

7267
def check_defaults(defaults)
@@ -106,7 +101,7 @@ def check_defaults(defaults)
106101
end
107102

108103
checker.errors << <<~MESSAGE unless config_diff.empty?
109-
#{APPLICATION_CONFIGURATION_PATH}: Incorrect load_defaults docs
104+
#{checker.files.application_configuration}: Incorrect load_defaults docs
110105
--- Expected
111106
+++ Actual
112107
#{config_diff.split("\n")[5..].join("\n")}

tools/rail_inspector/lib/rail_inspector/configuring/check/general_configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def call
2121
APP_CONFIG_CONST = "Rails::Application::Configuration"
2222

2323
def app_config_tree
24-
@checker.parse(APPLICATION_CONFIGURATION_PATH)
24+
@checker.files.application_configuration.parse
2525
end
2626
end
2727

0 commit comments

Comments
 (0)