Class: DocumentGenerator::DiffFile

Inherits:
Object
  • Object
show all
Defined in:
lib/document_generator/diff_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_diff_file) ⇒ DiffFile

Returns a new instance of DiffFile.



11
12
13
# File 'lib/document_generator/diff_file.rb', line 11

def initialize(git_diff_file)
  @git_diff_file = git_diff_file
end

Instance Attribute Details

#git_diff_fileObject

Returns the value of attribute git_diff_file.



5
6
7
# File 'lib/document_generator/diff_file.rb', line 5

def git_diff_file
  @git_diff_file
end

Instance Method Details

#action_typeObject



84
85
86
87
88
# File 'lib/document_generator/diff_file.rb', line 84

def action_type
  { new: 'Create file',
    modified: 'Update file',
    deleted: 'Remove file' }.fetch(type.to_sym, type)
end

#contentObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/document_generator/diff_file.rb', line 29

def content
  if type == 'deleted'
    return "####{patch_heading}\n\n"
  end

  temp = []
  temp << "####{patch_heading}"

  outputs = markdown_outputs
  if outputs.any?
    outputs.each do |output|
      if output.escaped_content.length > 0
        temp << "\n\n"
        temp << "#####{output.description}"
        temp << "\n```\n"
        if output.description == "Becomes"
          temp << output.content.join("\n") + "\n"
        else
          temp << output.escaped_content
        end
        temp << "\n```\n"
      end
    end

  end

  temp << "\n\n"

  temp.join
end

#ending_codeObject

The escaped end result code for the whole diff file returned as a string



60
61
62
63
64
65
66
# File 'lib/document_generator/diff_file.rb', line 60

def ending_code # The escaped end result code for the whole diff file returned as a string
  clean_hunks = []
  git_diff_file_hunks.each do |hunk|
    clean_hunks << ending_code_for(hunk).join("\n")
  end
  clean_hunks.join("\n")
end

#ending_code_for(hunk) ⇒ Object

The unescaped end result code for a particular hunk returned as array



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/document_generator/diff_file.rb', line 68

def ending_code_for(hunk) # The unescaped end result code for a particular hunk returned as array
  clean_lines = []

  git_diff_lines_for(hunk).each_with_index do |line, index|
    if (line[0]) == "-" || ignore_line?(line)
      next
    end

    if (line[0]) == "+"
      line = remove_first_character(line)
    end
    clean_lines << line
  end
  clean_lines
end

#git_diff_file_hunksObject



19
20
21
22
23
# File 'lib/document_generator/diff_file.rb', line 19

def git_diff_file_hunks
  hunks = git_diff_file.patch.split(/@@.*@@.*\n/)
  hunks.shift # Shift to pop first element off array which is just git diff header info
  hunks
end

#git_diff_lines_for(hunk) ⇒ Object



25
26
27
# File 'lib/document_generator/diff_file.rb', line 25

def git_diff_lines_for(hunk)
  hunk.split("\n")
end

#markdown_outputsObject



90
91
92
93
94
95
96
# File 'lib/document_generator/diff_file.rb', line 90

def markdown_outputs
  outputs = []
  git_diff_file_hunks.each do |hunk|
    outputs << markdown_outputs_for(hunk)
  end
  outputs.flatten
end

#markdown_outputs_for(hunk) ⇒ Object

returns an array of outputs for a particular hunk



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/document_generator/diff_file.rb', line 98

def markdown_outputs_for(hunk) # returns an array of outputs for a particular hunk
  outputs = []
  last_line = -1
  git_diff_lines_for(hunk).each_with_index do |line, index|
    next if index <= last_line
    case line.strip[0]

    when "+"
      last_line = last_same_line(index, hunk)
      output = Output.new
      output.description = "Add"
      output.content = line_block(index, last_line, hunk)
      outputs << output
    when "-"
      if line_sign(index + 1, hunk) == "+"
        output = Output.new
        output.description = "Change"
        output.content = line_block(index, last_same_line(index, hunk), hunk)
        outputs << output
        last_line = last_same_line(last_same_line(index, hunk) + 1, hunk)

        output = Output.new
        output.description = "To"
        output.content = line_block(last_same_line(index, hunk) + 1, last_line, hunk)
        outputs << output
        last_line = last_same_line(last_same_line(index, hunk) + 1, hunk)
      else
        output = Output.new
        output.description = "Remove"
        last_line = last_same_line(index, hunk)
        output.content = line_block(index, last_line, hunk)
        outputs << output
      end
    end
  end

  if git_diff_file.type == 'modified'
    output = Output.new
    output.description = "Becomes"
    output.content = ending_code_for(hunk)
    outputs << output
  end

  outputs
end

#patch_headingObject



15
16
17
# File 'lib/document_generator/diff_file.rb', line 15

def patch_heading
  "#{action_type} `#{git_diff_file.path}`"
end

#typeObject



7
8
9
# File 'lib/document_generator/diff_file.rb', line 7

def type
  git_diff_file.type
end