Class: Pandocomatic::CopyFileCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/pandocomatic/command/copy_file_command.rb

Overview

A command to copy a file

Instance Attribute Summary collapse

Attributes inherited from Command

#errors, #index

Instance Method Summary collapse

Methods inherited from Command

#all_errors, #count, #debug?, #directory?, #dry_run?, #errors?, #execute, #file_modified?, #index_to_s, #make_quiet, #modified_only?, #multiple?, #quiet?, reset, #runnable?, #skip?, #src_root, #uncount

Constructor Details

#initialize(src, dst) ⇒ CopyFileCommand

Create a new CopyFileCommand

Parameters:

  • src (String)

    path to the file to copy

  • dst (String)

    path to the place to copy the source file to



38
39
40
41
42
43
44
# File 'lib/pandocomatic/command/copy_file_command.rb', line 38

def initialize(src, dst)
  super()
  @src = src
  @dst = dst
  @errors.push IOError.new(:file_is_not_readable, nil, @src) unless File.readable? @src
  @errors.push IOError.new(:file_is_not_writable, nil, @dst) unless !File.exist?(@dst) || File.writable?(@dst)
end

Instance Attribute Details

#srcString

Returns path to the file to copy.

Returns:

  • (String)

    path to the file to copy



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pandocomatic/command/copy_file_command.rb', line 31

class CopyFileCommand < Command
  attr_reader :src

  # Create a new CopyFileCommand
  #
  # @param src [String] path to the file to copy
  # @param dst [String] path to the place to copy the source file to
  def initialize(src, dst)
    super()
    @src = src
    @dst = dst
    @errors.push IOError.new(:file_is_not_readable, nil, @src) unless File.readable? @src
    @errors.push IOError.new(:file_is_not_writable, nil, @dst) unless !File.exist?(@dst) || File.writable?(@dst)
  end

  # Run this CopyFileCommand
  def run
    FileUtils.cp(@src, @dst) if file_modified?(@src, @dst)
  rescue StandardError => e
    raise IOError.new(:unable_to_copy_file, e, [@src, @dst])
  end

  # A string representation of this CopyFileCommand
  #
  # @return [String]
  def to_s
    "copy #{File.basename @src}"
  end
end

Instance Method Details

#runObject

Run this CopyFileCommand



47
48
49
50
51
# File 'lib/pandocomatic/command/copy_file_command.rb', line 47

def run
  FileUtils.cp(@src, @dst) if file_modified?(@src, @dst)
rescue StandardError => e
  raise IOError.new(:unable_to_copy_file, e, [@src, @dst])
end

#to_sString

A string representation of this CopyFileCommand

Returns:

  • (String)


56
57
58
# File 'lib/pandocomatic/command/copy_file_command.rb', line 56

def to_s
  "copy #{File.basename @src}"
end