Sha256: fb4ee93949e22fba5bb55573174dfe290032b2fd9bd878244ed34c933d91565e

Contents?: true

Size: 659 Bytes

Versions: 7

Compression:

Stored size: 659 Bytes

Contents

# frozen_string_literal: true

require 'stringio'

module NanoBot
  module Components
    class Stream < StringIO
      def write(*args)
        if @callback
          @accumulated += args.first
          @callback.call(@accumulated, args.first, false)
        end
        super
      end

      def callback=(block)
        @accumulated = ''
        @callback = block
      end

      def finish
        flush
        result = string.clone
        truncate(0)
        rewind

        if @callback
          @callback.call(@accumulated, nil, true)
          @callback = nil
          @accumulated = nil
        end

        result
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
nano-bots-0.1.1 components/stream.rb
nano-bots-0.1.0 components/stream.rb
nano-bots-0.0.10 components/stream.rb
nano-bots-0.0.9 components/stream.rb
nano-bots-0.0.8 components/stream.rb
nano-bots-0.0.7 components/stream.rb
nano-bots-0.0.6 components/stream.rb