Class: Uniword::TrackedChanges
- Inherits:
-
Object
- Object
- Uniword::TrackedChanges
- Defined in:
- lib/uniword/tracked_changes.rb
Overview
Manages track changes for a Word document.
This class represents the collection of all revisions (tracked changes) in a document, including insertions, deletions, and formatting changes.
Track changes allow users to see what modifications have been made to a document, who made them, and when.
Instance Attribute Summary collapse
-
#enabled ⇒ Object
Whether track changes is enabled.
-
#revisions ⇒ Object
Collection of all revisions.
Instance Method Summary collapse
-
#accept_all ⇒ Integer
Accept all changes (remove all revisions).
-
#add_deletion(text, author:, date: nil) ⇒ Revision
Add a deletion.
-
#add_format_change(content, author:, date: nil) ⇒ Revision
Add a format change.
-
#add_insertion(text, author:, date: nil) ⇒ Revision
Add an insertion.
-
#add_revision(revision) ⇒ Revision
Add a revision to the collection.
-
#authors ⇒ Array<String>
Get all unique authors.
-
#clear ⇒ void
Clear all revisions.
-
#count ⇒ Integer
Get the number of revisions.
-
#deletions ⇒ Array<Revision>
Get all deletions.
-
#empty? ⇒ Boolean
Check if there are any revisions.
-
#find_revision(revision_id) ⇒ Revision?
Find a revision by ID.
-
#format_changes ⇒ Array<Revision>
Get all format changes.
-
#initialize(attributes = {}) ⇒ TrackedChanges
constructor
Initialize tracked changes.
-
#insertions ⇒ Array<Revision>
Get all insertions.
-
#inspect ⇒ String
Provide detailed inspection for debugging.
-
#reject_all ⇒ Integer
Reject all changes (remove all revisions).
-
#remove_revision(revision_id) ⇒ Revision?
Remove a revision by ID.
-
#revisions_by_author(author) ⇒ Array<Revision>
Get all revisions by a specific author.
-
#revisions_by_type(type) ⇒ Array<Revision>
Get all revisions of a specific type.
Constructor Details
#initialize(attributes = {}) ⇒ TrackedChanges
Initialize tracked changes
39 40 41 42 43 |
# File 'lib/uniword/tracked_changes.rb', line 39 def initialize(attributes = {}) @enabled = attributes[:enabled] || false @revisions = [] @revision_counter = 0 end |
Instance Attribute Details
#enabled ⇒ Object
Whether track changes is enabled
28 29 30 |
# File 'lib/uniword/tracked_changes.rb', line 28 def enabled @enabled end |
#revisions ⇒ Object
Collection of all revisions
28 29 30 |
# File 'lib/uniword/tracked_changes.rb', line 28 def revisions @revisions end |
Instance Method Details
#accept_all ⇒ Integer
Accept all changes (remove all revisions)
197 198 199 200 201 |
# File 'lib/uniword/tracked_changes.rb', line 197 def accept_all count_before = count clear count_before end |
#add_deletion(text, author:, date: nil) ⇒ Revision
Add a deletion
84 85 86 87 88 89 90 91 92 |
# File 'lib/uniword/tracked_changes.rb', line 84 def add_deletion(text, author:, date: nil) revision = Revision.new( type: :delete, text: text, author: , date: date, ) add_revision(revision) end |
#add_format_change(content, author:, date: nil) ⇒ Revision
Add a format change
100 101 102 103 104 105 106 107 108 |
# File 'lib/uniword/tracked_changes.rb', line 100 def add_format_change(content, author:, date: nil) revision = Revision.new( type: :format_change, content: content, author: , date: date, ) add_revision(revision) end |
#add_insertion(text, author:, date: nil) ⇒ Revision
Add an insertion
68 69 70 71 72 73 74 75 76 |
# File 'lib/uniword/tracked_changes.rb', line 68 def add_insertion(text, author:, date: nil) revision = Revision.new( type: :insert, text: text, author: , date: date, ) add_revision(revision) end |
#add_revision(revision) ⇒ Revision
Add a revision to the collection
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/uniword/tracked_changes.rb', line 49 def add_revision(revision) unless revision.is_a?(Revision) raise ArgumentError, "revision must be a Revision instance" end # Assign sequential ID if not already set revision.revision_id = next_revision_id unless revision.revision_id && !revision.revision_id.empty? revisions << revision revision end |
#authors ⇒ Array<String>
Get all unique authors
182 183 184 |
# File 'lib/uniword/tracked_changes.rb', line 182 def revisions.map(&:author).uniq.compact end |
#clear ⇒ void
This method returns an undefined value.
Clear all revisions
189 190 191 192 |
# File 'lib/uniword/tracked_changes.rb', line 189 def clear revisions.clear @revision_counter = 0 end |
#count ⇒ Integer
Get the number of revisions
168 169 170 |
# File 'lib/uniword/tracked_changes.rb', line 168 def count revisions.size end |
#deletions ⇒ Array<Revision>
Get all deletions
154 155 156 |
# File 'lib/uniword/tracked_changes.rb', line 154 def deletions revisions_by_type(:delete) end |
#empty? ⇒ Boolean
Check if there are any revisions
175 176 177 |
# File 'lib/uniword/tracked_changes.rb', line 175 def empty? revisions.empty? end |
#find_revision(revision_id) ⇒ Revision?
Find a revision by ID
114 115 116 |
# File 'lib/uniword/tracked_changes.rb', line 114 def find_revision(revision_id) revisions.find { |r| r.revision_id == revision_id.to_s } end |
#format_changes ⇒ Array<Revision>
Get all format changes
161 162 163 |
# File 'lib/uniword/tracked_changes.rb', line 161 def format_changes revisions_by_type(:format_change) end |
#insertions ⇒ Array<Revision>
Get all insertions
147 148 149 |
# File 'lib/uniword/tracked_changes.rb', line 147 def insertions revisions_by_type(:insert) end |
#inspect ⇒ String
Provide detailed inspection for debugging
215 216 217 218 219 220 |
# File 'lib/uniword/tracked_changes.rb', line 215 def inspect "#<Uniword::TrackedChanges enabled=#{enabled} " \ "revisions=#{count} " \ "insertions=#{insertions.count} " \ "deletions=#{deletions.count}>" end |
#reject_all ⇒ Integer
Reject all changes (remove all revisions)
206 207 208 209 210 |
# File 'lib/uniword/tracked_changes.rb', line 206 def reject_all count_before = count clear count_before end |
#remove_revision(revision_id) ⇒ Revision?
Remove a revision by ID
122 123 124 125 126 |
# File 'lib/uniword/tracked_changes.rb', line 122 def remove_revision(revision_id) revision = find_revision(revision_id) revisions.delete(revision) if revision revision end |
#revisions_by_author(author) ⇒ Array<Revision>
Get all revisions by a specific author
132 133 134 |
# File 'lib/uniword/tracked_changes.rb', line 132 def () revisions.select { |r| r. == } end |
#revisions_by_type(type) ⇒ Array<Revision>
Get all revisions of a specific type
140 141 142 |
# File 'lib/uniword/tracked_changes.rb', line 140 def revisions_by_type(type) revisions.select { |r| r.type == type } end |