Class: Kotoshu::Models::Result::DocumentResult

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/core/models/result/document_result.rb

Overview

Note:

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.

Examples:

Creating a successful document result

result = DocumentResult.new(
  file: "README.md",
  errors: [],
  word_count: 150
)
result.success?     # => true
result.error_count  # => 0

Creating a result with errors

errors = [WordResult.incorrect("helo"), WordResult.incorrect("wrold")]
result = DocumentResult.new(
  file: "document.txt",
  errors: errors,
  word_count: 100
)
result.success?     # => false
result.error_count  # => 2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file: nil, errors: [], word_count: 0, metadata: {}) ⇒ DocumentResult

Create a new DocumentResult.

Parameters:

  • file (String, nil) (defaults to: nil)

    The file path (optional)

  • errors (Array<WordResult>) (defaults to: [])

    List of errors

  • word_count (Integer) (defaults to: 0)

    Total word count

  • metadata (Hash) (defaults to: {})

    Additional metadata (optional)



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

#errorsArray<WordResult> (readonly)

Returns List of spelling errors found.

Returns:

  • (Array<WordResult>)

    List of spelling errors found



38
39
40
# File 'lib/kotoshu/core/models/result/document_result.rb', line 38

def errors
  @errors
end

#fileString? (readonly)

Returns The file path (if applicable).

Returns:

  • (String, nil)

    The file path (if applicable)



35
36
37
# File 'lib/kotoshu/core/models/result/document_result.rb', line 35

def file
  @file
end

#metadataHash (readonly)

Returns Additional metadata.

Returns:

  • (Hash)

    Additional metadata



44
45
46
# File 'lib/kotoshu/core/models/result/document_result.rb', line 44

def 
  @metadata
end

#word_countInteger (readonly)

Returns Total word count.

Returns:

  • (Integer)

    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.

Examples:

errors = [WordResult.incorrect("helo"), WordResult.incorrect("wrold")]
DocumentResult.failure(file: "doc.txt", errors: errors, word_count: 100)

Parameters:

  • file (String, nil) (defaults to: nil)

    The file path (optional)

  • errors (Array<WordResult>) (defaults to: [])

    List of errors

  • word_count (Integer) (defaults to: 0)

    Total word count

Returns:



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.

Examples:

Merging results from multiple files

result1 = DocumentResult.new(file: "file1.txt", errors: [e1], word_count: 50)
result2 = DocumentResult.new(file: "file2.txt", errors: [e2, e3], word_count: 75)
DocumentResult.merge([result1, result2])
# => DocumentResult with 3 errors and 125 words

Parameters:

Returns:



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.

Examples:

DocumentResult.success(file: "README.md", word_count: 150)

Parameters:

  • file (String, nil) (defaults to: nil)

    The file path (optional)

  • word_count (Integer) (defaults to: 0)

    Total word count

Returns:



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.

Parameters:

Returns:

  • (Boolean)

    True if equal



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_jsonHash

Convert to JSON-compatible hash.

Returns:

  • (Hash)

    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.

Yields:

  • (error)

    Each error

Returns:

  • (Enumerator)

    Enumerator if no block given



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.

Yields:

  • (word, errors)

    Each unique word and its errors

Returns:

  • (Enumerator)

    Enumerator if no block given



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_countInteger

Get the number of errors found.

Returns:

  • (Integer)

    Error count



78
79
80
# File 'lib/kotoshu/core/models/result/document_result.rb', line 78

def error_count
  @errors.size
end

#error_summaryHash

Get error summary as a hash.

Returns:

  • (Hash)

    Summary of errors



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.

Parameters:

  • word (String)

    The word

Returns:



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

Returns:

  • (Boolean)

    True if errors were found



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.

Parameters:

  • n (Integer) (defaults to: 10)

    Number of errors to return

Returns:



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.

Parameters:

  • word (String)

    The word to check

Returns:

  • (Boolean)

    True if the 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

#hashInteger

Hash based on file and errors.

Returns:

  • (Integer)

    Hash code



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

Returns:

  • (Boolean)

    True if no errors were found



64
65
66
# File 'lib/kotoshu/core/models/result/document_result.rb', line 64

def success?
  @errors.empty?
end

#to_hHash

Convert to hash.

Returns:

  • (Hash)

    Hash representation



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_sString Also known as: inspect

String representation.

Returns:

  • (String)

    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_countInteger

Get the number of unique errors (by word).

Returns:

  • (Integer)

    Unique error count



85
86
87
# File 'lib/kotoshu/core/models/result/document_result.rb', line 85

def unique_error_count
  @errors.map(&:word).uniq.size
end