Class: FileTransactions::CreateFileCommand
- Inherits:
-
BaseCommand
- Object
- BaseCommand
- FileTransactions::CreateFileCommand
- 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
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, &block) ⇒ CreateFileCommand
constructor
A new instance of CreateFileCommand.
Methods inherited from BaseCommand
execute, #execute, #executed?, #failed?, #register, #undo
Constructor Details
#initialize(name, &block) ⇒ CreateFileCommand
Returns a new instance of CreateFileCommand.
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
#block ⇒ Object (readonly)
Returns the value of attribute block.
30 31 32 |
# File 'lib/file_transactions/create_file_command.rb', line 30 def block @block end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
30 31 32 |
# File 'lib/file_transactions/create_file_command.rb', line 30 def name @name end |