Method: Rundoc::CodeCommand::FileCommand::Append#insert_contents_into_at_line

Defined in:
lib/rundoc/code_command/file_command/append.rb

#insert_contents_into_at_line(doc) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rundoc/code_command/file_command/append.rb', line 45

def insert_contents_into_at_line(doc)
  lines = doc.lines
  raise "Expected #{filename} to have at least #{@line_number} but only has #{lines.count}" if lines.count < @line_number
  result = []
  lines.each_with_index do |line, index|
    line_number = index.next
    if line_number == @line_number
      result << contents
      result << "\n" unless ends_in_newline?(contents)
    end
    result << line
  end
  result.flatten.join("")
end