Module: Woods::ModelNameCache
- Defined in:
- lib/woods/model_name_cache.rb
Overview
Caches ActiveRecord model names and builds a precompiled regex for scanning source code for model references.
Avoids O(n*m) per-extractor iteration of ActiveRecord::Base.descendants. Invalidated per extraction run (call .reset! before a new run).
Provides two resolution layers:
-
ModelNameCache.model_names_regex — whole-word match against every fully-qualified model name. Catches ‘User`, `Library::Book`, and `“Library::Book”` (as a string literal) because `b` treats `:` and `“` as boundaries.
-
ModelNameCache.resolve_short_name — when source references the bare inner name (e.g. ‘Book.new` inside `module Library`), resolve it back to its fully-qualified owner when the short name is unambiguous. Needed because the cache holds `Library::Book` but the source writes `Book` after a `module Library` opens.
Class Method Summary collapse
-
.model_names ⇒ Array<String>
All named AR model descendant names.
-
.model_names_regex ⇒ Regexp
Precompiled regex matching any model name as a whole word.
-
.reset! ⇒ Object
Clear cache (call at the start of each extraction run).
-
.resolve_short_name(short) ⇒ String?
Resolve a bare short name (e.g. ‘Book`) to its fully-qualified owner (`Library::Book`) when unambiguous.
-
.short_name_map ⇒ Hash{String => String, nil}
Short-name → fully-qualified owner mapping.
-
.short_names_regex ⇒ Regexp
Regex matching bare short names of namespaced models.
Class Method Details
.model_names ⇒ Array<String>
Returns All named AR model descendant names.
33 34 35 |
# File 'lib/woods/model_name_cache.rb', line 33 def model_names @model_names ||= compute_model_names end |
.model_names_regex ⇒ Regexp
Returns Precompiled regex matching any model name as a whole word.
38 39 40 |
# File 'lib/woods/model_name_cache.rb', line 38 def model_names_regex @model_names_regex ||= build_regex end |
.reset! ⇒ Object
Clear cache (call at the start of each extraction run)
71 72 73 74 75 76 |
# File 'lib/woods/model_name_cache.rb', line 71 def reset! @model_names = nil @model_names_regex = nil @short_name_map = nil @short_names_regex = nil end |
.resolve_short_name(short) ⇒ String?
Resolve a bare short name (e.g. ‘Book`) to its fully-qualified owner (`Library::Book`) when unambiguous. Returns nil otherwise.
57 58 59 |
# File 'lib/woods/model_name_cache.rb', line 57 def resolve_short_name(short) short_name_map[short.to_s] end |
.short_name_map ⇒ Hash{String => String, nil}
Short-name → fully-qualified owner mapping. Ambiguous short names (two different models sharing the same inner name) map to nil so callers can detect the collision and skip the edge rather than guess.
48 49 50 |
# File 'lib/woods/model_name_cache.rb', line 48 def short_name_map @short_name_map ||= build_short_name_map end |
.short_names_regex ⇒ Regexp
Regex matching bare short names of namespaced models. Used by the dependency scanner to surface references like ‘Book.new` inside the `Library` module, which the full-name regex misses.
66 67 68 |
# File 'lib/woods/model_name_cache.rb', line 66 def short_names_regex @short_names_regex ||= build_short_names_regex end |