insert_after 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +10 -4
- data/lib/insert_after/version.rb +1 -1
- data/lib/insert_after.rb +27 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3134f9519e4d025e12992e3d45aa3ecaf93d01e5e61043b39e3e8dd398a4e733
|
4
|
+
data.tar.gz: 91eebce8f84cd3aec1135d5b91a44d3e83855517516a1ffe1729705a7f9e6eb6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbf47ab40beee36593a5aaa949a2472f3496e2ad17cb121e30aa7d7e51e9095120afa9c8c31be6c6640ed0ef3f6cea2fb64c1008f789f93edc3d55d0d1a0b269
|
7
|
+
data.tar.gz: b023d3e0a6b06ca66d20f849ba344700a823fe06ca036f5a2fc188d57114f00f66c165a00824c8078733553c3cf54121ddbcc4880c7fdfd66ce6236c6ebe686c
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -9,15 +9,20 @@ Can be used on the command line or in a Ruby program.
|
|
9
9
|
|
10
10
|
## Command-line Installation
|
11
11
|
|
12
|
+
```ruby
|
13
|
+
$ gem install insert_after
|
14
|
+
```
|
15
|
+
|
16
|
+
|
17
|
+
## Installation For Use In a Ruby Program
|
18
|
+
|
12
19
|
Either add this line to your application’s `Gemfile`:
|
13
20
|
|
14
21
|
```ruby
|
15
22
|
gem 'insert_after'
|
16
23
|
```
|
17
24
|
|
18
|
-
|
19
|
-
|
20
|
-
Add the following to your application’s `.gemspec`:
|
25
|
+
... or add the following to your application’s `.gemspec`:
|
21
26
|
|
22
27
|
```ruby
|
23
28
|
spec.add_dependency 'insert_after'
|
@@ -54,7 +59,8 @@ The `demo/demo.rb` program is an example of how to use `insert_after` in a Ruby
|
|
54
59
|
```ruby
|
55
60
|
require 'insert_after'
|
56
61
|
|
57
|
-
insert_after
|
62
|
+
InsertAfer.insert_after 'line 2', 'New line', 'my_file.txt'
|
63
|
+
InsertAfter.insert_after 'line', 'Another line from Ruby', 'my_file.txt', all: true
|
58
64
|
```
|
59
65
|
|
60
66
|
|
data/lib/insert_after/version.rb
CHANGED
data/lib/insert_after.rb
CHANGED
@@ -16,31 +16,49 @@ module InsertAfter
|
|
16
16
|
# Inserts 'Inserted 1' after the first line containing 'line' into demo/my_file.txt:
|
17
17
|
$ insert_after line 'Inserted 1' demo/my_file.txt
|
18
18
|
|
19
|
+
# Inserts 'Inserted 1' after every line containing 'line' into demo/my_file.txt:
|
20
|
+
$ insert_after -a line 'Inserted 1' demo/my_file.txt
|
21
|
+
|
19
22
|
# Inserts an empty line after the first line containing 'line 1' into demo/my_file.txt:
|
20
23
|
$ insert_after 'line 1' '' demo/my_file.txt
|
21
24
|
|
25
|
+
# Inserts an empty line after every line containing 'line 1' into demo/my_file.txt:
|
26
|
+
$ insert_after -a 'line 1' '' demo/my_file.txt
|
27
|
+
|
22
28
|
# Inserts 'Inserted 2' after the first line starting with 'line 2' into demo/my_file.txt:
|
23
29
|
$ insert_after '^line 2' 'Inserted 2' demo/my_file.txt
|
24
30
|
|
31
|
+
# Inserts 'Inserted 2' after every line starting with 'line 2' into demo/my_file.txt:
|
32
|
+
$ insert_after -a '^line 2' 'Inserted 2' demo/my_file.txt
|
33
|
+
|
25
34
|
# Inserts 'Inserted 3' after the first line containing an 'e' followed by a '2' into demo/my_file.txt:
|
26
35
|
$ insert_after 'e.*2' 'Inserted 3' demo/my_file.txt
|
27
36
|
|
37
|
+
# Inserts 'Inserted 3' after every line containing an 'e' followed by a '2' into demo/my_file.txt:
|
38
|
+
$ insert_after -a 'e.*2' 'Inserted 3' demo/my_file.txt
|
39
|
+
|
28
40
|
Ruby usage:
|
29
41
|
require 'insert_after'
|
30
|
-
InsertAfter.insert_after 'line 2' 'New line' 'demo/my_file.txt'
|
42
|
+
InsertAfter.insert_after 'line 2', 'New line', 'demo/my_file.txt'
|
43
|
+
|
44
|
+
InsertAfter.insert_after 'line 2', 'New line', 'demo/my_file.txt', all: true
|
31
45
|
END_MSG
|
32
46
|
exit 1
|
33
47
|
end
|
34
48
|
|
35
|
-
def self.insert_after(regex, new_line, filename)
|
49
|
+
def self.insert_after(regex, new_line, filename, all: false)
|
36
50
|
InsertAfter.help "#{filename} does not exist" unless File.exist? filename
|
37
51
|
|
38
52
|
original_file = File.new(filename)
|
39
53
|
temporary_file = Tempfile.new(filename)
|
54
|
+
inserted = false
|
40
55
|
begin
|
41
56
|
original_file.each do |line|
|
42
57
|
temporary_file << line
|
43
|
-
|
58
|
+
if line.downcase.match?(/#{regex}/)
|
59
|
+
temporary_file << "#{new_line}\n" unless inserted
|
60
|
+
inserted = true unless all
|
61
|
+
end
|
44
62
|
end
|
45
63
|
original_file.close
|
46
64
|
temporary_file.close
|
@@ -54,13 +72,17 @@ module InsertAfter
|
|
54
72
|
|
55
73
|
def self.cmd(argv = ARGV)
|
56
74
|
InsertAfter.help if argv.empty?
|
75
|
+
if argv.include? '-a'
|
76
|
+
all = true
|
77
|
+
argv.delete '-a'
|
78
|
+
end
|
57
79
|
InsertAfter.help 'The new line to insert was not provided' if argv.length == 1
|
58
80
|
InsertAfter.help 'No file name was provided' if argv.length == 2
|
59
81
|
InsertAfter.help 'Too many arguments' if argv.length > 3
|
60
82
|
regex = argv[0].downcase
|
61
|
-
new_line = argv[1]
|
83
|
+
new_line = argv[1].gsub('\n', "\n").gsub('\r', "\r").gsub('\t', "\t")
|
62
84
|
filename = argv[2]
|
63
|
-
InsertAfter.insert_after(regex, new_line, filename)
|
85
|
+
InsertAfter.insert_after(regex, new_line, filename, all: all)
|
64
86
|
end
|
65
87
|
end
|
66
88
|
|