Sha256: 161b01c006787ada27430cd21705836c9f1dcdf6d89c99657a6067613365caac

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

# wrap module
# wrap ruby objects, and make it work with lightio

module LightIO::Wrap
  # wrapper for normal ruby objects
  module Wrapper
    # both works in class scope and singleton class scope
    module HelperMethods
      protected
      # run method in thread pool for performance
      def wrap_methods_run_in_threads_pool(*args)
        #TODO
      end
    end

    class << self
      def included(base)
        base.send :extend, HelperMethods
      end
    end
  end

  # wrapper for ruby io objects
  module IOWrapper
    # wrap raw ruby io objects
    #
    # @param [IO, Socket]  io raw ruby io object
    def initialize(*args)
      @obj ||= super
    end

    protected
    # wait io nonblock method
    #
    # @param [Symbol]  method method name, example: wait_nonblock
    # @param [args] args arguments pass to method
    def wait_nonblock(method, *args)
      loop do
        result = __send__(method, *args, exception: false)
        case result
          when :wait_readable
            io_watcher.wait_readable
          when :wait_writable
            io_watcher.wait_writable
          else
            return result
        end
      end
    end

    def io_watcher
      @io_watcher ||= LightIO::Watchers::IO.new(@obj)
    end

    module ClassMethods
      # include Wrapper::ClassMethods
      protected
      # wrap blocking method with "#{method}_nonblock"
      #
      # @param [Symbol]  method method name, example: wait
      def wrap_blocking_method(method)
        define_method method do |*args|
          wait_nonblock(:"#{method}_nonblock", *args)
        end
      end

      def wrap_blocking_methods(*methods)
        methods.each {|m| wrap_blocking_method(m)}
      end
    end

    class << self
      def included(base)
        base.send :extend, ClassMethods
        base.send :include, Wrapper
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lightio-0.4.4 lib/lightio/wrap.rb
lightio-0.4.3 lib/lightio/wrap.rb
lightio-0.4.2 lib/lightio/wrap.rb
lightio-0.4.1 lib/lightio/wrap.rb
lightio-0.4.0 lib/lightio/wrap.rb
lightio-0.4.0.pre lib/lightio/wrap.rb