From 63a0886d60670d37a52723e77d81efa01b522f69 Mon Sep 17 00:00:00 2001 From: Matias Korhonen Date: Thu, 18 Sep 2025 15:38:07 +0300 Subject: [PATCH] Make it possible to filter files by Regexp --- .../model_annotator/model_files_getter.rb | 5 +++++ lib/annotate_rb/options.rb | 1 + .../model_annotator/model_files_getter_spec.rb | 12 ++++++++++++ 3 files changed, 18 insertions(+) diff --git a/lib/annotate_rb/model_annotator/model_files_getter.rb b/lib/annotate_rb/model_annotator/model_files_getter.rb index 5e10f6ef..8710cf87 100644 --- a/lib/annotate_rb/model_annotator/model_files_getter.rb +++ b/lib/annotate_rb/model_annotator/model_files_getter.rb @@ -27,6 +27,11 @@ def call(options) end end + if options[:ignore_filename_regexp] + regexp = Regexp.new(options[:ignore_filename_regexp]) + model_files.reject! { |_, file| File.basename(file).match?(regexp) } + end + if model_files.empty? warn "No models found in directory '#{options[:model_dir].join("', '")}'." warn "Either specify models on the command line, or use the --model-dir option." diff --git a/lib/annotate_rb/options.rb b/lib/annotate_rb/options.rb index 2a3b3d32..f9c97d34 100644 --- a/lib/annotate_rb/options.rb +++ b/lib/annotate_rb/options.rb @@ -90,6 +90,7 @@ def from(options = {}, state = {}) PATH_OPTIONS = { additional_file_patterns: [], # ModelAnnotator + ignore_filename_regexp: nil, # ModelAnnotator model_dir: ["app/models"], # ModelAnnotator require: [], # Core root_dir: [""] # Core; Old model Annotate code depends on it being empty when not provided another value diff --git a/spec/lib/annotate_rb/model_annotator/model_files_getter_spec.rb b/spec/lib/annotate_rb/model_annotator/model_files_getter_spec.rb index 5f680b02..c4c560f1 100644 --- a/spec/lib/annotate_rb/model_annotator/model_files_getter_spec.rb +++ b/spec/lib/annotate_rb/model_annotator/model_files_getter_spec.rb @@ -47,6 +47,18 @@ is_expected.to contain_exactly([model_dir, "foo.rb"]) end end + + context "when `ignore_file_regexp` option is enabled" do + let(:base_options) { {model_dir: [model_dir], ignore_filename_regexp: '\Aquux\.rb\z'} } + let(:options) { AnnotateRb::Options.new(base_options, {working_args: []}) } + + it "returns model files that do not match the regexp" do + is_expected.to contain_exactly( + [model_dir, "foo.rb"], + [model_dir, File.join("bar", "baz.rb")], + ) + end + end end context "when the model files are specified" do