Class: Uniword::FindReplace::Result

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

Overview

Aggregated result of a find-replace pass.

Counts total substitutions and breaks the count down by scope so callers can see where matches happened. Returned by Engine#run and DocumentRoot#find_replace.

Examples:

result = engine.run
result.count              # => 7
result.by_scope           # => { body: 5, headers: 2 }
result.scopes_touched     # => [:body, :headers]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResult

Returns a new instance of Result.



19
20
21
# File 'lib/uniword/find_replace/result.rb', line 19

def initialize
  @by_scope = Hash.new { |hash, key| hash[key] = 0 }
end

Instance Attribute Details

#by_scopeObject (readonly)

Returns the value of attribute by_scope.



17
18
19
# File 'lib/uniword/find_replace/result.rb', line 17

def by_scope
  @by_scope
end

Instance Method Details

#add(scope, substitutions) ⇒ void

This method returns an undefined value.

Increment the count for one scope.

Parameters:

  • scope (Symbol)

    scope name (e.g. :body, :headers)

  • substitutions (Integer)

    substitutions added



28
29
30
# File 'lib/uniword/find_replace/result.rb', line 28

def add(scope, substitutions)
  @by_scope[scope] += substitutions
end

#countInteger

Total substitutions across all scopes.

Returns:

  • (Integer)


35
36
37
# File 'lib/uniword/find_replace/result.rb', line 35

def count
  @by_scope.values.sum
end

#empty?Boolean

True when zero substitutions were made.

Returns:

  • (Boolean)


49
50
51
# File 'lib/uniword/find_replace/result.rb', line 49

def empty?
  count.zero?
end

#scopes_touchedArray<Symbol>

Scopes that produced at least one substitution.

Returns:

  • (Array<Symbol>)


42
43
44
# File 'lib/uniword/find_replace/result.rb', line 42

def scopes_touched
  @by_scope.select { |_, c| c.positive? }.keys
end