Class: Commands::RevisionDiff

Inherits:
GetFile show all
Defined in:
lib/gdsh/revision_diff.rb

Overview

Summarizes changes between revisions

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GetFile

#download, #download_revision_as_txt, #generate_filename_from_revision, #puts_downloading_banner, #write_to_file

Methods inherited from QueryRevision

#docx_link_label, #modified_date_label, #modifying_user_label, #modifying_users, #pdf_link_label, #puts_banner, #puts_download_links, #puts_revision_info, #revision_id_label, #revisions, #txt_link, #txt_link_label

Methods included from Mime

#docx, #html, #jpeg, #ods, #odt, #pdf, #png, #pptx, #rtf, #svg, #txt, #xlsx

Methods included from DriveError

#drive_error_string

Methods included from CommandMixin

included

Constructor Details

#initialize(client, params) ⇒ RevisionDiff

Returns a new instance of RevisionDiff.



24
25
26
27
# File 'lib/gdsh/revision_diff.rb', line 24

def initialize(client, params)
  super(client, params)
  init_revisions
end

Class Method Details

.command_nameObject



11
12
13
# File 'lib/gdsh/revision_diff.rb', line 11

def self.command_name
  'diff'
end

.functionObject



19
20
21
22
# File 'lib/gdsh/revision_diff.rb', line 19

def self.function
  'Compares and summarizes the changes between two revisions. If no ' \
  'revision numbers are provided, a consolidated summary is returned.'
end

.parametersObject



15
16
17
# File 'lib/gdsh/revision_diff.rb', line 15

def self.parameters
  '(<file_id>[, <rev_1>, <rev_2>])'
end

Instance Method Details

#compare_and_print_change_count(low, high) ⇒ Object



60
61
62
63
# File 'lib/gdsh/revision_diff.rb', line 60

def compare_and_print_change_count(low, high)
  changes = compare_two_revs(low, high)
  print_summary_of_changes(changes)
end

#compare_two_revs(low, high) ⇒ Object



48
49
50
51
52
# File 'lib/gdsh/revision_diff.rb', line 48

def compare_two_revs(low, high)
  first = download_revision_as_txt(low)
  second = download_revision_as_txt(high)
  Differ.diff_by_word(first, second)
end

#consecutive_revisionsObject



41
42
43
44
45
46
# File 'lib/gdsh/revision_diff.rb', line 41

def consecutive_revisions
  return unless @all
  keys = modifying_users.keys.sort_by { |x| x.to_i }
  len = keys.length
  keys.first(len - 1).zip(keys.last(len - 1))
end

#diff_author(pair) ⇒ Object



70
71
72
73
# File 'lib/gdsh/revision_diff.rb', line 70

def diff_author(pair)
  "From rev #{pair[0]} to rev #{pair[1]} ".colorize(:green) +
  "modified by #{modifying_users[pair[0]]}".colorize(:green)
end

#executeObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gdsh/revision_diff.rb', line 75

def execute
  puts_diff_note
  if @all
    consecutive_revisions.each do |pair|
      puts diff_author(pair)
      compare_and_print_change_count(pair[0], pair[1])
    end
  else
    compare_and_print_change_count(@low_rev, @high_rev)
  end
end

#init_revisionsObject



29
30
31
32
33
34
35
# File 'lib/gdsh/revision_diff.rb', line 29

def init_revisions
  @low_rev = (@params.length == 4) ? @params[2] : nil
  @high_rev = (@params.length == 4) ? @params[3] : nil
  @all = @low_rev.nil? && @high_rev.nil?
  return if @all || order_is_correct
  @low_rev, @high_rev = @high_rev, @low_rev
end

#order_is_correctObject



37
38
39
# File 'lib/gdsh/revision_diff.rb', line 37

def order_is_correct
  @high_rev.to_i > @low_rev.to_i
end


54
55
56
57
58
# File 'lib/gdsh/revision_diff.rb', line 54

def print_summary_of_changes(changes)
  puts "#{changes.change_count} words changed, ".colorize(:green) +
  "#{changes.insert_count} inserts, ".colorize(:green) +
  "#{changes.delete_count} deletes.".colorize(:green)
end

#puts_diff_noteObject



65
66
67
68
# File 'lib/gdsh/revision_diff.rb', line 65

def puts_diff_note
  puts "Note: 'ab' -> 'ac' counts as both an insert and".colorize(:green) +
  ' a delete but counts as only one change.'.colorize(:green)
end