Class: DevelopWithPassion::Expander::FileMerge

Inherits:
Object
  • Object
show all
Defined in:
lib/developwithpassion_expander/file_merge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_file) ⇒ FileMerge

Returns a new instance of FileMerge.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/developwithpassion_expander/file_merge.rb', line 6

def initialize(output_file)
  array :before_files do|a|
    a.read_and_write
    a.mutator :add_before_original_contents do|file|
      add_merge_file(@before_files,file)
    end
    a.mutator :add do|file|
      add_before_original_contents(file)
    end
  end
  array :after_files do|a|
    a.readable
    a.writable
    a.mutator :add_after_original_contents do|file|
      add_merge_file(@after_files,file)
    end
  end
  @output_file = output_file
  @read_original_contents = true
end

Instance Attribute Details

#after_filesObject

Returns the value of attribute after_files.



4
5
6
# File 'lib/developwithpassion_expander/file_merge.rb', line 4

def after_files
  @after_files
end

#before_filesObject

Returns the value of attribute before_files.



4
5
6
# File 'lib/developwithpassion_expander/file_merge.rb', line 4

def before_files
  @before_files
end

#read_original_contentsObject

Returns the value of attribute read_original_contents.



4
5
6
# File 'lib/developwithpassion_expander/file_merge.rb', line 4

def read_original_contents
  @read_original_contents
end

Instance Method Details

#add_merge_file(items, file) ⇒ Object



48
49
50
51
# File 'lib/developwithpassion_expander/file_merge.rb', line 48

def add_merge_file(items,file)
  return if items.include?(file)
  items << file if File.exists?(file)
end

#dont_read_original_file_contentsObject



27
28
29
# File 'lib/developwithpassion_expander/file_merge.rb', line 27

def dont_read_original_file_contents
  @read_original_contents = false
end

#merge_files(source_files, target) ⇒ Object



41
42
43
44
45
# File 'lib/developwithpassion_expander/file_merge.rb', line 41

def merge_files(source_files,target)
  source_files.each do|source|
    target.write File.read_all_text(source)
  end
end

#runObject



31
32
33
34
35
36
37
38
39
# File 'lib/developwithpassion_expander/file_merge.rb', line 31

def run
  original_contents = File.read_all_text(@output_file)
  FileUtils.rm_f @output_file
  File.open_for_write(@output_file) do |file|
    merge_files(@before_files,file)
    file.write original_contents if @read_original_contents
    merge_files(@after_files,file)
  end
end