Class: Utilrb::EventLoop::Forwardable::Forward

Inherits:
Object
  • Object
show all
Defined in:
lib/utilrb/event_loop.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, accessor, event_loop, options = Hash.new) ⇒ Forward

Returns a new instance of Forward


839
840
841
842
843
844
# 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

Class Method Details

.def_event_loop_delegator(klass, accessor, event_loop, method, options = Hash.new) ⇒ Object

Raises:

  • (ArgumentError)

874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
# 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
        [email protected]_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) ⇒ Object

Defines multiple method as delegator instance methods


954
955
956
957
958
959
960
961
962
963
964
965
# 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

Instance Method Details

#def_delegator(method, options = Hash.new) ⇒ Object


868
869
870
871
# 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) ⇒ Object


858
859
860
861
862
863
864
865
866
# 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) ⇒ Object


846
847
848
849
850
# 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) ⇒ Object


852
853
854
855
856
# File 'lib/utilrb/event_loop.rb', line 852

def thread_safe(&block)
    options :sync_key => nil do 
        block.call
    end
end