Class: Uniword::TrackedChanges

Inherits:
Object
  • Object
show all
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.

Examples:

Enable track changes

tracked_changes = Uniword::TrackedChanges.new
tracked_changes.enabled = true

Add an insertion

revision = Uniword::Revision.new(
  type: :insert,
  author: "John Doe",
  text: "New content"
)
tracked_changes.add_revision(revision)

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ TrackedChanges

Initialize tracked changes

Parameters:

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

    Tracked changes attributes

Options Hash (attributes):

  • :enabled (Boolean)

    Whether track changes is enabled



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

#enabledObject

Whether track changes is enabled



28
29
30
# File 'lib/uniword/tracked_changes.rb', line 28

def enabled
  @enabled
end

#revisionsObject

Collection of all revisions



28
29
30
# File 'lib/uniword/tracked_changes.rb', line 28

def revisions
  @revisions
end

Instance Method Details

#accept_allInteger

Accept all changes (remove all revisions)

Returns:

  • (Integer)

    The number of changes accepted



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

Parameters:

  • text (String)

    The deleted text

  • author (String)

    The author name

  • date (String, Time, nil) (defaults to: nil)

    Optional date

Returns:

  • (Revision)

    The created deletion revision



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: author,
    date: date,
  )
  add_revision(revision)
end

#add_format_change(content, author:, date: nil) ⇒ Revision

Add a format change

Parameters:

  • content (String)

    Description of format change

  • author (String)

    The author name

  • date (String, Time, nil) (defaults to: nil)

    Optional date

Returns:

  • (Revision)

    The created format change revision



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: author,
    date: date,
  )
  add_revision(revision)
end

#add_insertion(text, author:, date: nil) ⇒ Revision

Add an insertion

Parameters:

  • text (String)

    The inserted text

  • author (String)

    The author name

  • date (String, Time, nil) (defaults to: nil)

    Optional date

Returns:

  • (Revision)

    The created insertion revision



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: author,
    date: date,
  )
  add_revision(revision)
end

#add_revision(revision) ⇒ Revision

Add a revision to the collection

Parameters:

  • revision (Revision)

    The revision to add

Returns:

  • (Revision)

    The added revision with assigned ID



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

#authorsArray<String>

Get all unique authors

Returns:

  • (Array<String>)

    List of unique author names



182
183
184
# File 'lib/uniword/tracked_changes.rb', line 182

def authors
  revisions.map(&:author).uniq.compact
end

#clearvoid

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

#countInteger

Get the number of revisions

Returns:

  • (Integer)

    The count of revisions



168
169
170
# File 'lib/uniword/tracked_changes.rb', line 168

def count
  revisions.size
end

#deletionsArray<Revision>

Get all deletions

Returns:

  • (Array<Revision>)

    All deletion revisions



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

Returns:

  • (Boolean)

    true if empty



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

Parameters:

  • revision_id (String)

    The revision ID to find

Returns:

  • (Revision, nil)

    The revision if found, nil otherwise



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_changesArray<Revision>

Get all format changes

Returns:

  • (Array<Revision>)

    All format change revisions



161
162
163
# File 'lib/uniword/tracked_changes.rb', line 161

def format_changes
  revisions_by_type(:format_change)
end

#insertionsArray<Revision>

Get all insertions

Returns:

  • (Array<Revision>)

    All insertion revisions



147
148
149
# File 'lib/uniword/tracked_changes.rb', line 147

def insertions
  revisions_by_type(:insert)
end

#inspectString

Provide detailed inspection for debugging

Returns:

  • (String)

    A readable representation of tracked changes



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_allInteger

Reject all changes (remove all revisions)

Returns:

  • (Integer)

    The number of changes rejected



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

Parameters:

  • revision_id (String)

    The revision ID to remove

Returns:

  • (Revision, nil)

    The removed revision if found, nil otherwise



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

Parameters:

  • author (String)

    The author name

Returns:

  • (Array<Revision>)

    Revisions by the author



132
133
134
# File 'lib/uniword/tracked_changes.rb', line 132

def revisions_by_author(author)
  revisions.select { |r| r.author == author }
end

#revisions_by_type(type) ⇒ Array<Revision>

Get all revisions of a specific type

Parameters:

  • type (Symbol)

    The revision type (:insert, :delete, :format_change)

Returns:

  • (Array<Revision>)

    Revisions of the specified type



140
141
142
# File 'lib/uniword/tracked_changes.rb', line 140

def revisions_by_type(type)
  revisions.select { |r| r.type == type }
end