Revision: 3401 http://trac.macosforge.org/projects/ruby/changeset/3401 Author: ernest.prabhakar@gmail.com Date: 2010-02-01 17:35:36 -0800 (Mon, 01 Feb 2010) Log Message: ----------- Rewrote Actor on top of SimpleDelegator Modified Paths: -------------- MacRuby/trunk/lib/dispatch/actor.rb MacRuby/trunk/spec/macruby/library/dispatch/actor_spec.rb Modified: MacRuby/trunk/lib/dispatch/actor.rb =================================================================== --- MacRuby/trunk/lib/dispatch/actor.rb 2010-02-02 01:35:32 UTC (rev 3400) +++ MacRuby/trunk/lib/dispatch/actor.rb 2010-02-02 01:35:36 UTC (rev 3401) @@ -1,57 +1,57 @@ +require 'delegate' + module Dispatch - # Create an Actor that serializes or asynchronizes access to an object + # Create an Actor that serializes or asynchronizes access to a delegate. # Forwards method invocations to the passed object via a private serial queue, - # and optinally calls back asynchronously (if given a block or group). - # Note that this will NOT work for methods that themselves expect a block - class Actor + # and optionally calls back asynchronously (if given a block or group). + # + # Note that this will NOT work for methods that themselves expect a block. + # For those, use e.g., the Enumerable p_* methods insteads. + # + class Actor < SimpleDelegator - CRITICAL = /__(.+)__|method_missing|object_id/ - instance_methods.each do |method| - undef_method(method) unless method =~ CRITICAL - end - - # Create an Actor to wrap the given +actee+, + # Create an Actor to wrap the given +delegate+, # optionally specifying the default +callback+ queue - def initialize(actee, callback=nil) - @actee = actee - @callback_default = callback || Dispatch::Queue.concurrent - @q = Dispatch::Queue.new("dispatch.actor.#{actee}.#{object_id}") + def initialize(delegate, callback=nil) + super(delegate) + @default_callback = callback || Dispatch::Queue.concurrent + @q = Dispatch::Queue.new("dispatch.actor.#{delegate}.#{object_id}") __reset!__ end def __reset!__ - @callback = @callback_default + @callback = @default_callback @group = nil end # Specify the +callback+ queue for the next async request - def _on(callback) + def _on_(callback) @callback = callback end # Specify the +group+ for the next async request - def _with(group) + def _with_(group) @group = group end # Wait until the internal private queue has completed execution - # then returns the +actee+ delegate object - def _done + # then returns the +delegate+ object + def _done_ @q.sync { } - @actee + __getobj__ end def method_missing(symbol, *args, &block) if block_given? or not @group.nil? callback = @callback @q.async(@group) do - retval = @actee.__send__(symbol, *args) + retval = __getobj__.__send__(symbol, *args) callback.async { block.call(retval) } if not callback.nil? end return __reset!__ else @retval = nil - @q.sync { @retval = @actee.__send__(symbol, *args) } + @q.sync { @retval = __getobj__.__send__(symbol, *args) } return @retval end end Modified: MacRuby/trunk/spec/macruby/library/dispatch/actor_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/library/dispatch/actor_spec.rb 2010-02-02 01:35:32 UTC (rev 3400) +++ MacRuby/trunk/spec/macruby/library/dispatch/actor_spec.rb 2010-02-02 01:35:36 UTC (rev 3401) @@ -2,22 +2,31 @@ require 'dispatch' if MACOSX_VERSION >= 10.6 + + class Actee + def initialize(s); @s = s; end + def current_queue; Dispatch::Queue.current; end + def to_s; @s; end + end + describe "Dispatch::Actor" do before :each do - @actee = "me" + @actee = Actee.new("my actee") @actor = Dispatch::Actor.new(@actee) end it "should return an Actor when called with an actee" do - true.should == true + @actor.should be_kind_of(Dispatch::Actor) + @actor.should be_kind_of(SimpleDelegator) end - it "should undef most of its inherited methods" do - true.should == true + it "should return the actee when called with __getobj__" do + @actee.should be_kind_of(Actee) + @actor.__getobj__.should be_kind_of(Actee) end - it "should NOT undef missing_method or object-id" do - true.should == true + it "should invoke actee for most inherited methods" do + @actor.to_s.should == @actee.to_s end it "should invoke actee methods on a private serial queue" do
participants (1)
-
source_changes@macosforge.org