Sha256: 6536208dd0dec4c9f09ca4e26a325bbc23ad79813b88e35b65f18448f7931f9e

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

module DeadlySerious
  module Engine

    # Restrict IO class that opens ONLY
    # when trying to read something.
    #
    # *This is the object passed to Components.*
    #
    # Also, used to reopen lost connections.
    #
    # By "restrict", I mean it implements
    # just a few IO operations.
    class LazyIo
      def initialize(channel)
        @channel = channel
      end

      # @return [String, nil] the name of the file or pipe,
      #         nil if it's a socket
      def filename
        @channel.io_name if @channel.respond_to?(:io_name)
      end

      def gets
        open_reader
        @io.gets
      end

      def each(&block)
        open_reader
        @io.each &block
      end

      def each_cons(qty, &block)
        open_reader
        @io.each_cons(qty, &block)
      end

      def each_with_object(object, &block)
        open_reader
        @io.each_with_object(object, &block)
      end

      def <<(element)
        open_writer
        @io << element
      end

      def eof?
        open_reader
        @io.eof?
      end

      def closed?
        @io.nil? || @io.closed?
      end

      def close
        @io.close unless closed?
        @io = nil
      end

      def flush
        @io.flush unless closed?
      end

      private

      def open_reader
        if closed?
          @io = @channel.open_reader
        end
      end

      def open_writer
        if closed?
          @io = @channel.open_writer
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
deadly_serious-1.0.2 lib/deadly_serious/engine/lazy_io.rb
deadly_serious-1.0.1 lib/deadly_serious/engine/lazy_io.rb
deadly_serious-1.0.0 lib/deadly_serious/engine/lazy_io.rb
deadly_serious-1.0.0.rc1 lib/deadly_serious/engine/lazy_io.rb
deadly_serious-1.0.0.pre lib/deadly_serious/engine/lazy_io.rb
deadly_serious-0.12.0 lib/deadly_serious/engine/lazy_io.rb