insert_after 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7b32c8d75b05bb77620742256241e80ffa08ae9dd2aab62c77ff89162958aa3
4
- data.tar.gz: a005eeca4d06274644788acc9ef772ab17fcde223ab177a4c73f4f8c2885a0fc
3
+ metadata.gz: 3134f9519e4d025e12992e3d45aa3ecaf93d01e5e61043b39e3e8dd398a4e733
4
+ data.tar.gz: 91eebce8f84cd3aec1135d5b91a44d3e83855517516a1ffe1729705a7f9e6eb6
5
5
  SHA512:
6
- metadata.gz: 22c3845706c9672bc8e1786610de92337c20a03ec9ae9597c10b10f0abf12ce1ff087f45bfc176136453c2c1c8c9b3799d893879ee48900c7bd8b332aa83c0c1
7
- data.tar.gz: 0bb1f80568534d1bb4715a5838e87f0abc8caaaaadd31fb2a174d881ea7c400386f96000151ee85202e040e08cac3f79322e3a118bf3d9863e4f1dff0b7db66f
6
+ metadata.gz: cbf47ab40beee36593a5aaa949a2472f3496e2ad17cb121e30aa7d7e51e9095120afa9c8c31be6c6640ed0ef3f6cea2fb64c1008f789f93edc3d55d0d1a0b269
7
+ data.tar.gz: b023d3e0a6b06ca66d20f849ba344700a823fe06ca036f5a2fc188d57114f00f66c165a00824c8078733553c3cf54121ddbcc4880c7fdfd66ce6236c6ebe686c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.3.0
4
+
5
+ * Handles replacement strings containing escapes, like \n, \r and \t.
6
+
7
+
8
+ ## 0.2.0
9
+
10
+ * Added ability to insert just one or multiple instances of the new line.
11
+
12
+
3
13
  ## 0.1.0
4
14
 
5
15
  * Initial release.
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
- ## Installation For Use In a Ruby Program
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 'line 2', 'New line', 'my_file.txt'
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
 
@@ -1,3 +1,3 @@
1
1
  module InsertAfter
2
- VERSION = '0.1.0'.freeze unless defined? VERSION
2
+ VERSION = '0.3.0'.freeze unless defined? VERSION
3
3
  end
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
- temporary_file << "#{new_line}\n" if line.downcase.match?(/#{regex}/)
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: insert_after
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Slinn