Class: MemFs::IO
- Inherits:
-
Object
- Object
- MemFs::IO
- Extended by:
- SingleForwardable
- Includes:
- Constants
- Defined in:
- lib/memfs/io.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#autoclose ⇒ Object
writeonly
Sets the attribute autoclose.
-
#close_on_exec ⇒ Object
writeonly
Sets the attribute close_on_exec.
Class Method Summary collapse
- .read(path, *args) ⇒ Object
-
.write(path, string, offset = 0, open_args = nil) ⇒ Object
rubocop:disable Metrics/MethodLength.
Instance Method Summary collapse
- #<<(object) ⇒ Object
- #advise(advice_type, _offset = 0, _len = 0) ⇒ Object
- #autoclose? ⇒ Boolean
- #binmode ⇒ Object
- #binmode? ⇒ Boolean
- #close ⇒ Object
- #close_on_exec? ⇒ Boolean
- #closed? ⇒ Boolean
- #each(sep = $/, &block) ⇒ Object
- #each_byte(&block) ⇒ Object (also: #bytes)
- #each_char(&block) ⇒ Object (also: #chars)
- #eof? ⇒ Boolean (also: #eof)
- #external_encoding ⇒ Object
- #fileno ⇒ Object
- #pos ⇒ Object
- #print(*objs) ⇒ Object
- #printf(format_string, *objs) ⇒ Object
- #puts(text) ⇒ Object
- #read(length = nil, buffer = +'')) ⇒ Object
- #seek(amount, whence = ::IO::SEEK_SET) ⇒ Object
- #stat ⇒ Object
- #write(string) ⇒ Object
Instance Attribute Details
#autoclose=(value) ⇒ Object (writeonly)
Sets the attribute autoclose
59 60 61 |
# File 'lib/memfs/io.rb', line 59 def autoclose=(value) @autoclose = value end |
#close_on_exec=(value) ⇒ Object (writeonly)
Sets the attribute close_on_exec
59 60 61 |
# File 'lib/memfs/io.rb', line 59 def close_on_exec=(value) @close_on_exec = value end |
Class Method Details
.read(path, *args) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/memfs/io.rb', line 18 def self.read(path, *args) = args.last.is_a?(Hash) ? args.pop : {} = { encoding: nil, mode: File::RDONLY, open_args: nil }.merge() open_args = [:open_args] || [[:mode], { encoding: [:encoding] }] length, offset = args file = open(path, *open_args) file.seek(offset || 0) file.read(length) ensure file&.close end |
.write(path, string, offset = 0, open_args = nil) ⇒ Object
rubocop:disable Metrics/MethodLength
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/memfs/io.rb', line 33 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.positive? 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 end |
Instance Method Details
#<<(object) ⇒ Object
62 63 64 65 66 |
# File 'lib/memfs/io.rb', line 62 def <<(object) fail IOError, 'not opened for writing' unless writable? content << object.to_s end |
#advise(advice_type, _offset = 0, _len = 0) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/memfs/io.rb', line 68 def advise(advice_type, _offset = 0, _len = 0) advice_types = i[dontneed noreuse normal random sequential willneed] return if advice_types.include?(advice_type) fail NotImplementedError, "Unsupported advice: #{advice_type.inspect}" end |
#autoclose? ⇒ Boolean
76 77 78 |
# File 'lib/memfs/io.rb', line 76 def autoclose? defined?(@autoclose) ? !!@autoclose : true end |
#binmode ⇒ Object
80 81 82 83 84 |
# File 'lib/memfs/io.rb', line 80 def binmode @binmode = true @external_encoding = Encoding::ASCII_8BIT self end |
#binmode? ⇒ Boolean
86 87 88 |
# File 'lib/memfs/io.rb', line 86 def binmode? defined?(@binmode) ? @binmode : false end |
#close ⇒ Object
90 91 92 |
# File 'lib/memfs/io.rb', line 90 def close self.closed = true end |
#close_on_exec? ⇒ Boolean
98 99 100 |
# File 'lib/memfs/io.rb', line 98 def close_on_exec? defined?(@close_on_exec) ? !!@close_on_exec : true end |
#closed? ⇒ Boolean
94 95 96 |
# File 'lib/memfs/io.rb', line 94 def closed? closed end |
#each(sep = $/, &block) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/memfs/io.rb', line 115 def each(sep = $/, &block) return to_enum(__callee__, sep) unless block_given? fail IOError, 'not opened for reading' unless readable? content.each_line(sep, &block) self end |
#each_byte(&block) ⇒ Object Also known as: bytes
122 123 124 125 126 127 |
# File 'lib/memfs/io.rb', line 122 def each_byte(&block) return to_enum(__callee__) unless block_given? fail IOError, 'not opened for reading' unless readable? content.each_byte(&block) self end |
#each_char(&block) ⇒ Object Also known as: chars
130 131 132 133 134 135 |
# File 'lib/memfs/io.rb', line 130 def each_char(&block) return to_enum(__callee__) unless block_given? fail IOError, 'not opened for reading' unless readable? content.each_char(&block) self end |
#eof? ⇒ Boolean Also known as: eof
102 103 104 |
# File 'lib/memfs/io.rb', line 102 def eof? pos >= content.size end |
#external_encoding ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/memfs/io.rb', line 107 def external_encoding if writable? @external_encoding else @external_encoding ||= Encoding.default_external end end |
#fileno ⇒ Object
138 139 140 |
# File 'lib/memfs/io.rb', line 138 def fileno entry.fileno end |
#pos ⇒ Object
142 143 144 |
# File 'lib/memfs/io.rb', line 142 def pos entry.pos end |
#print(*objs) ⇒ Object
146 147 148 149 150 |
# File 'lib/memfs/io.rb', line 146 def print(*objs) objs << $_ if objs.empty? self << objs.join($,) << $\.to_s nil end |
#printf(format_string, *objs) ⇒ Object
152 153 154 |
# File 'lib/memfs/io.rb', line 152 def printf(format_string, *objs) print format_string % objs end |
#puts(text) ⇒ Object
156 157 158 159 160 |
# File 'lib/memfs/io.rb', line 156 def puts(text) fail IOError, 'not opened for writing' unless writable? content.puts text end |
#read(length = nil, buffer = +'')) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/memfs/io.rb', line 162 def read(length = nil, buffer = +'') fail(Errno::ENOENT, path) unless entry default = length ? nil : '' content.read(length, buffer) || default end |
#seek(amount, whence = ::IO::SEEK_SET) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/memfs/io.rb', line 169 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.negative? entry.pos = new_pos 0 end |
#stat ⇒ Object
183 184 185 |
# File 'lib/memfs/io.rb', line 183 def stat File.stat(path) end |
#write(string) ⇒ Object
187 188 189 190 191 |
# File 'lib/memfs/io.rb', line 187 def write(string) fail(IOError, 'not opened for writing') unless writable? content.write(string.to_s) end |