Module: Metz::FileClassifier
- Defined in:
- lib/metz/file_classifier.rb
Overview
Path-based classifier for Rails-shaped source files. Used by the
Rails-aware cops (Metz/ControllersTooManyDirectCollaborators,
Metz/ViewsDeepNavigation) to decide whether a file is in scope for
those cops. Predicates accept either a relative or an absolute path and
always return a strict boolean.
The matching rules are deliberately syntactic: a path is a controller
iff some path segment chain ends in app/controllers/<...>.rb, a view
iff app/views/<...>.{erb,haml,slim}, a model iff app/models/<...>.rb.
We never touch the filesystem from here -- the inputs are already paths
that RuboCop or a caller has handed us.
Phase 3 reuse: future cross-file analyzers (project index, service-soup detector, repeated-branching detector) need the same path-shape question answered. Keep predicates pure, allocation-light, and dependency-free so they can be reused as-is from the wrapper or future analyzers.
Constant Summary collapse
- CONTROLLER_PATTERN =
%r{(?:\A|/)app/controllers/[^\0]+\.rb\z}- VIEW_PATTERN =
%r{(?:\A|/)app/views/[^\0]+\.(?:erb|haml|slim)\z}- MODEL_PATTERN =
%r{(?:\A|/)app/models/[^\0]+\.rb\z}
Class Method Summary collapse
- .controller?(path) ⇒ Boolean
- .model?(path) ⇒ Boolean
-
.normalize(path) ⇒ Object
Windows-style paths arrive with backslash separators when the caller is an editor running on Windows or a CI step that shells out via
cmd.exe. - .view?(path) ⇒ Boolean
Class Method Details
.controller?(path) ⇒ Boolean
27 28 29 |
# File 'lib/metz/file_classifier.rb', line 27 def controller?(path) normalize(path).match?(CONTROLLER_PATTERN) end |
.model?(path) ⇒ Boolean
35 36 37 |
# File 'lib/metz/file_classifier.rb', line 35 def model?(path) normalize(path).match?(MODEL_PATTERN) end |
.normalize(path) ⇒ Object
Windows-style paths arrive with backslash separators when the caller is
an editor running on Windows or a CI step that shells out via cmd.exe.
Normalize to forward slashes so the regexes below stay POSIX-shaped.
42 43 44 |
# File 'lib/metz/file_classifier.rb', line 42 def normalize(path) path.to_s.tr("\\", "/") end |
.view?(path) ⇒ Boolean
31 32 33 |
# File 'lib/metz/file_classifier.rb', line 31 def view?(path) normalize(path).match?(VIEW_PATTERN) end |