class Patterns::DeleteFile

DeleteFile -> Command

Deletes a file, and provides the ability to undo that operation

Public Class Methods

new(path) click to toggle source

Initializes the DeleteFile Class

  • path - The path of the file to delete

Examples

=> delete_file_cmd = DeleteFile.new(file_path)
Calls superclass method Patterns::Command.new
# File app/models/command/delete_file.rb, line 15
def initialize(path)
  super("Delete File: #{path}")
  @path = path
  @data = nil
end

Public Instance Methods

execute() click to toggle source

Deletes a file if it exists

Examples

=> delete_file_cmd.execute
Calls superclass method Patterns::Command#execute
# File app/models/command/delete_file.rb, line 26
def execute
  function = Proc.new do
    if File.exist?(@path)
      @data = File.read(@path)
    end
    File.delete(@path)
  end
  super(function)
end
undo() click to toggle source

Replaces a file that was deleted

Examples

=> delete_file_cmd.undo
Calls superclass method Patterns::Command#undo
# File app/models/command/delete_file.rb, line 41
def undo
  function = Proc.new do
    if @data != nil
      f = File.open(@path, 'w')
      f.write @data
      f.close
    end
  end
  super(function)
end