Class: Bak::FileCopier

Inherits:
Object
  • Object
show all
Defined in:
lib/bak/copier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backupNameGenerator, output_stream) ⇒ FileCopier

Returns a new instance of FileCopier.



4
5
6
7
8
9
# File 'lib/bak/copier.rb', line 4

def initialize(backupNameGenerator, output_stream)
    @generator = backupNameGenerator
    @output = output_stream
    @start_file = @generator.filename
    @end_file = @generator.start
end

Instance Attribute Details

#end_fileObject

Returns the value of attribute end_file.



3
4
5
# File 'lib/bak/copier.rb', line 3

def end_file
  @end_file
end

#generatorObject

Returns the value of attribute generator.



3
4
5
# File 'lib/bak/copier.rb', line 3

def generator
  @generator
end

#start_fileObject

Returns the value of attribute start_file.



3
4
5
# File 'lib/bak/copier.rb', line 3

def start_file
  @start_file
end

Instance Method Details

#check_errorsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bak/copier.rb', line 11

def check_errors
    unless File.exists?(@start_file)
        return "#{start_file}: No such file or directory"
    end
    if File.exists?(@end_file) && !@generator[:force]
        return "#{end_file}: File already exists"
    end
    if @generator[:create_path] == true
      FileUtils::mkdir_p @generator[:target_path]
    end
    if @generator[:target_path] && !File.directory?(@generator[:target_path])
      return "#{@generator[:target_path]} directory does not exist"
    end
end

#startObject



26
27
28
29
30
31
32
33
# File 'lib/bak/copier.rb', line 26

def start
    errors = check_errors
    if errors
        @output.puts errors
    else
        FileUtils.cp_r(start_file, end_file)
    end
end