Class: Docsmith::Diff::Result
- Inherits:
-
Object
- Object
- Docsmith::Diff::Result
- 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
-
#changes ⇒ Array<Hash>
readonly
Grouped edits produced by Renderers::Base#compute.
-
#content_type ⇒ String
readonly
Content type of the diffed document ("markdown", "html", "json").
-
#from_version ⇒ Integer
readonly
Version_number of the from (older) version.
-
#to_version ⇒ Integer
readonly
Version_number of the to (newer) version.
Instance Method Summary collapse
-
#as_json(options = nil) ⇒ Hash
The canonical JSON representation, and the single source of truth for it.
-
#deletions ⇒ Integer
Number of pure deletions.
-
#initialize(content_type:, from_version:, to_version:, changes:) ⇒ Result
constructor
A new instance of Result.
-
#insertions ⇒ Integer
Number of pure insertions.
-
#replacements ⇒ Integer
Number of replacements (an old span became a new one).
-
#stats ⇒ Hash
Edit counts by kind.
-
#to_html ⇒ String
HTML diff representation.
-
#to_json(*args) ⇒ String
Args are forwarded so JSON.pretty_generate indents a nested Result instead of splicing it in as one compact line.
Constructor Details
#initialize(content_type:, from_version:, to_version:, changes:) ⇒ Result
Returns a new instance of Result.
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
#changes ⇒ Array<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: }.
22 23 24 |
# File 'lib/docsmith/diff/result.rb', line 22 def changes @changes end |
#content_type ⇒ String (readonly)
Returns 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_version ⇒ Integer (readonly)
Returns 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_version ⇒ Integer (readonly)
Returns 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".
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/docsmith/diff/result.rb', line 79 def as_json( = 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 |
#deletions ⇒ Integer
Returns number of pure deletions.
41 42 43 |
# File 'lib/docsmith/diff/result.rb', line 41 def deletions changes.count { |c| c[:type] == :delete } end |
#insertions ⇒ Integer
Returns number of pure insertions.
36 37 38 |
# File 'lib/docsmith/diff/result.rb', line 36 def insertions changes.count { |c| c[:type] == :insert } end |
#replacements ⇒ Integer
Returns 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 |
#stats ⇒ Hash
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.
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_html ⇒ String
Returns 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.
94 95 96 |
# File 'lib/docsmith/diff/result.rb', line 94 def to_json(*args) as_json.to_json(*args) end |