Sha256: 76a611531785dda72fe486f97d4908b87ee90f2fcdde054889439757ba010d13

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

##
# Midori Extension of File reading and writing
module Midori
  module Contrib
    class File
      # Init File object
      # @param [Array] args same args like File.new
      def initialize(*args)
        @file = ::File.new(*args)
      end

      # read file
      # @return [String] string readed
      def read
        await(Promise.new do |resolve|
          data = ''
          EventLoop.register(@file, :r) do
            if @file.eof?
              EventLoop.deregister(@file)
              resolve.call(data)
            else
              data << @file.read_nonblock(16384)
            end
          end
        end)
      end

      # write file
      # @param [String] data string to be written
      def write(data)
        await(Promise.new do |resolve|
          written = 0
          EventLoop.register(@file, :w) do
            written += @file.write_nonblock(data[written..-1])
            if written == data.size
              EventLoop.deregister(@file)
              resolve.call(written)
            end
          end
        end)
      end

      # raw file object
      # @return [File] file
      def raw
        @file
      end

      # Close the file
      def close
        @file.close
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
midori-contrib-0.1.3 lib/midori-contrib/file.rb
midori-contrib-0.1.2 lib/midori-contrib/file.rb
midori-contrib-0.1.1 lib/midori-contrib/file.rb
midori-contrib-0.1.0 lib/midori-contrib/file.rb