Module: TextFileMutator

Defined in:
lib/txt_file_mutator.rb

Class Method Summary collapse

Class Method Details

.append_line(path, content) ⇒ Object



103
104
105
106
107
# File 'lib/txt_file_mutator.rb', line 103

def self.append_line(path, content)
  File.open(path, 'a') do |f|
    f.puts(content)
  end
end

.comment_gem_config(file, line) ⇒ Object



56
57
58
# File 'lib/txt_file_mutator.rb', line 56

def self.comment_gem_config(file, line)  
  comment_line_w_contents(file, 'config.gem', line)
end

.comment_line(file, line) ⇒ Object



75
76
77
78
79
# File 'lib/txt_file_mutator.rb', line 75

def self.comment_line(file, line)  
  gsub_file file, /^\s*[^#]\s*(#{Regexp.escape(line)}$)/i do |match|
    "# " + "#{match}"
  end
end

.comment_line_w_contents(file, part1, part2) ⇒ Object



81
82
83
84
85
# File 'lib/txt_file_mutator.rb', line 81

def self.comment_line_w_contents(file, part1, part2)  
  gsub_file file, /^\s*[^#]\s*#{Regexp.escape(part1)}.+(#{Regexp.escape(part2)}$)/i do |match|
    "# " + "#{match}"
  end
end

.comment_rails3_gem_config(file, line) ⇒ Object



60
61
62
# File 'lib/txt_file_mutator.rb', line 60

def self.comment_rails3_gem_config(file, line)  
  comment_line_w_contents(file, 'gem', line)
end

.has_content?(file_content, content) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/txt_file_mutator.rb', line 15

def self.has_content?(file_content, content)
  res = file_content =~ /#{content}/
  !res.nil?
end

.has_content_after?(file_content, content, after) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/txt_file_mutator.rb', line 25

def self.has_content_after?(file_content, content, after)
  res = file_content =~ /#{after}\s*(#{content})/
  !res.nil?
end

.has_content_before?(file_content, content, before) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/txt_file_mutator.rb', line 10

def self.has_content_before?(file_content, content, before) 
  res = file_content =~ /(#{content})\s*#{before}/
  !res.nil?
end

.has_multiple_content_after?(file_content, content, after) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/txt_file_mutator.rb', line 20

def self.has_multiple_content_after?(file_content, content, after)
  res = file_content =~ /#{after}\s*(#{content}\s*#{content})/
  !res.nil?
end

.has_multiple_content_before?(file_content, content, before) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
# File 'lib/txt_file_mutator.rb', line 5

def self.has_multiple_content_before?(file_content, content, before) 
  res = file_content =~ /(#{content}\s*#{content})\s*#{before}/
  !res.nil?
end

.insert_after(file, line, txt_after, exist_check = true) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/txt_file_mutator.rb', line 40

def self.insert_after(file, line, txt_after, exist_check=true) 
  content = File.read(file)
  if exist_check    
    return if has_content_after?(content, txt_after, line)
  end
  gsub_content_file file, /(#{Regexp.escape(line)})/ do |match|
    "#{match}\n#{txt_after}"
  end
end

.insert_before(file, line, txt_before, exist_check = true) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/txt_file_mutator.rb', line 30

def self.insert_before(file, line, txt_before, exist_check=true)
  content = File.read(file)
  if exist_check    
    return if has_content_before?(content, txt_before, line)
  end
  gsub_content_file content, file, /(#{Regexp.escape(line)})/ do |match|
    "#{txt_before}\n#{match}"
  end
end

.remove_content(file, line) ⇒ Object



87
88
89
90
91
# File 'lib/txt_file_mutator.rb', line 87

def self.remove_content(file, line)  
  gsub_file file, /(#{Regexp.escape(line)})/i do |match|
    ""
  end
end

.remove_duplicate_lines(file) ⇒ Object



50
51
52
53
54
# File 'lib/txt_file_mutator.rb', line 50

def self.remove_duplicate_lines(file)
  content = File.read(file)
  content.gsub!(/^(.*)(\r?\n\1)+$/, '\1')
  File.open(path, 'wb') { |file| file.write(content) }
end

.remove_line(file, line) ⇒ Object



93
94
95
# File 'lib/txt_file_mutator.rb', line 93

def self.remove_line(file, line)  
  replace_line file, line, ""
end

.remove_line_w_contents(file, part1, part2) ⇒ Object

TODO: support both ” and “”



69
70
71
72
73
# File 'lib/txt_file_mutator.rb', line 69

def self.remove_line_w_contents(file, part1, part2)
  gsub_file file, /^\s*#{Regexp.escape(part1)}\s+'#{Regexp.escape(part2)}'$/ do |match|
    ""
  end
end

.remove_require(file, line) ⇒ Object



64
65
66
# File 'lib/txt_file_mutator.rb', line 64

def self.remove_require(file, line)
  remove_line_w_contents file, 'require', line
end

.replace_line(file, line, replacement) ⇒ Object



97
98
99
100
101
# File 'lib/txt_file_mutator.rb', line 97

def self.replace_line(file, line, replacement)  
  gsub_file file, /^.*#{Regexp.escape(line)}.*$/i do |match|
    "#{replacement}"
  end
end