lib/io_streams/paths/file.rb in iostreams-1.0.0.beta7 vs lib/io_streams/paths/file.rb in iostreams-1.0.0
- old
+ new
@@ -1,10 +1,17 @@
require "fileutils"
module IOStreams
module Paths
class File < IOStreams::Path
+ attr_accessor :create_path
+
+ def initialize(file_name, create_path: true)
+ @create_path = create_path
+ super(file_name)
+ end
+
# Yields Paths within the current path.
#
# Examples:
#
# # Case Insensitive file name lookup:
@@ -144,23 +151,25 @@
# Returns the real path by stripping `.`, `..` and expands any symlinks.
def realpath
self.class.new(::File.realpath(path))
end
+ private
+
# Read from file
- def reader(&block)
- ::File.open(path, "rb") { |io| streams.reader(io, &block) }
+ def stream_reader(&block)
+ ::File.open(path, "rb") { |io| builder.reader(io, &block) }
end
# Write to file
#
# Note:
# If an exception is raised whilst the file is being written to the file is removed to
# prevent incomplete / partial files from being created.
- def writer(create_path: true, &block)
+ def stream_writer(&block)
mkpath if create_path
begin
- ::File.open(path, "wb") { |io| streams.writer(io, &block) }
+ ::File.open(path, "wb") { |io| builder.writer(io, &block) }
rescue StandardError => e
::File.unlink(path) if ::File.exist?(path)
raise(e)
end
end