Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Created January 31, 2016 19:53
Show Gist options
  • Save KamilLelonek/ae41145fc75b8ef04d10 to your computer and use it in GitHub Desktop.
Save KamilLelonek/ae41145fc75b8ef04d10 to your computer and use it in GitHub Desktop.
No overwrite FileWriter in Ruby
class NoOverwriteFileWriter
def initialize(default_file_writer)
@default_file_writer = default_file_writer
end
def write(path, contents)
fail_if_file_exist(path)
default_file_writer.write(path, contents)
end
private
attr_reader :default_file_writer
def fail_if_file_exist(path)
raise 'File already exists!' if file_exist?(path)
end
def file_exist?(path)
File.exist?(path) && !File.zero?(path)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment