Class: Uniword::Diff::PackageDiffResult

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

Overview

Value object holding the result of comparing two DOCX packages.

Contains lists of added, removed, and modified ZIP parts, plus detailed XML change descriptions for modified parts, ZIP metadata differences, and OPC validation issues.

Examples:

result = PackageDiffer.new("bad.docx", "repaired.docx").diff
puts result.summary
puts result.to_json

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_path:, new_path:, added_parts:, removed_parts:, modified_parts:, unchanged_parts:, xml_changes:, zip_metadata_changes: [], opc_issues: []) ⇒ PackageDiffResult

Initialize a PackageDiffResult.

Parameters:

  • old_path (String)
  • new_path (String)
  • added_parts (Array<String>)
  • removed_parts (Array<String>)
  • modified_parts (Array<PartChange>)
  • unchanged_parts (Array<String>)
  • xml_changes (Array<XmlChange>)
  • zip_metadata_changes (Array<ZipMetadataChange>) (defaults to: [])
  • opc_issues (Array<OpcIssue>) (defaults to: [])


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/uniword/diff/package_diff_result.rb', line 130

def initialize(old_path:, new_path:,
               added_parts:, removed_parts:,
               modified_parts:, unchanged_parts:,
               xml_changes:, zip_metadata_changes: [],
               opc_issues: [])
  @old_path = old_path
  @new_path = new_path
  @added_parts = added_parts
  @removed_parts = removed_parts
  @modified_parts = modified_parts
  @unchanged_parts = unchanged_parts
  @xml_changes = xml_changes
  @zip_metadata_changes = 
  @opc_issues = opc_issues
end

Instance Attribute Details

#added_partsObject (readonly)

Returns the value of attribute added_parts.



113
114
115
# File 'lib/uniword/diff/package_diff_result.rb', line 113

def added_parts
  @added_parts
end

#modified_partsObject (readonly)

Returns the value of attribute modified_parts.



113
114
115
# File 'lib/uniword/diff/package_diff_result.rb', line 113

def modified_parts
  @modified_parts
end

#new_pathObject (readonly)

Returns the value of attribute new_path.



113
114
115
# File 'lib/uniword/diff/package_diff_result.rb', line 113

def new_path
  @new_path
end

#old_pathObject (readonly)

Returns the value of attribute old_path.



113
114
115
# File 'lib/uniword/diff/package_diff_result.rb', line 113

def old_path
  @old_path
end

#opc_issuesObject (readonly)

Returns the value of attribute opc_issues.



113
114
115
# File 'lib/uniword/diff/package_diff_result.rb', line 113

def opc_issues
  @opc_issues
end

#removed_partsObject (readonly)

Returns the value of attribute removed_parts.



113
114
115
# File 'lib/uniword/diff/package_diff_result.rb', line 113

def removed_parts
  @removed_parts
end

#unchanged_partsObject (readonly)

Returns the value of attribute unchanged_parts.



113
114
115
# File 'lib/uniword/diff/package_diff_result.rb', line 113

def unchanged_parts
  @unchanged_parts
end

#xml_changesObject (readonly)

Returns the value of attribute xml_changes.



113
114
115
# File 'lib/uniword/diff/package_diff_result.rb', line 113

def xml_changes
  @xml_changes
end

#zip_metadata_changesObject (readonly)

Returns the value of attribute zip_metadata_changes.



113
114
115
# File 'lib/uniword/diff/package_diff_result.rb', line 113

def 
  @zip_metadata_changes
end

Instance Method Details

#empty?Boolean

Whether the two packages are identical.

Returns:

  • (Boolean)


149
150
151
152
# File 'lib/uniword/diff/package_diff_result.rb', line 149

def empty?
  added_parts.empty? && removed_parts.empty? &&
    modified_parts.empty?
end

#summaryString

Human-readable summary.

Returns:

  • (String)


164
165
166
167
168
169
170
171
172
# File 'lib/uniword/diff/package_diff_result.rb', line 164

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

  parts = []
  parts << "#{added_parts.size} part(s) added" if added_parts.any?
  parts << "#{removed_parts.size} part(s) removed" if removed_parts.any?
  parts << "#{modified_parts.size} part(s) modified" if modified_parts.any?
  parts.join(", ")
end

#to_hHash

Convert to plain Hash.

Returns:

  • (Hash)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/uniword/diff/package_diff_result.rb', line 184

def to_h
  h = {
    old_path: @old_path,
    new_path: @new_path,
    summary: summary,
    added_parts: @added_parts,
    removed_parts: @removed_parts,
    modified_parts: @modified_parts.map(&:to_h),
    unchanged_count: @unchanged_parts.size,
    xml_changes: @xml_changes.map(&:to_h),
  }
  if @zip_metadata_changes.any?
    h[:zip_metadata_changes] =
      @zip_metadata_changes.map(&:to_h)
  end
  h[:opc_issues] = @opc_issues.map(&:to_h) if @opc_issues.any?
  h
end

#to_json(*_args) ⇒ String

Serialize to JSON string.

Returns:

  • (String)


177
178
179
# File 'lib/uniword/diff/package_diff_result.rb', line 177

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

#total_changesInteger

Total number of changes.

Returns:

  • (Integer)


157
158
159
# File 'lib/uniword/diff/package_diff_result.rb', line 157

def total_changes
  added_parts.size + removed_parts.size + modified_parts.size
end