Class: BulldozeRenamer::FileContentSubstitutor
- Inherits:
-
Object
- Object
- BulldozeRenamer::FileContentSubstitutor
- Defined in:
- lib/bulldoze_renamer/file_content_substitutor.rb
Instance Method Summary collapse
- #full_path(path) ⇒ Object
-
#initialize(dir) ⇒ FileContentSubstitutor
constructor
A new instance of FileContentSubstitutor.
- #move_directory(original_path, new_path) ⇒ Object
- #substitute_in(original_path, new_path, mappings) ⇒ Object
- #with_content_written_to_temporary_file(original_path, content) ⇒ Object
Constructor Details
#initialize(dir) ⇒ FileContentSubstitutor
Returns a new instance of FileContentSubstitutor.
6 7 8 |
# File 'lib/bulldoze_renamer/file_content_substitutor.rb', line 6 def initialize(dir) @dir = dir end |
Instance Method Details
#full_path(path) ⇒ Object
10 11 12 |
# File 'lib/bulldoze_renamer/file_content_substitutor.rb', line 10 def full_path(path) File.join(@dir, path) end |
#move_directory(original_path, new_path) ⇒ Object
28 29 30 |
# File 'lib/bulldoze_renamer/file_content_substitutor.rb', line 28 def move_directory(original_path, new_path) FileUtils.mv(full_path(original_path), full_path(new_path)) end |
#substitute_in(original_path, new_path, mappings) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bulldoze_renamer/file_content_substitutor.rb', line 32 def substitute_in(original_path, new_path, mappings) content = File.read(full_path(original_path)) mappings.each do |m| content.gsub!(m[0], m[1]) end with_content_written_to_temporary_file(original_path, content) do |f| FileUtils.cp_r(f, full_path(new_path), preserve: true) if new_path != original_path FileUtils.rm(full_path(original_path)) end end end |
#with_content_written_to_temporary_file(original_path, content) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/bulldoze_renamer/file_content_substitutor.rb', line 14 def with_content_written_to_temporary_file(original_path, content) Dir.mktmpdir do |dir| filename = File.join(dir, 'replaced_content') # First copy original file to preserve permissions, even though we will overwrite FileUtils.cp_r(File.join(@dir, original_path), filename, preserve: true) File.open(filename, 'w') do |f| f.print content end yield(filename) end end |