class CTI::Builder::StreamBuilder

Version

$Id: StreamBuilder.rb 902 2013-04-23 05:07:04Z miyabe $

ストリームに対して結果を構築するオブジェクトです。

Public Instance Methods

add_block() click to toggle source
# File CTI/Builder/StreamBuilder.rb, line 156
def add_block
  id = @frgs.size
  frg = Fragment.new(id)
  @frgs[id] = frg
  unless @first
    @first = frg
  else
    @last.nxt = frg
    frg.prev = @last
  end
  @last = frg
end
close_block(id) click to toggle source
# File CTI/Builder/StreamBuilder.rb, line 187
def close_block(id)
end
dispose() click to toggle source
# File CTI/Builder/StreamBuilder.rb, line 207
def dispose
end
finish() click to toggle source
# File CTI/Builder/StreamBuilder.rb, line 194
def finish
  begin
    @finish.call(@length) if @finish
    frg = @first
    while frg
      frg.flush(@tempFile, @out)
      frg = frg.nxt
    end
  ensure
    @tempFile.close
  end
end
insert_block_before(anchor_id) click to toggle source
# File CTI/Builder/StreamBuilder.rb, line 169
def insert_block_before(anchor_id)
  id = @frgs.size
  anchor = @frgs[anchor_id]
  frg = Fragment.new(id)
  @frgs[id] = frg
  frg.prev = anchor.prev
  frg.nxt = anchor
  anchor.prev.nxt = frg
  anchor.prev = frg
  @first = frg if @first.id == anchor.id
end
serial_write(data) click to toggle source
# File CTI/Builder/StreamBuilder.rb, line 190
def serial_write(data)
  @out.write(data)
end
write(id, data) click to toggle source
# File CTI/Builder/StreamBuilder.rb, line 181
def write(id, data)
  frg = @frgs[id]
  @onMemory, @segment, len = frg.write(@tempFile, @onMemory, @segment, data)
  @length += len
end

Public Class Methods

new(out, &finish) click to toggle source

結果構築オブジェクトを作成します。

out

出力先ストリーム

&finish

結果を実際に出力する前に呼び出されるブロック。引数として結果のバイト数が渡されます

例:

SingleResult.new(StreamBuilder.new($stdout) do |length|
  print "Content-Length: #{length}\r\n\r\n"
end) do |opts|
  print "Content-Type: #{opts['mime_type']}\r\n"
end
# File CTI/Builder/StreamBuilder.rb, line 144
def initialize(out, &finish)
  @tempFile = Tempfile.open(['cti', '.tmp'])
  @out = out
  @finish = finish
  @frgs = []
  @first = nil
  @last = nil
  @onMemory = 0
  @length = 0
  @segment = 0
end