Class: Uniword::FindReplace::Result
- Inherits:
-
Object
- Object
- Uniword::FindReplace::Result
- 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.
Instance Attribute Summary collapse
-
#by_scope ⇒ Object
readonly
Returns the value of attribute by_scope.
Instance Method Summary collapse
-
#add(scope, substitutions) ⇒ void
Increment the count for one scope.
-
#count ⇒ Integer
Total substitutions across all scopes.
-
#empty? ⇒ Boolean
True when zero substitutions were made.
-
#initialize ⇒ Result
constructor
A new instance of Result.
-
#scopes_touched ⇒ Array<Symbol>
Scopes that produced at least one substitution.
Constructor Details
#initialize ⇒ Result
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_scope ⇒ Object (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.
28 29 30 |
# File 'lib/uniword/find_replace/result.rb', line 28 def add(scope, substitutions) @by_scope[scope] += substitutions end |
#count ⇒ Integer
Total substitutions across all scopes.
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.
49 50 51 |
# File 'lib/uniword/find_replace/result.rb', line 49 def empty? count.zero? end |
#scopes_touched ⇒ Array<Symbol>
Scopes that produced at least one substitution.
42 43 44 |
# File 'lib/uniword/find_replace/result.rb', line 42 def scopes_touched @by_scope.select { |_, c| c.positive? }.keys end |