Skip to content

Commit

Permalink
minor refactoring of @mode handling
Browse files Browse the repository at this point in the history
Doing this to ease the merging of the codedb-ffi-io branch
later on. While fixing some code in rubysl-socket it was noted
that it touched the @mode variable directly. To simplify
compatibility (forward and backward) with that gem, it makes
sense to move this @mode handling to its own methods.
  • Loading branch information
chuckremes committed Mar 30, 2016
1 parent 089e9eb commit 532cb80
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions core/io.rb
Expand Up @@ -1169,7 +1169,7 @@ def initialize(fd, mode=undefined, options=undefined)
(@external || Encoding.default_external) == Encoding::ASCII_8BIT
@internal = nil
end
elsif @mode != RDONLY
elsif !mode_read_only?
if Encoding.default_external != Encoding.default_internal
@internal = Encoding.default_internal
end
Expand Down Expand Up @@ -1278,7 +1278,7 @@ def <<(obj)
def close_read
return if closed?

if @mode == WRONLY || @mode == RDWR
if mode_write_only? || mode_read_write?
raise IOError, 'closing non-duplex IO for reading'
end
close
Expand All @@ -1300,7 +1300,7 @@ def close_read
def close_write
return if closed?

if @mode == RDONLY || @mode == RDWR
if mode_read_only? || mode_read_write?
raise IOError, 'closing non-duplex IO for writing'
end
close
Expand Down Expand Up @@ -1636,19 +1636,17 @@ def eof?

def ensure_open_and_readable
ensure_open
write_only = @mode & ACCMODE == WRONLY
raise IOError, "not opened for reading" if write_only
raise IOError, "not opened for reading" if mode_write_only?
end

def ensure_open_and_writable
ensure_open
read_only = @mode & ACCMODE == RDONLY
raise IOError, "not opened for writing" if read_only
raise IOError, "not opened for writing" if mode_read_only?
end

def external_encoding
return @external if @external
return Encoding.default_external if @mode == RDONLY
return Encoding.default_external if mode_read_only?
end

##
Expand Down Expand Up @@ -1747,6 +1745,16 @@ def flush
self
end

def force_read_only
@mode = (@mode & ~ACCMODE ) | RDONLY
end
private :force_read_only

def force_write_only
@mode = (@mode & ~ACCMODE) | WRONLY
end
private :force_write_only

##
# Immediately writes all buffered data in ios to disk. Returns
# nil if the underlying operating system does not support fsync(2).
Expand Down Expand Up @@ -1831,6 +1839,21 @@ def lineno=(line_number)
@lineno = Integer(line_number)
end

def mode_read_only?
(@mode & ACCMODE) == RDONLY
end
private :mode_read_only?

def mode_read_write?
(@mode & ACCMODE) == RDWR
end
private :mode_read_write?

def mode_write_only?
(@mode & ACCMODE) == WRONLY
end
private :mode_write_only?

##
# FIXME
# Returns the process ID of a child process
Expand Down Expand Up @@ -2234,7 +2257,7 @@ def reopen(other, mode=undefined)
mode = @mode
# If this IO was already opened for writing, we should
# create the target file if it doesn't already exist.
if (mode & RDWR == RDWR) || (mode & WRONLY == WRONLY)
if mode_read_write? || mode_write_only?
mode |= CREAT
end
else
Expand Down Expand Up @@ -2301,7 +2324,7 @@ def set_encoding(external, internal=nil, options=undefined)
when String
@external = nil
when nil
if @mode == RDONLY || @external
if mode_read_only? || @external
@external = nil
else
@external = Encoding.default_external
Expand Down

0 comments on commit 532cb80

Please sign in to comment.