Class: Uniword::Diff::DiffResult

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/diff/diff_result.rb

Overview

Value object holding the result of comparing two documents.

Contains arrays of text, formatting, structural, metadata, and style changes discovered during a diff operation. Use #empty? to check if the two documents are identical.

Examples:

result = DocumentDiffer.new(old_doc, new_doc).diff
puts result.summary
puts result.to_json

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text_changes: [], format_changes: [], structure_changes: [], metadata_changes: {}, style_changes: []) ⇒ DiffResult

Initialize a DiffResult with change arrays.

Parameters:

  • text_changes (Array<Hash>) (defaults to: [])

    Text-level diffs

  • format_changes (Array<Hash>) (defaults to: [])

    Formatting diffs

  • structure_changes (Array<Hash>) (defaults to: [])

    Structural diffs

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

    Changed metadata fields

  • style_changes (Array<Hash>) (defaults to: [])

    Style definition diffs



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/uniword/diff/diff_result.rb', line 29

def initialize(text_changes: [],
               format_changes: [],
               structure_changes: [],
               metadata_changes: {},
               style_changes: [])
  @text_changes = text_changes
  @format_changes = format_changes
  @structure_changes = structure_changes
  @metadata_changes = 
  @style_changes = style_changes
end

Instance Attribute Details

#format_changesObject (readonly)

Returns the value of attribute format_changes.



18
19
20
# File 'lib/uniword/diff/diff_result.rb', line 18

def format_changes
  @format_changes
end

#metadata_changesObject (readonly)

Returns the value of attribute metadata_changes.



18
19
20
# File 'lib/uniword/diff/diff_result.rb', line 18

def 
  @metadata_changes
end

#structure_changesObject (readonly)

Returns the value of attribute structure_changes.



18
19
20
# File 'lib/uniword/diff/diff_result.rb', line 18

def structure_changes
  @structure_changes
end

#style_changesObject (readonly)

Returns the value of attribute style_changes.



18
19
20
# File 'lib/uniword/diff/diff_result.rb', line 18

def style_changes
  @style_changes
end

#text_changesObject (readonly)

Returns the value of attribute text_changes.



18
19
20
# File 'lib/uniword/diff/diff_result.rb', line 18

def text_changes
  @text_changes
end

Instance Method Details

#empty?Boolean

Whether the documents are identical (no changes of any kind).

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/uniword/diff/diff_result.rb', line 44

def empty?
  text_changes.empty? &&
    format_changes.empty? &&
    structure_changes.empty? &&
    .empty? &&
    style_changes.empty?
end

#summaryString

Human-readable summary of changes.

Returns:

  • (String)


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/uniword/diff/diff_result.rb', line 66

def summary
  return "No differences found." if empty?

  parts = []
  parts << "#{text_changes.size} text change(s)" if text_changes.any?
  parts << "#{format_changes.size} format change(s)" if format_changes.any?
  parts << "#{structure_changes.size} structural change(s)" if structure_changes.any?
  parts << "#{.size} metadata change(s)" if .any?
  parts << "#{style_changes.size} style change(s)" if style_changes.any?
  parts.join(", ")
end

#to_hHash

Convert to a plain Hash suitable for serialization.

Returns:

  • (Hash)


88
89
90
91
92
93
94
95
96
97
# File 'lib/uniword/diff/diff_result.rb', line 88

def to_h
  {
    text_changes: text_changes,
    format_changes: format_changes,
    structure_changes: structure_changes,
    metadata_changes: ,
    style_changes: style_changes,
    summary: summary,
  }
end

#to_json(*_args) ⇒ String

Serialize to JSON string.

Returns:

  • (String)

    Pretty-printed JSON



81
82
83
# File 'lib/uniword/diff/diff_result.rb', line 81

def to_json(*_args)
  JSON.pretty_generate(to_h)
end

#total_changesInteger

Total number of changes across all categories.

Returns:

  • (Integer)


55
56
57
58
59
60
61
# File 'lib/uniword/diff/diff_result.rb', line 55

def total_changes
  text_changes.size +
    format_changes.size +
    structure_changes.size +
    .size +
    style_changes.size
end