insert_after 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +10 -4
- data/lib/insert_after/version.rb +1 -1
- data/lib/insert_after.rb +26 -4
- 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: cc5fc1c4ee18a625fc45c1a704f7a33d5d0180e809f630efa0b329b2145f57cd
|
4
|
+
data.tar.gz: 6b02196f89f4f81054cf0222f262027a50aafabbda7f0a29f7ada19b499675ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb5ecb5fa902acb4ad62ac326258ac7e5aa3604cc8ce82969507bdf2cb161b1f7136fa09af754856eba09ae8984b3c63693a269f02560cb79bfb23c49a7831a3
|
7
|
+
data.tar.gz: 3b3010274b680e0fd468a74ac142f5a07f42733154f597638924287801aa06ff95999bb4f3054157bcbe9cb686cb26b237a3e89e0a8c55904c542465db787c93
|
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
83
|
new_line = argv[1]
|
62
84
|
filename = argv[2]
|
63
|
-
InsertAfter.insert_after(regex, new_line, filename)
|
85
|
+
InsertAfter.insert_after(regex, new_line, filename, all)
|
64
86
|
end
|
65
87
|
end
|
66
88
|
|