class Patterns::CreateFile
Creates a file and writes data to it, and provides a command for undoing that operation
Public Class Methods
new(path, data)
click to toggle source
Initializes the CreateFile Class
-
path
- The file path to write the file -
data
- The data to write to the file
Examples
=> create_file_cmd = CreateFile.new(file_path, data_to_write)
Calls superclass method
Patterns::Command.new
# File app/models/command/create_file.rb, line 16 def initialize(path, data) super("Create File: #{path}") @path = path @data = data end
Public Instance Methods
execute()
click to toggle source
Creates a file and writes data to it; will overwrite data if the file already exists. This function should probably be called write to file, with optional append instead of overwrite.
Examples
=> create_file_cmd.execute
Calls superclass method
Patterns::Command#execute
# File app/models/command/create_file.rb, line 28 def execute function = Proc.new do f = File.open(@path, 'w') f.write @data f.close end super(function) end
undo()
click to toggle source
Undoes file creation; to be more useful, this should probably only remove data if it was appended, and only remove the file if it was created.
Examples
=> create_file_cmd.undo
Calls superclass method
Patterns::Command#undo
# File app/models/command/create_file.rb, line 42 def undo function = Proc.new do File.delete(@path) end super(function) end