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



70
71
72
73
74
# File 'lib/document_generator/diff_file.rb', line 70

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

#contentObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/document_generator/diff_file.rb', line 23

def content
  if type == 'deleted'
    return patch_heading + "\n\n"
  end

  temp = []
  temp << patch_heading

  if markdown_outputs.any?
    markdown_outputs.each do |output|
      temp << "\n\n"
      temp << output.description
      temp << "\n<pre><code>"
      temp << output.escaped_content
      temp << "</code></pre>\n"
    end
  end

  if git_diff_file.type == "modified"
    temp << "\n\n"
    temp << "Becomes"
    temp << "\n<pre><code>"
    temp << ending_code
    temp << "\n</code></pre>\n"
  end

  temp << "\n\n"

  temp.join
end

#ending_codeObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/document_generator/diff_file.rb', line 54

def ending_code
  clean_lines = []
  git_diff_file_lines[code_line_start..-1].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
  Output.no_really_escape(CGI.escapeHTML(clean_lines.join("\n")))
end

#git_diff_file_linesObject



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

def git_diff_file_lines
  git_diff_file.patch.split("\n")
end

#markdown_outputsObject

returns an array of outputs



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/document_generator/diff_file.rb', line 76

def markdown_outputs # returns an array of outputs
  outputs = []
  last_line = 0
  git_diff_file_lines.each_with_index do |line, index|
    next if index < code_line_start
    next if index <= last_line
    case line.strip[0]

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

  end
  outputs
end

#patch_headingObject



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

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