Class: Docsmith::Diff::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/docsmith/diff/result.rb

Overview

Holds the computed diff between two DocumentVersion records. Produced by Diff::Engine.between; consumed by callers for stats and rendering.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type:, from_version:, to_version:, changes:) ⇒ Result

Returns a new instance of Result.

Parameters:

  • content_type (String)
  • from_version (Integer)
  • to_version (Integer)
  • changes (Array<Hash>)


28
29
30
31
32
33
# File 'lib/docsmith/diff/result.rb', line 28

def initialize(content_type:, from_version:, to_version:, changes:)
  @content_type = content_type
  @from_version = from_version
  @to_version   = to_version
  @changes      = changes
end

Instance Attribute Details

#changesArray<Hash> (readonly)

Grouped edits produced by Renderers::Base#compute. Symbol-keyed mirror of the serialized form: each entry is { type: :insert|:delete|:replace, old: Docsmith::Diff::Result......span, new: Docsmith::Diff::Result......span } where a span is { start:, end:, line:, column:, text: }.

Returns:

  • (Array<Hash>)


22
23
24
# File 'lib/docsmith/diff/result.rb', line 22

def changes
  @changes
end

#content_typeString (readonly)

Returns content type of the diffed document ("markdown", "html", "json").

Returns:

  • (String)

    content type of the diffed document ("markdown", "html", "json")



11
12
13
# File 'lib/docsmith/diff/result.rb', line 11

def content_type
  @content_type
end

#from_versionInteger (readonly)

Returns version_number of the from (older) version.

Returns:

  • (Integer)

    version_number of the from (older) version



13
14
15
# File 'lib/docsmith/diff/result.rb', line 13

def from_version
  @from_version
end

#to_versionInteger (readonly)

Returns version_number of the to (newer) version.

Returns:

  • (Integer)

    version_number of the to (newer) version



15
16
17
# File 'lib/docsmith/diff/result.rb', line 15

def to_version
  @to_version
end

Instance Method Details

#as_json(options = nil) ⇒ Hash

The canonical JSON representation, and the single source of truth for it.

Defining as_json is what makes nesting work: ActiveSupport calls it for render json: { diff: result }, [result].to_json, and JSON.generate. Without it those fell through to Object#as_json (instance_values), which silently dropped "stats" and emitted "line" instead of "position".

Parameters:

  • options (Hash, nil) (defaults to: nil)

    accepted for API compatibility; unused

Returns:

  • (Hash)

    string-keyed, JSON-ready



79
80
81
82
83
84
85
86
87
88
# File 'lib/docsmith/diff/result.rb', line 79

def as_json(options = nil) # rubocop:disable Lint/UnusedMethodArgument
  {
    "schema_version" => Docsmith::JSON_SCHEMA_VERSION,
    "content_type"   => content_type,
    "from_version"   => from_version,
    "to_version"     => to_version,
    "stats"          => stats,
    "changes"        => changes.map { |c| serialize_change(c) }
  }
end

#deletionsInteger

Returns number of pure deletions.

Returns:

  • (Integer)

    number of pure deletions



41
42
43
# File 'lib/docsmith/diff/result.rb', line 41

def deletions
  changes.count { |c| c[:type] == :delete }
end

#insertionsInteger

Returns number of pure insertions.

Returns:

  • (Integer)

    number of pure insertions



36
37
38
# File 'lib/docsmith/diff/result.rb', line 36

def insertions
  changes.count { |c| c[:type] == :insert }
end

#replacementsInteger

Returns number of replacements (an old span became a new one).

Returns:

  • (Integer)

    number of replacements (an old span became a new one)



46
47
48
# File 'lib/docsmith/diff/result.rb', line 46

def replacements
  changes.count { |c| c[:type] == :replace }
end

#statsHash

Edit counts by kind. insertions and deletions are pure; a replacement is counted once rather than as one of each, so a rewritten document never reports 0/0 alongside a non-empty changes array. For git-style totals, add replacements to either side.

Returns:

  • (Hash)

    string-keyed counts



56
57
58
59
60
61
62
63
# File 'lib/docsmith/diff/result.rb', line 56

def stats
  {
    "insertions"   => insertions,
    "deletions"    => deletions,
    "replacements" => replacements,
    "total"        => changes.length
  }
end

#to_htmlString

Returns HTML diff representation.

Returns:

  • (String)

    HTML diff representation



66
67
68
# File 'lib/docsmith/diff/result.rb', line 66

def to_html
  Renderers::Registry.for(content_type).new.render_html(changes)
end

#to_json(*args) ⇒ String

Args are forwarded so JSON.pretty_generate indents a nested Result instead of splicing it in as one compact line.

Returns:

  • (String)

    JSON diff representation matching the documented schema



94
95
96
# File 'lib/docsmith/diff/result.rb', line 94

def to_json(*args)
  as_json.to_json(*args)
end