class FileTransactions::CreateDirectoryCommand

This command creates a new directory. It will also create parent directories if the given path contains directories that doesn't exist.

When this command has been executed, the created directories can be removed again by calling undo.

Examples

# Pass in the new directory name to ::new
cmd1 = CreateDirectoryCommand.new('directory_name')

# The new directory name may be a path of multiple non exsting directories (like `mkdir -p`)
cmd2 = CreateDirectoryCommand.new('non_existing_dir1/non_existing_dir2/new_dir')

Attributes

name[R]

Public Class Methods

new(name) click to toggle source

@param name [String] The name of the new directory. May be just a name or an absolut or relative path

# File lib/file_transactions/create_directory_command.rb, line 23
def initialize(name)
  @name = name
end

Private Instance Methods

directories() click to toggle source
# File lib/file_transactions/create_directory_command.rb, line 44
def directories
  @directories ||= []
end
execute!() click to toggle source
# File lib/file_transactions/create_directory_command.rb, line 29
def execute!
  dir = name

  until Dir.exist? dir
    directories.unshift dir
    dir = File.dirname dir
  end

  directories.each { |dir| Dir.mkdir dir }
end
undo!() click to toggle source
# File lib/file_transactions/create_directory_command.rb, line 40
def undo!
  directories.reverse_each { |dir| Dir.unlink(dir) if Dir.exist? dir }
end