Class: MailSmtp::PrefixedReader

Inherits:
Object
  • Object
show all
Defined in:
lib/mailsmtp/data_reader.rb

Overview

Prepends a fixed string once, then delegates — used for add_received. Duck-types the same IO surface as DataReader.

Instance Method Summary collapse

Constructor Details

#initialize(prefix, inner) ⇒ PrefixedReader

Returns a new instance of PrefixedReader.



179
180
181
182
# File 'lib/mailsmtp/data_reader.rb', line 179

def initialize(prefix, inner)
  @prefix = prefix.to_s.b
  @inner = inner
end

Instance Method Details

#binmodeObject



234
# File 'lib/mailsmtp/data_reader.rb', line 234

def binmode = self

#closeObject



235
# File 'lib/mailsmtp/data_reader.rb', line 235

def close = @inner.close

#closed?Boolean

Returns:

  • (Boolean)


236
# File 'lib/mailsmtp/data_reader.rb', line 236

def closed? = @inner.closed?

#drainObject



222
223
224
225
# File 'lib/mailsmtp/data_reader.rb', line 222

def drain
  @prefix = nil
  @inner.drain
end

#drain_ceiling_hit?Boolean

Returns:

  • (Boolean)


228
# File 'lib/mailsmtp/data_reader.rb', line 228

def drain_ceiling_hit? = @inner.drain_ceiling_hit?

#eof?Boolean

Returns:

  • (Boolean)


230
231
232
# File 'lib/mailsmtp/data_reader.rb', line 230

def eof?
  (@prefix.nil? || @prefix.empty?) && @inner.eof?
end

#failureObject



227
# File 'lib/mailsmtp/data_reader.rb', line 227

def failure = @inner.failure

#read(length = nil, outbuf = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/mailsmtp/data_reader.rb', line 184

def read(length = nil, outbuf = nil)
  # Match DataReader / IO#read(0).
  if length == 0
    chunk = +""
    return outbuf ? outbuf.replace(chunk) : chunk
  end

  if @prefix
    if length.nil?
      chunk = @prefix
      @prefix = nil
      rest = @inner.read
      chunk = rest ? "#{chunk}#{rest}" : chunk
    elsif length >= @prefix.bytesize
      chunk = @prefix
      @prefix = nil
      rest_len = length - chunk.bytesize
      rest = rest_len.positive? ? @inner.read(rest_len) : +""
      chunk = rest ? "#{chunk}#{rest}" : chunk
    else
      chunk = @prefix.slice!(0, length)
    end
    outbuf ? outbuf.replace(chunk) : chunk
  else
    @inner.read(length, outbuf)
  end
end

#readpartial(maxlen, outbuf = nil) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'lib/mailsmtp/data_reader.rb', line 212

def readpartial(maxlen, outbuf = nil)
  if @prefix && !@prefix.empty?
    chunk = @prefix.slice!(0, maxlen)
    @prefix = nil if @prefix.empty?
    outbuf ? outbuf.replace(chunk) : chunk
  else
    @inner.readpartial(maxlen, outbuf)
  end
end