Class: XRB::IOBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/xrb/buffer.rb

Overview

Represents an IO buffer with a given path.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, path: io.inspect) ⇒ IOBuffer

Create a new IO buffer from an IO object.



102
103
104
105
106
# File 'lib/xrb/buffer.rb', line 102

def initialize(io, path: io.inspect)
  @io = io
  @path = path
  @cache = nil
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



118
119
120
# File 'lib/xrb/buffer.rb', line 118

def path
  @path
end

#the path name of the buffer.(pathnameofthebuffer.) ⇒ Object (readonly)



118
# File 'lib/xrb/buffer.rb', line 118

attr :path

Instance Method Details

#encodingObject



121
122
123
# File 'lib/xrb/buffer.rb', line 121

def encoding
  read.encoding
end

#freezeObject

Freeze the buffer, caching the contents of the IO object.



109
110
111
112
113
114
115
# File 'lib/xrb/buffer.rb', line 109

def freeze
  return self if frozen?
  
  read
  
  super
end

#readObject

Read the contents of the IO object into the buffer.



126
127
128
129
130
131
132
133
# File 'lib/xrb/buffer.rb', line 126

def read
  unless @cache
    @cache = @io.read.freeze
    @io.close
  end
  
  return @cache
end

#to_bufferObject



136
137
138
# File 'lib/xrb/buffer.rb', line 136

def to_buffer
  Buffer.new(self.read, path: @path)
end