class Patterns::CopyFile

CopyFile -> Command

Class that implements a command to Copy a file from one location to another, and to undo that copy

Public Class Methods

new(source, target) click to toggle source

Initializes the CopyFile Class

  • source - The source path for the file to be copied

  • target - The target destination for the copied file

Examples

=> copy_file_cmd = CopyFile.new(file_to_copy, destination)
Calls superclass method Patterns::Command.new
# File app/models/command/copy_file.rb, line 16
def initialize(source, target)
  super("Copy File: #{source} to file: #{target}")
  @source = source
  @target = target
end

Public Instance Methods

execute() click to toggle source

Copies a file from a source location to a target destination

Examples

=> copy_file_cmd.execute
Calls superclass method Patterns::Command#execute
# File app/models/command/copy_file.rb, line 27
def execute
  function = Proc.new do
    if File.exists?(@target)
      @data = File.read(@target)
    end
    FileUtils.copy(@source, @target)
  end
  super(function)
end
undo() click to toggle source

Undoes a copy operation

Examples

=> copy_file_cmd.undo
Calls superclass method Patterns::Command#undo
# File app/models/command/copy_file.rb, line 42
def undo
  function = Proc.new do
      File.delete(@target)
  end
  super(function)
end