Module: RSCM::LineEditor

Defined in:
lib/rscm/line_editor.rb

Class Method Summary collapse

Class Method Details

.comment_out(original, line_regex, comment_template, output) ⇒ Object

Comments out line by line if they match the line_regex. Does not comment out already commented out lines. If comment_template is nil, the matching lines will be deleted Returns true if at least one line is commented out or changed



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rscm/line_editor.rb', line 10

def comment_out(original, line_regex, comment_template, output)
  did_comment_out = false
  already_commented_exp = /^[#{comment_template}]/ unless comment_template.nil?
  original.each_line do |line|
    out_line = nil
    if(line_regex =~ line)
      if(already_commented_exp && already_commented_exp =~ line)
        out_line = line
      else
        did_comment_out = true
        out_line = "#{comment_template}#{line}" unless comment_template.nil?
      end
    else
      out_line = line
    end
    output << out_line unless out_line.nil?
  end
  did_comment_out
end