Class: Utilrb::EventLoop::Forwardable::Forward
- Defined in:
- lib/utilrb/event_loop.rb
Class Method Summary collapse
- .def_event_loop_delegator(klass, accessor, event_loop, method, options = Hash.new) ⇒ Object
-
.def_event_loop_delegators(klass, accessor, event_loop, *methods) ⇒ Object
Defines multiple method as delegator instance methods.
Instance Method Summary collapse
- #def_delegator(method, options = Hash.new) ⇒ Object
- #def_delegators(*methods) ⇒ Object
-
#initialize(klass, accessor, event_loop, options = Hash.new) ⇒ Forward
constructor
A new instance of Forward.
- #options(options = Hash.new, &block) ⇒ Object
- #thread_safe(&block) ⇒ Object
Constructor Details
#initialize(klass, accessor, event_loop, options = Hash.new) ⇒ Forward
Returns a new instance of Forward.
841 842 843 844 845 846 |
# File 'lib/utilrb/event_loop.rb', line 841 def initialize(klass,accessor,event_loop, = Hash.new) @klass = klass @stack = [] @accessor = accessor @event_loop = event_loop end |
Class Method Details
.def_event_loop_delegator(klass, accessor, event_loop, method, options = Hash.new) ⇒ Object
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 951 952 |
# File 'lib/utilrb/event_loop.rb', line 876 def self.def_event_loop_delegator(klass,accessor,event_loop, method, = Hash.new ) = Kernel. , :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 = [:alias] return if klass.instance_methods.include? ali.to_sym filter = [:filter] sync_key = [:sync_key] sync_key ||= :nil errors = "[#{Array([:known_errors]).map(&:name).join(",")}]" on_error = [:on_error] line_no = __LINE__; str = %Q{ def #{ali}(*args, &block) accessor,error = #{if [:known_errors] %Q{ begin #{accessor} # cache the accessor. rescue #{Array([: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) ⇒ Object
Defines multiple method as delegator instance methods
956 957 958 959 960 961 962 963 964 965 966 967 |
# File 'lib/utilrb/event_loop.rb', line 956 def self.def_event_loop_delegators(klass,accessor,event_loop, *methods) methods.flatten! = 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 .has_key?(:alias) methods.each do |method| def_event_loop_delegator(klass,accessor,event_loop,method,) end end |
Instance Method Details
#def_delegator(method, options = Hash.new) ⇒ Object
870 871 872 873 |
# File 'lib/utilrb/event_loop.rb', line 870 def def_delegator(method, = Hash.new) = @stack.last.merge Forward.def_event_loop_delegator(@klass,@accessor,@event_loop,method,) end |
#def_delegators(*methods) ⇒ Object
860 861 862 863 864 865 866 867 868 |
# File 'lib/utilrb/event_loop.rb', line 860 def def_delegators(*methods) = if methods.last.is_a? Hash methods.pop else Hash.new end methods << @stack.last.merge() Forward.def_event_loop_delegators(@klass,@accessor,@event_loop,*methods) end |
#options(options = Hash.new, &block) ⇒ Object
848 849 850 851 852 |
# File 'lib/utilrb/event_loop.rb', line 848 def ( = Hash.new,&block) @stack << @stack.last.merge() block.call @stack.pop end |
#thread_safe(&block) ⇒ Object
854 855 856 857 858 |
# File 'lib/utilrb/event_loop.rb', line 854 def thread_safe(&block) :sync_key => nil do block.call end end |