Class: Kotoshu::Models::Result::DocumentResult
- Inherits:
-
Object
- Object
- Kotoshu::Models::Result::DocumentResult
- Defined in:
- lib/kotoshu/core/models/result/document_result.rb
Overview
This class is immutable and frozen on initialization.
Result object for checking a document or file.
This is a value object that represents the result of checking an entire document for spelling errors.
Instance Attribute Summary collapse
-
#errors ⇒ Array<WordResult>
readonly
List of spelling errors found.
-
#file ⇒ String?
readonly
The file path (if applicable).
-
#metadata ⇒ Hash
readonly
Additional metadata.
-
#word_count ⇒ Integer
readonly
Total word count.
Class Method Summary collapse
-
.failure(file: nil, errors: [], word_count: 0) ⇒ DocumentResult
Create a failed document result.
-
.merge(results) ⇒ DocumentResult
Merge multiple document results.
-
.success(file: nil, word_count: 0) ⇒ DocumentResult
Create a successful document result.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality based on file and errors.
-
#as_json ⇒ Hash
Convert to JSON-compatible hash.
-
#each_error {|error| ... } ⇒ Enumerator
Iterate over errors.
-
#each_unique_error {|word, errors| ... } ⇒ Enumerator
Iterate over unique error words.
-
#error_count ⇒ Integer
Get the number of errors found.
-
#error_summary ⇒ Hash
Get error summary as a hash.
-
#errors_for(word) ⇒ Array<WordResult>
Get errors for a specific word.
-
#failed? ⇒ Boolean
Check if the document check failed (has errors).
-
#first_errors(n = 10) ⇒ Array<WordResult>
Get the first N errors.
-
#has_error_for?(word) ⇒ Boolean
Check if a specific word has an error.
-
#hash ⇒ Integer
Hash based on file and errors.
-
#initialize(file: nil, errors: [], word_count: 0, metadata: {}) ⇒ DocumentResult
constructor
Create a new DocumentResult.
-
#success? ⇒ Boolean
Check if the document check was successful (no errors).
-
#to_h ⇒ Hash
Convert to hash.
-
#to_s ⇒ String
(also: #inspect)
String representation.
-
#unique_error_count ⇒ Integer
Get the number of unique errors (by word).
Constructor Details
#initialize(file: nil, errors: [], word_count: 0, metadata: {}) ⇒ DocumentResult
Create a new DocumentResult.
52 53 54 55 56 57 58 59 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 52 def initialize(file: nil, errors: [], word_count: 0, metadata: {}) @file = file&.dup&.freeze @errors = errors.dup.freeze @word_count = word_count @metadata = .dup.freeze freeze end |
Instance Attribute Details
#errors ⇒ Array<WordResult> (readonly)
Returns List of spelling errors found.
38 39 40 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 38 def errors @errors end |
#file ⇒ String? (readonly)
Returns The file path (if applicable).
35 36 37 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 35 def file @file end |
#metadata ⇒ Hash (readonly)
Returns Additional metadata.
44 45 46 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 44 def @metadata end |
#word_count ⇒ Integer (readonly)
Returns Total word count.
41 42 43 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 41 def word_count @word_count end |
Class Method Details
.failure(file: nil, errors: [], word_count: 0) ⇒ DocumentResult
Create a failed document result.
234 235 236 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 234 def self.failure(file: nil, errors: [], word_count: 0) new(file: file, errors: errors, word_count: word_count) end |
.merge(results) ⇒ DocumentResult
Merge multiple document results.
248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 248 def self.merge(results) return new if results.empty? all_errors = results.flat_map(&:errors) total_words = results.sum(&:word_count) new( file: nil, # Merged results don't have a single file errors: all_errors, word_count: total_words ) end |
.success(file: nil, word_count: 0) ⇒ DocumentResult
Create a successful document result.
220 221 222 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 220 def self.success(file: nil, word_count: 0) new(file: file, errors: [], word_count: word_count) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Check equality based on file and errors.
180 181 182 183 184 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 180 def ==(other) return false unless other.is_a?(DocumentResult) @file == other.file && @errors == other.errors end |
#as_json ⇒ Hash
Convert to JSON-compatible hash.
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 163 def as_json { "file" => @file, "success" => success?, "wordCount" => @word_count, "errorCount" => error_count, "uniqueErrorCount" => unique_error_count, "errors" => @errors.map(&:as_json), "errorSummary" => error_summary, "metadata" => @metadata } end |
#each_error {|error| ... } ⇒ Enumerator
Iterate over errors.
109 110 111 112 113 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 109 def each_error(&block) return enum_for(:each_error) unless block_given? @errors.each(&block) end |
#each_unique_error {|word, errors| ... } ⇒ Enumerator
Iterate over unique error words.
119 120 121 122 123 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 119 def each_unique_error(&block) return enum_for(:each_unique_error) unless block_given? @errors.group_by(&:word).each(&block) end |
#error_count ⇒ Integer
Get the number of errors found.
78 79 80 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 78 def error_count @errors.size end |
#error_summary ⇒ Hash
Get error summary as a hash.
136 137 138 139 140 141 142 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 136 def error_summary summary = Hash.new(0) each_error do |error| summary[error.word] += 1 end summary end |
#errors_for(word) ⇒ Array<WordResult>
Get errors for a specific word.
101 102 103 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 101 def errors_for(word) @errors.select { |e| e.word == word } end |
#failed? ⇒ Boolean
Check if the document check failed (has errors).
71 72 73 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 71 def failed? !success? end |
#first_errors(n = 10) ⇒ Array<WordResult>
Get the first N errors.
129 130 131 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 129 def first_errors(n = 10) @errors.first(n) end |
#has_error_for?(word) ⇒ Boolean
Check if a specific word has an error.
93 94 95 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 93 def has_error_for?(word) @errors.any? { |e| e.word == word } end |
#hash ⇒ Integer
Hash based on file and errors.
190 191 192 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 190 def hash [@file, @errors].hash end |
#success? ⇒ Boolean
Check if the document check was successful (no errors).
64 65 66 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 64 def success? @errors.empty? end |
#to_h ⇒ Hash
Convert to hash.
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 147 def to_h { file: @file, success: success?, word_count: @word_count, error_count: error_count, unique_error_count: unique_error_count, errors: @errors.map(&:to_h), error_summary: error_summary, metadata: @metadata } end |
#to_s ⇒ String Also known as: inspect
String representation.
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 197 def to_s if success? if @file "File '#{@file}': No spelling errors found (#{@word_count} words checked)" else "No spelling errors found (#{@word_count} words checked)" end else prefix = @file ? "File '#{@file}':" : "" "#{prefix} #{error_count} spelling error(s) found " \ "(#{unique_error_count} unique) in #{@word_count} words" end end |
#unique_error_count ⇒ Integer
Get the number of unique errors (by word).
85 86 87 |
# File 'lib/kotoshu/core/models/result/document_result.rb', line 85 def unique_error_count @errors.map(&:word).uniq.size end |