Module: MemFs::IO::InstanceMethods

Included in:
File
Defined in:
lib/memfs/io.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#autoclose=(value) ⇒ Object (writeonly)

Sets the attribute autoclose

Parameters:

  • value

    the value to set the attribute autoclose to.



24
25
26
# File 'lib/memfs/io.rb', line 24

def autoclose=(value)
  @autoclose = value
end

#close_on_exec=(value) ⇒ Object (writeonly)

Sets the attribute close_on_exec

Parameters:

  • value

    the value to set the attribute close_on_exec to.



24
25
26
# File 'lib/memfs/io.rb', line 24

def close_on_exec=(value)
  @close_on_exec = value
end

Instance Method Details

#<<(object) ⇒ Object



27
28
29
30
31
# File 'lib/memfs/io.rb', line 27

def <<(object)
  fail IOError, 'not opened for writing' unless writable?

  content << object.to_s
end

#advise(advice_type, offset = 0, len = 0) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/memfs/io.rb', line 33

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?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/memfs/io.rb', line 48

def autoclose?
  @autoclose.nil? ? true : !!@autoclose
end

#binmodeObject



52
53
54
55
56
# File 'lib/memfs/io.rb', line 52

def binmode
  @binmode = true
  @external_encoding = Encoding::ASCII_8BIT
  self
end

#binmode?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/memfs/io.rb', line 58

def binmode?
  @binmode.nil? ? false : @binmode
end

#closeObject



62
63
64
# File 'lib/memfs/io.rb', line 62

def close
  self.closed = true
end

#close_on_exec?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/memfs/io.rb', line 70

def close_on_exec?
  @close_on_exec.nil? ? true : !!@close_on_exec
end

#closed?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/memfs/io.rb', line 66

def closed?
  closed
end

#each(sep = $/, &block) ⇒ Object



87
88
89
90
91
92
# File 'lib/memfs/io.rb', line 87

def each(sep = $/, &block)
  return to_enum(__callee__) unless block_given?
  fail IOError, 'not opened for reading' unless readable?
  content.each_line(sep) { |line| block.call(line) }
  self
end

#each_byte(&block) ⇒ Object Also known as: bytes



94
95
96
97
98
99
# File 'lib/memfs/io.rb', line 94

def each_byte(&block)
  return to_enum(__callee__) unless block_given?
  fail IOError, 'not opened for reading' unless readable?
  content.each_byte { |byte| block.call(byte) }
  self
end

#each_char(&block) ⇒ Object Also known as: chars



102
103
104
105
106
107
# File 'lib/memfs/io.rb', line 102

def each_char(&block)
  return to_enum(__callee__) unless block_given?
  fail IOError, 'not opened for reading' unless readable?
  content.each_char { |char| block.call(char) }
  self
end

#eof?Boolean Also known as: eof

Returns:

  • (Boolean)


74
75
76
# File 'lib/memfs/io.rb', line 74

def eof?
  pos >= content.size
end

#external_encodingObject



79
80
81
82
83
84
85
# File 'lib/memfs/io.rb', line 79

def external_encoding
  if writable?
    @external_encoding
  else
    @external_encoding ||= Encoding.default_external
  end
end

#posObject



110
111
112
# File 'lib/memfs/io.rb', line 110

def pos
  entry.pos
end


114
115
116
117
118
119
# File 'lib/memfs/io.rb', line 114

def print(*objs)
  $stdout.puts $_.inspect
  objs << $_ if objs.empty?
  self << objs.join($,) << $\.to_s
  nil
end

#printf(format_string, *objs) ⇒ Object



121
122
123
# File 'lib/memfs/io.rb', line 121

def printf(format_string, *objs)
  print format_string % objs
end

#puts(text) ⇒ Object



125
126
127
128
129
# File 'lib/memfs/io.rb', line 125

def puts(text)
  fail IOError, 'not opened for writing' unless writable?

  content.puts text
end

#read(length = nil, buffer = '') ⇒ Object



131
132
133
134
135
136
137
# File 'lib/memfs/io.rb', line 131

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) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/memfs/io.rb', line 139

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

#statObject



152
153
154
# File 'lib/memfs/io.rb', line 152

def stat
  File.stat(path)
end

#write(string) ⇒ Object



156
157
158
159
160
# File 'lib/memfs/io.rb', line 156

def write(string)
  fail IOError, 'not opened for writing' unless writable?

  content.write(string.to_s)
end