class FileTransactions::MoveFileCommand

This command supports moveing (renaming) a file. It will also create parent directories if the given path contains directories that doesn't exist.

When this command has been executed, the file can be moved back to the original location again by calling undo. Any directories created during execute will be deleted.

Examples

cmd1 = MoveFileCommand.new('some_existing_file', 'some_new_dir/a_new_name')

Attributes

from[R]
to[R]

Public Class Methods

new(from:, to:) click to toggle source

@param from [String] The name of the source file to be renamed. May be just a name or an absolut or relative path @param to [String] The target name of the file. May be just a name or an absolut or relative path

# File lib/file_transactions/move_file_command.rb, line 19
def initialize(from:, to:)
  @from = from
  @to = to
end

Private Instance Methods

before() click to toggle source
# File lib/file_transactions/move_file_command.rb, line 26
def before
  CreateDirectoryCommand.execute(File.dirname(to))
end
execute!() click to toggle source
# File lib/file_transactions/move_file_command.rb, line 30
def execute!
  File.rename from, to
end
undo!() click to toggle source
# File lib/file_transactions/move_file_command.rb, line 34
def undo!
  File.rename to, from
end