Skip to content

Commit

Permalink
ARROW-4034: [Ruby] Add support :append option to FileOutputStream
Browse files Browse the repository at this point in the history
Author: Kouhei Sutou <[email protected]>

Closes apache#3193 from kou/ruby-file-output-stream-append and squashes the following commits:

6240f4b <Kouhei Sutou>  Add support :append option to FileOutputStream
  • Loading branch information
kou authored and shiro615 committed Dec 17, 2018
1 parent 77d3a46 commit 5d1934f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions ruby/red-arrow/lib/arrow/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
module Arrow
class Field
alias_method :initialize_raw, :initialize
private :initialize_raw
def initialize(name, data_type)
case data_type
when String, Symbol
Expand Down
34 changes: 34 additions & 0 deletions ruby/red-arrow/lib/arrow/file-output-stream.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Arrow
class FileOutputStream
alias_method :initialize_raw, :initialize
private :initialize_raw
def initialize(path, options={})
append = nil
case options
when true, false
append = options
when Hash
append = options[:append]
end
append = false if append.nil?
initialize_raw(path, append)
end
end
end
1 change: 1 addition & 0 deletions ruby/red-arrow/lib/arrow/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def require_libraries
require "arrow/date64-array"
require "arrow/date64-array-builder"
require "arrow/field"
require "arrow/file-output-stream"
require "arrow/path-extension"
require "arrow/record"
require "arrow/record-batch"
Expand Down
1 change: 1 addition & 0 deletions ruby/red-arrow/lib/arrow/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def load(path, options={})
end

alias_method :initialize_raw, :initialize
private :initialize_raw
def initialize(schema_or_raw_table_or_columns, columns=nil)
if columns.nil?
if schema_or_raw_table_or_columns[0].is_a?(Column)
Expand Down
54 changes: 54 additions & 0 deletions ruby/red-arrow/test/test-file-output-stream.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

class TestFileOutputStream < Test::Unit::TestCase
sub_test_case(".open") do
def setup
@file = Tempfile.open("arrow-file-output-stream")
@file.write("Hello")
@file.close
end

def test_default
Arrow::FileOutputStream.open(@file.path) do |file|
file.write(" World")
end
assert_equal(" World", File.read(@file.path))
end

def test_options_append
Arrow::FileOutputStream.open(@file.path, append: true) do |file|
file.write(" World")
end
assert_equal("Hello World", File.read(@file.path))
end

def test_append_true
Arrow::FileOutputStream.open(@file.path, true) do |file|
file.write(" World")
end
assert_equal("Hello World", File.read(@file.path))
end

def test_append_false
Arrow::FileOutputStream.open(@file.path, false) do |file|
file.write(" World")
end
assert_equal(" World", File.read(@file.path))
end
end
end

0 comments on commit 5d1934f

Please sign in to comment.