Class: Expansions::FileMerge
- Inherits:
-
Object
- Object
- Expansions::FileMerge
- Includes:
- ArrayFu
- Defined in:
- lib/expansions/file_merge.rb
Instance Attribute Summary collapse
-
#read_original_contents ⇒ Object
Returns the value of attribute read_original_contents.
Instance Method Summary collapse
- #add_merge_file(items, file) ⇒ Object
- #dont_read_original_file_contents ⇒ Object
-
#initialize(output_file) ⇒ FileMerge
constructor
A new instance of FileMerge.
- #merge_files(source_files, target) ⇒ Object
- #run ⇒ Object
- #write_contents(target_file_stream, file_to_read) ⇒ Object
Constructor Details
#initialize(output_file) ⇒ FileMerge
Returns a new instance of FileMerge.
26 27 28 29 30 |
# File 'lib/expansions/file_merge.rb', line 26 def initialize(output_file) super @output_file = output_file @read_original_contents = true end |
Instance Attribute Details
#read_original_contents ⇒ Object
Returns the value of attribute read_original_contents.
4 5 6 |
# File 'lib/expansions/file_merge.rb', line 4 def read_original_contents @read_original_contents end |
Instance Method Details
#add_merge_file(items, file) ⇒ Object
60 61 62 63 |
# File 'lib/expansions/file_merge.rb', line 60 def add_merge_file(items,file) return if items.include?(file) items << file if File.exist?(file) end |
#dont_read_original_file_contents ⇒ Object
32 33 34 |
# File 'lib/expansions/file_merge.rb', line 32 def dont_read_original_file_contents @read_original_contents = false end |
#merge_files(source_files, target) ⇒ Object
54 55 56 57 58 |
# File 'lib/expansions/file_merge.rb', line 54 def merge_files(source_files,target) source_files.each do|source| write_contents(target,source) end end |
#run ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/expansions/file_merge.rb', line 36 def run copy_name = File.join(File.dirname(@output_file),"copy_of_#{File.basename(@output_file)}") FileUtils.cp @output_file, copy_name if File.exist?(@output_file) FileUtils.rm_f @output_file File.open_for_write(@output_file) do |file| merge_files(@before_files,file) write_contents(file,copy_name) if @read_original_contents && File.exist?(copy_name) merge_files(@after_files,file) end FileUtils.rm_f copy_name if File.exist?(copy_name) end |
#write_contents(target_file_stream, file_to_read) ⇒ Object
48 49 50 51 52 |
# File 'lib/expansions/file_merge.rb', line 48 def write_contents(target_file_stream,file_to_read) File.open_for_read(file_to_read) do|line| target_file_stream << line end end |