Created
January 31, 2016 19:53
-
-
Save KamilLelonek/ae41145fc75b8ef04d10 to your computer and use it in GitHub Desktop.
No overwrite FileWriter in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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