Class: Uniword::FindReplace::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/find_replace/engine.rb

Overview

Orchestrates a find-replace pass over a package.

Owns the scope registry (which parts to scan) and matcher dispatch (literal vs regex). Returns a Result with total count and per-scope breakdown.

Open/closed: scopes register in SCOPE_REGISTRY; adding one is data, not behavior. The matcher class is chosen by the regex: flag, but callers can pass a custom Matcher instance to bypass the flag entirely.

Constant Summary collapse

SCOPE_REGISTRY =

Symbol => Scope subclass. Adding a scope = adding an entry.

{
  body: BodyScope,
  headers: HeaderScope,
  footers: FooterScope,
  footnotes: FootnoteScope,
  endnotes: EndnoteScope,
  comments: CommentScope,
  styles: StylesScope,
}.freeze
ALL_SCOPES =

All registered scope names. Used by :all to expand.

SCOPE_REGISTRY.keys.freeze

Instance Method Summary collapse

Constructor Details

#initialize(document:, matcher:, scopes: :all) ⇒ Engine

Returns a new instance of Engine.

Parameters:



34
35
36
37
38
# File 'lib/uniword/find_replace/engine.rb', line 34

def initialize(document:, matcher:, scopes: :all)
  @document = document
  @matcher = matcher
  @scopes = resolve_scopes(scopes)
end

Instance Method Details

#runResult

Run the find-replace pass.

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/uniword/find_replace/engine.rb', line 43

def run
  result = Result.new
  return result if @matcher.nil? || @scopes.empty?

  @scopes.each do |scope_name|
    scope_class = SCOPE_REGISTRY.fetch(scope_name)
    scope = scope_class.new(@document)
    substitutions_in_scope = apply_scope(scope)
    result.add(scope_name, substitutions_in_scope)
  end
  result
end