class MemFs::IO
Attributes
autoclose[W]
close_on_exec[W]
closed[RW]
entry[RW]
opening_mode[RW]
path[R]
Public Class Methods
read(path, *args)
click to toggle source
# File lib/memfs/io.rb, line 17 def self.read(path, *args) options = args.last.is_a?(Hash) ? args.pop : {} options = { mode: File::RDONLY, encoding: nil, open_args: nil }.merge(options) open_args = options[:open_args] || [options[:mode], encoding: options[:encoding]] length, offset = args file = open(path, *open_args) file.seek(offset || 0) file.read(length) ensure file.close if file end
write(path, string, offset = 0, open_args = nil)
click to toggle source
# File lib/memfs/io.rb, line 36 def self.write(path, string, offset = 0, open_args = nil) open_args ||= [File::WRONLY, encoding: nil] offset = 0 if offset.nil? unless offset.respond_to?(:to_int) fail TypeError, "no implicit conversion from #{offset.class}" end offset = offset.to_int if offset > 0 fail NotImplementedError, 'MemFs::IO.write with offset not yet supported.' end file = open(path, *open_args) file.seek(offset) file.write(string) ensure file.close if file end
Private Class Methods
original_io_class()
click to toggle source
# File lib/memfs/io.rb, line 57 def self.original_io_class MemFs::OriginalIO end
Public Instance Methods
<<(object)
click to toggle source
# File lib/memfs/io.rb, line 65 def <<(object) fail IOError, 'not opened for writing' unless writable? content << object.to_s end
advise(advice_type, _offset = 0, _len = 0)
click to toggle source
# File lib/memfs/io.rb, line 71 def advise(advice_type, _offset = 0, _len = 0) advice_types = [ :dontneed, :noreuse, :normal, :random, :sequential, :willneed ] unless advice_types.include?(advice_type) fail NotImplementedError, "Unsupported advice: #{advice_type.inspect}" end nil end
autoclose?()
click to toggle source
# File lib/memfs/io.rb, line 86 def autoclose? defined?(@autoclose) ? !!@autoclose : true end
binmode()
click to toggle source
# File lib/memfs/io.rb, line 90 def binmode @binmode = true @external_encoding = Encoding::ASCII_8BIT self end
binmode?()
click to toggle source
# File lib/memfs/io.rb, line 96 def binmode? defined?(@binmode) ? @binmode : false end
close()
click to toggle source
# File lib/memfs/io.rb, line 100 def close self.closed = true end
close_on_exec?()
click to toggle source
# File lib/memfs/io.rb, line 108 def close_on_exec? defined?(@close_on_exec) ? !!@close_on_exec : true end
closed?()
click to toggle source
# File lib/memfs/io.rb, line 104 def closed? closed end
each(sep = $/) { |line| ... }
click to toggle source
# File lib/memfs/io.rb, line 125 def each(sep = $/) return to_enum(__callee__) unless block_given? fail IOError, 'not opened for reading' unless readable? content.each_line(sep) { |line| yield(line) } self end
each_byte() { |byte| ... }
click to toggle source
# File lib/memfs/io.rb, line 132 def each_byte return to_enum(__callee__) unless block_given? fail IOError, 'not opened for reading' unless readable? content.each_byte { |byte| yield(byte) } self end
Also aliased as: bytes
each_char() { |char| ... }
click to toggle source
# File lib/memfs/io.rb, line 140 def each_char return to_enum(__callee__) unless block_given? fail IOError, 'not opened for reading' unless readable? content.each_char { |char| yield(char) } self end
Also aliased as: chars
eof?()
click to toggle source
# File lib/memfs/io.rb, line 112 def eof? pos >= content.size end
Also aliased as: eof
external_encoding()
click to toggle source
# File lib/memfs/io.rb, line 117 def external_encoding if writable? @external_encoding else @external_encoding ||= Encoding.default_external end end
pos()
click to toggle source
# File lib/memfs/io.rb, line 148 def pos entry.pos end
print(*objs)
click to toggle source
# File lib/memfs/io.rb, line 152 def print(*objs) objs << $_ if objs.empty? self << objs.join($,) << $\.to_s nil end
printf(format_string, *objs)
click to toggle source
# File lib/memfs/io.rb, line 158 def printf(format_string, *objs) print format_string % objs end
puts(text)
click to toggle source
# File lib/memfs/io.rb, line 162 def puts(text) fail IOError, 'not opened for writing' unless writable? content.puts text end
read(length = nil, buffer = '')
click to toggle source
# File lib/memfs/io.rb, line 168 def read(length = nil, buffer = '') unless entry fail(Errno::ENOENT, path) end default = length ? nil : '' content.read(length, buffer) || default end
seek(amount, whence = ::IO::SEEK_SET)
click to toggle source
# File lib/memfs/io.rb, line 176 def seek(amount, whence = ::IO::SEEK_SET) new_pos = case whence when ::IO::SEEK_CUR then entry.pos + amount when ::IO::SEEK_END then content.to_s.length + amount when ::IO::SEEK_SET then amount end fail Errno::EINVAL, path if new_pos.nil? || new_pos < 0 entry.pos = new_pos 0 end
stat()
click to toggle source
# File lib/memfs/io.rb, line 189 def stat File.stat(path) end
write(string)
click to toggle source
# File lib/memfs/io.rb, line 193 def write(string) fail IOError, 'not opened for writing' unless writable? content.write(string.to_s) end
Private Instance Methods
content()
click to toggle source
# File lib/memfs/io.rb, line 207 def content entry.content end
create_file?()
click to toggle source
# File lib/memfs/io.rb, line 211 def create_file? (opening_mode & File::CREAT).nonzero? end
readable?()
click to toggle source
# File lib/memfs/io.rb, line 215 def readable? (opening_mode & File::RDWR).nonzero? || (opening_mode | File::RDONLY).zero? end
str_to_mode_int(mode)
click to toggle source
# File lib/memfs/io.rb, line 220 def str_to_mode_int(mode) return mode unless mode.is_a?(String) unless mode =~ /\A([rwa]\+?)([bt])?(:bom)?(\|.+)?\z/ fail ArgumentError, "invalid access mode #{mode}" end mode_str = $~[1] File::MODE_MAP[mode_str] end
truncate_file?()
click to toggle source
# File lib/memfs/io.rb, line 231 def truncate_file? (opening_mode & File::TRUNC).nonzero? end
writable?()
click to toggle source
# File lib/memfs/io.rb, line 235 def writable? (opening_mode & File::WRONLY).nonzero? || (opening_mode & File::RDWR).nonzero? end