Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CSV delegation to missing StringIO methods #80

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions lib/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1106,12 +1106,44 @@ def line
### IO and StringIO Delegation ###

extend Forwardable
def_delegators :@io, :binmode, :binmode?, :close, :close_read, :close_write,
def_delegators :@io, :binmode, :close, :close_read, :close_write,
:closed?, :external_encoding, :fcntl,
:fileno, :flock, :flush, :fsync, :internal_encoding,
:ioctl, :isatty, :path, :pid, :pos, :pos=, :reopen,
:seek, :stat, :string, :sync, :sync=, :tell, :to_i,
:to_io, :truncate, :tty?
:fileno, :flush, :fsync, :internal_encoding,
:isatty, :pid, :pos, :pos=, :reopen,
:seek, :string, :sync, :sync=, :tell,
:truncate, :tty?

def binmode?
!!(@io.binmode? if @io.respond_to?(:binmode?))
end

def flock(*args)
raise NotImplementedError unless @io.respond_to?(:flock)
@io.flock(*args)
end

def ioctl(*args)
raise NotImplementedError unless @io.respond_to?(:ioctl)
@io.ioctl(*args)
end

def path
@io.path if @io.respond_to?(:path)
end

def stat(*args)
raise NotImplementedError unless @io.respond_to?(:stat)
@io.stat(*args)
end

def to_i
raise NotImplementedError unless @io.respond_to?(:to_i)
@io.to_i
end

def to_io
@io.respond_to?(:to_io) ? @io.to_io : @io
end

def eof?
begin
Expand Down
18 changes: 18 additions & 0 deletions test/csv/interface/test_delegation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: false

require_relative "../helper"

class TestCSVInterfaceDelegation < Test::Unit::TestCase
def test_stringio_missing_methods_delegation
csv = CSV.new("h1,h2")

assert_raise(NotImplementedError) { csv.flock(0) }
assert_raise(NotImplementedError) { csv.ioctl(0) }
assert_raise(NotImplementedError) { csv.stat }
assert_raise(NotImplementedError) { csv.to_i }

assert_equal(false, csv.binmode?)
assert_equal(nil, csv.path)
assert_instance_of(StringIO, csv.to_io)
end
end