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:

  1. 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.

  2. 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.

Examples:

Woods::ModelNameCache.model_names
# => ["User", "Library::Book", ...]

Woods::ModelNameCache.model_names_regex
# => /\b(?:User|Library::Book|...)\b/

Woods::ModelNameCache.resolve_short_name("Book")
# => "Library::Book"   (or nil when ambiguous)

Class Method Summary collapse

Class Method Details

.model_namesArray<String>

Returns All named AR model descendant names.

Returns:

  • (Array<String>)

    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_regexRegexp

Returns Precompiled regex matching any model name as a whole word.

Returns:

  • (Regexp)

    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.

Parameters:

  • short (String)

Returns:

  • (String, nil)


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_mapHash{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.

Returns:

  • (Hash{String => String, nil})


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_regexRegexp

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.

Returns:

  • (Regexp)


66
67
68
# File 'lib/woods/model_name_cache.rb', line 66

def short_names_regex
  @short_names_regex ||= build_short_names_regex
end