class Utilrb::EventLoop::Forwardable::Forward

Public Class Methods

def_event_loop_delegator(klass,accessor,event_loop, method, options = Hash.new ) click to toggle source
# File lib/utilrb/event_loop.rb, line 874
def self.def_event_loop_delegator(klass,accessor,event_loop, method, options = Hash.new )
    options = Kernel.validate_options options, :filter => nil,
                                               :alias => method,
                                               :sync_key => :accessor,
                                               :known_errors => nil,
                                               :on_error => nil

    raise ArgumentError, "accessor is nil" unless accessor
    raise ArgumentError, "event_loop is nil" unless event_loop
    raise ArgumentError, "method is nil" unless method

    ali = options[:alias]
    return if klass.instance_methods.include? ali.to_sym

    filter = options[:filter]
    sync_key = options[:sync_key]
    sync_key ||= :nil
    errors = "[#{Array(options[:known_errors]).map(&:name).join(",")}]"
    on_error = options[:on_error]

    line_no = __LINE__; str = %Q{
    def #{ali}(*args, &block)
        accessor,error = #{if options[:known_errors]
                            %Q{
                                begin
                                    #{accessor} # cache the accessor.
                                rescue #{Array(options[:known_errors]).join(",")} => e
                                   [nil,e]
                                end
                               }
                          else
                                accessor.to_s
                          end}
        if !block
            begin
                if !accessor
                    error ||= DesignatedObjectNotFound.new 'designated object is nil'
                    raise error
                else
                    result = #{sync_key != :nil ? "#{event_loop}.sync(#{sync_key}){accessor.__send__(:#{method}, *args)}" : "accessor.__send__(:#{method}, *args)"}
                    #{filter ? "#{filter}(result)" : "result"}
                end
            rescue Exception => error
                #{"#{on_error}(error)" if on_error}
                raise error
            end
        else
            work = Proc.new do |*callback_args|
                    acc,err = #{accessor} # cache accessor
                    if !acc
                        if err
                            raise err
                        else
                            raise DesignatedObjectNotFound,'designated object is nil'
                        end
                    else
                        acc.__send__(:#{method}, *callback_args, &block)
                    end
                end
            callback = #{filter ? "block.to_proc.arity == 2 ? Proc.new { |r,e| block.call(#{filter}(r),e)} : Proc.new {|r| block.call(#{filter}(r))}" : "block"}
            #{event_loop}.async_with_options(work,
                                             {:sync_key => #{sync_key},:known_errors => #{errors},
                                             :on_error => #{ on_error ? "self.method(#{on_error.inspect})" : "nil" }},
                                             *args, &callback)
        end
      rescue Exception
        $@.delete_if{|s| %r"#{Regexp.quote(__FILE__)}"o =~ s}
        ::Kernel::raise
    end
    }
    # If it's not a class or module, it's an instance
    begin
        klass.module_eval(str, __FILE__, line_no)
    rescue
        klass.instance_eval(str, __FILE__, line_no)
    end
end
def_event_loop_delegators(klass,accessor,event_loop, *methods) click to toggle source

Defines multiple method as delegator instance methods @see def_event_loop_delegator

# File lib/utilrb/event_loop.rb, line 954
def self.def_event_loop_delegators(klass,accessor,event_loop, *methods)
    methods.flatten!
    options = if methods.last.is_a? Hash
                  methods.pop
              else
                  Hash.new
              end
    raise ArgumentError, ":alias is not supported when defining multiple methods at once." if options.has_key?(:alias)
    methods.each do |method|
        def_event_loop_delegator(klass,accessor,event_loop,method,options)
    end
end
new(klass,accessor,event_loop,options = Hash.new) click to toggle source
# File lib/utilrb/event_loop.rb, line 839
def initialize(klass,accessor,event_loop,options = Hash.new)
    @klass = klass
    @stack = [options]
    @accessor = accessor
    @event_loop = event_loop
end

Public Instance Methods

def_delegator(method,options = Hash.new) click to toggle source
# File lib/utilrb/event_loop.rb, line 868
def def_delegator(method,options = Hash.new)
    options = @stack.last.merge options
    Forward.def_event_loop_delegator(@klass,@accessor,@event_loop,method,options)
end
def_delegators(*methods) click to toggle source
# File lib/utilrb/event_loop.rb, line 858
def def_delegators(*methods)
    options = if methods.last.is_a? Hash
                  methods.pop
              else
                  Hash.new
              end
    methods << @stack.last.merge(options)
    Forward.def_event_loop_delegators(@klass,@accessor,@event_loop,*methods)
end
options(options = Hash.new,&block) click to toggle source
# File lib/utilrb/event_loop.rb, line 846
def options(options = Hash.new,&block)
    @stack << @stack.last.merge(options)
        block.call
    @stack.pop
end
thread_safe(&block) click to toggle source
# File lib/utilrb/event_loop.rb, line 852
def thread_safe(&block)
    options :sync_key => nil do 
        block.call
    end
end