Class: FileTransactions::CreateFileCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/file_transactions/create_file_command.rb

Overview

This command creates a new file. It will also create parent directories if the given path contains directories that doesn’t exist. The block passed to ::new must either return a String with the content that will be written to the new file. Or the block must itself create the file with the filname name (and return anything but a String).

When this command has been executed, the created file (and any directories) can be removed again by calling #undo.

Examples

# Pass in the new directory name to ::new
cmd1 = CreateFileCommand.new('new_name') do
  <<~EOF
    Some content to be
    written to the file.
  EOF
end

# Files can also be created manually.
# Note: the block gets name as argument.
cmd2 = CreateFileCommand.new('another_file') do |name|
  GenerateAwesomeReport.call(filename: name)
  true
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

execute, #execute, #executed?, #failed?, #register, #undo

Constructor Details

#initialize(name, &block) ⇒ CreateFileCommand

Returns a new instance of CreateFileCommand.

Parameters:

  • name (String)

    The name of the new directory. May be just a name or an absolut or relative path



33
34
35
36
# File 'lib/file_transactions/create_file_command.rb', line 33

def initialize(name, &block)
  @name = name
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



30
31
32
# File 'lib/file_transactions/create_file_command.rb', line 30

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/file_transactions/create_file_command.rb', line 30

def name
  @name
end