Module: InsertFromFile
- Defined in:
- lib/insert_from_file.rb,
lib/insert_from_file/version.rb
Constant Summary collapse
- VERSION =
'0.0.5'.freeze
Class Method Summary collapse
- .add_after(file_source, file_dest, str_dest) ⇒ Object
- .add_before(file_source, file_dest, str_dest) ⇒ Object
- .add_beginning(file_source, file_dest) ⇒ Object
- .add_end(file_source, file_dest) ⇒ Object
- .replace(file_source, file_dest, str_dest) ⇒ Object
- .replace_between(file_source, file_dest, str1, str2) ⇒ Object
Class Method Details
.add_after(file_source, file_dest, str_dest) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/insert_from_file.rb', line 25 def self.add_after(file_source, file_dest, str_dest) path_old = file_dest path_new = "#{path_old}.new" file_w = File.open(path_new, 'w') File.readlines(path_old).each do |line| file_w.write(line) # rubocop:disable Style/Next if line.include? str_dest File.readlines(file_source).each do |line_s| file_w.write(line_s) end end # rubocop:enable Style/Next end file_w.close system("rm #{path_old}") system("mv #{path_new} #{path_old}") end |
.add_before(file_source, file_dest, str_dest) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/insert_from_file.rb', line 8 def self.add_before(file_source, file_dest, str_dest) path_old = file_dest path_new = "#{path_old}.new" file_w = File.open(path_new, 'w') File.readlines(path_old).each do |line| if line.include? str_dest File.readlines(file_source).each do |line_s| file_w.write(line_s) end end file_w.write(line) end file_w.close system("rm #{path_old}") system("mv #{path_new} #{path_old}") end |
.add_beginning(file_source, file_dest) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/insert_from_file.rb', line 89 def self.add_beginning(file_source, file_dest) path_old = file_dest path_new = "#{path_old}.new" file_w = File.open(path_new, 'w') File.readlines(file_source).each do |line| file_w.write(line) end File.readlines(path_old).each do |line| file_w.write(line) end file_w.close system("rm #{path_old}") system("mv #{path_new} #{path_old}") end |
.add_end(file_source, file_dest) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/insert_from_file.rb', line 104 def self.add_end(file_source, file_dest) path_old = file_dest path_new = "#{path_old}.new" file_w = File.open(path_new, 'w') File.readlines(path_old).each do |line| file_w.write(line) end File.readlines(file_source).each do |line| file_w.write(line) end file_w.close system("rm #{path_old}") system("mv #{path_new} #{path_old}") end |
.replace(file_source, file_dest, str_dest) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/insert_from_file.rb', line 44 def self.replace(file_source, file_dest, str_dest) path_old = file_dest path_new = "#{path_old}.new" file_w = File.open(path_new, 'w') File.readlines(path_old).each do |line| if line.include? str_dest File.readlines(file_source).each do |line_s| file_w.write(line_s) end else file_w.write(line) end end file_w.close system("rm #{path_old}") system("mv #{path_new} #{path_old}") end |
.replace_between(file_source, file_dest, str1, str2) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/insert_from_file.rb', line 62 def self.replace_between(file_source, file_dest, str1, str2) LineContaining.delete_between(str1, str2, file_dest) path_old = file_dest path_new = "#{path_old}.new" file_w = File.open(path_new, 'w') is_before = true is_after = false File.readlines(path_old).each do |line| if is_before == true # Beginning file_w.write(line) if line.include? str1 File.readlines(file_source).each do |line_s| file_w.write(line_s) end end elsif is_after == true # End file_w.write(line) elsif line.include? str2 # Middle file_w.write(line) is_after = true end end file_w.close system("rm #{path_old}") system("mv #{path_new} #{path_old}") end |