txt_file_mutator 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/txt_file_mutator.rb +13 -6
- data/test/test_remove_line_w_content.rb +20 -0
- data/test/test_txt_file_mutator.rb +11 -1
- metadata +2 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/lib/txt_file_mutator.rb
CHANGED
@@ -1,40 +1,47 @@
|
|
1
1
|
module TextFileMutator
|
2
2
|
def self.insert_before(file, line, txt_before)
|
3
|
-
gsub_file file, /(#{Regexp.escape(line)})/
|
3
|
+
gsub_file file, /(#{Regexp.escape(line)})/ do |match|
|
4
4
|
"#{txt_before}\n#{match}"
|
5
5
|
end
|
6
6
|
end
|
7
7
|
|
8
8
|
def self.insert_after(file, line, txt_after)
|
9
|
-
gsub_file file, /(#{Regexp.escape(line)})/
|
9
|
+
gsub_file file, /(#{Regexp.escape(line)})/ do |match|
|
10
10
|
"#{match}\n#{txt_after}"
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.comment_gem_config(file, line)
|
15
|
-
gsub_file file,
|
15
|
+
gsub_file file, /^\s*[^#]\s*config.gem.+'#{Regexp.escape(line)}'$/ do |match|
|
16
16
|
"# " + "#{match}"
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
def self.remove_require(file, line)
|
21
|
-
gsub_file file,
|
21
|
+
gsub_file file, /^\s*require\s+'#{Regexp.escape(line)}'$/ do |match|
|
22
22
|
""
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
def self.comment_line(file, line)
|
27
|
-
gsub_file file,
|
27
|
+
gsub_file file, /^\s*[^#]\s*(#{Regexp.escape(line)}$)/i do |match|
|
28
28
|
"# " + "#{match}"
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
+
def self.remove_content(file, line)
|
33
|
+
gsub_file file, /(#{Regexp.escape(line)})/i do |match|
|
34
|
+
""
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
32
38
|
def self.remove_line(file, line)
|
33
|
-
gsub_file file,
|
39
|
+
gsub_file file, /^.*#{Regexp.escape(line)}.*$)/i do |match|
|
34
40
|
""
|
35
41
|
end
|
36
42
|
end
|
37
43
|
|
44
|
+
|
38
45
|
protected
|
39
46
|
def self.gsub_file(path, regexp, *args, &block)
|
40
47
|
content = File.read(path).gsub(regexp, *args, &block)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'txt_file_mutator'
|
2
|
+
require "test/unit"
|
3
|
+
|
4
|
+
class TestTxtFileMutator < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@tfm = TextFileMutator
|
8
|
+
@file = 'DATA_REMOVE.txt'
|
9
|
+
puts @tfm.instance_methods
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
## Nothing really
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_remove_line
|
17
|
+
@tfm.remove_line @file, 'delete me'
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -22,10 +22,20 @@ class TestTxtFileMutator < Test::Unit::TestCase
|
|
22
22
|
@tfm.comment_gem_config @file, 'hobo'
|
23
23
|
end
|
24
24
|
|
25
|
+
def test_remove_content
|
26
|
+
@tfm.remove_content @file, 'remove me'
|
27
|
+
end
|
28
|
+
|
25
29
|
def test_remove_line
|
26
|
-
@tfm.remove_line @file, '
|
30
|
+
@tfm.remove_line @file, 'delete me'
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
def test_remove_require
|
35
|
+
@tfm.remove_require @file, 'hobo/tasks/rails'
|
27
36
|
end
|
28
37
|
|
38
|
+
|
29
39
|
def test_insert_before
|
30
40
|
@tfm.insert_before @file, 'before', "BEFORE"
|
31
41
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: txt_file_mutator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kristian Mandrup
|
@@ -76,4 +76,5 @@ summary: mutate a text file in various ways
|
|
76
76
|
test_files:
|
77
77
|
- spec/spec_helper.rb
|
78
78
|
- spec/txt_file_mutator_spec.rb
|
79
|
+
- test/test_remove_line_w_content.rb
|
79
80
|
- test/test_txt_file_mutator.rb
|