lib/io_streams/stream.rb in iostreams-1.6.2 vs lib/io_streams/stream.rb in iostreams-1.7.0
- old
+ new
@@ -149,10 +149,13 @@
#
# :convert [true|false]
# Whether to apply the stream conversions during the copy.
# Default: true
#
+ # :mode [:line, :array, :hash]
+ # When convert is `true` then use this mode to convert the contents of the file.
+ #
# Examples:
#
# # Copy and convert streams based on file extensions
# IOStreams.path("target_file.json").copy_from("source_file_name.csv.gz")
#
@@ -160,27 +163,33 @@
# IOStreams.path("target_file.json").copy_from("source_file_name.csv.gz", convert: false)
#
# # Advanced copy with custom stream conversions on source and target.
# source = IOStreams.path("source_file").stream(encoding: "BINARY")
# IOStreams.path("target_file.pgp").option(:pgp, passphrase: "hello").copy_from(source)
- def copy_from(source, convert: true)
+ def copy_from(source, convert: true, mode: nil, **args)
if convert
stream = IOStreams.new(source)
- writer do |target|
- stream.reader { |src| IO.copy_stream(src, target) }
+ if mode
+ writer(mode, **args) do |target|
+ stream.each(mode) { |row| target << row }
+ end
+ else
+ writer(**args) do |target|
+ stream.reader { |src| IO.copy_stream(src, target) }
+ end
end
else
stream = source.is_a?(Stream) ? source.dup : IOStreams.new(source)
dup.stream(:none).writer do |target|
stream.stream(:none).reader { |src| IO.copy_stream(src, target) }
end
end
end
- def copy_to(target, convert: true)
+ def copy_to(target, **args)
target = IOStreams.new(target)
- target.copy_from(self, convert: convert)
+ target.copy_from(self, **args)
end
# Set/get the original file_name
def file_name(file_name = :none)
if file_name == :none
@@ -363,12 +372,12 @@
line_writer(delimiter: delimiter) do |io|
IOStreams::Row::Writer.stream(
io,
original_file_name: builder.file_name,
- format: builder.format,
- format_options: builder.format_options,
+ format: builder.format,
+ format_options: builder.format_options,
**args,
&block
)
end
end
@@ -378,13 +387,14 @@
line_writer(delimiter: delimiter) do |io|
IOStreams::Record::Writer.stream(
io,
original_file_name: builder.file_name,
- format: builder.format,
- format_options: builder.format_options,
+ format: builder.format,
+ format_options: builder.format_options,
**args,
- &block)
+ &block
+ )
end
end
end
end