6
6
7
7
module RailInspector
8
8
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
12
30
end
13
31
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 = { }
16
35
end
17
- end
18
36
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
24
45
25
46
class Doc
26
47
attr_accessor :general_config , :versioned_defaults
@@ -45,12 +66,19 @@ def to_s
45
66
end
46
67
end
47
68
48
- attr_reader :errors , :parser
69
+ attr_reader :errors , :files
49
70
50
71
def initialize ( rails_path )
51
72
@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
+ }
54
82
end
55
83
56
84
def check
@@ -60,27 +88,15 @@ def check
60
88
end
61
89
62
90
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 )
76
92
end
77
93
78
94
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
80
96
end
81
97
82
98
def write!
83
- File . write ( doc_path , doc . to_s )
99
+ files . doc_path . write ( doc . to_s )
84
100
end
85
101
86
102
def error_message
@@ -90,10 +106,5 @@ def error_message
90
106
"Make sure new configurations are added to configuring.md#rails-general-configuration in alphabetical order.\n " +
91
107
"Errors may be autocorrectable with the --autocorrect flag"
92
108
end
93
-
94
- private
95
- def doc_path
96
- @rails_path . join ( DOC_PATH )
97
- end
98
109
end
99
110
end
0 commit comments