Skip to content

Commit

Permalink
List all registered actors with Celluloid::Actor.registered
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Nov 30, 2011
1 parent aea3512 commit 4b4adbb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
@@ -1,5 +1,6 @@
0.6.2 0.6.2
----- -----
* List all registered actors with Celluloid::Actor.registered
* All logging now handled through Celluloid::Logger * All logging now handled through Celluloid::Logger
* Rescue DeadActorError in Celluloid::ActorProxy#inspect * Rescue DeadActorError in Celluloid::ActorProxy#inspect


Expand Down
13 changes: 9 additions & 4 deletions lib/celluloid/registry.rb
Expand Up @@ -5,24 +5,29 @@ module Celluloid
module Registry module Registry
@@registry = {} @@registry = {}
@@registry_lock = Mutex.new @@registry_lock = Mutex.new

# Register an Actor # Register an Actor
def []=(name, actor) def []=(name, actor)
actor_singleton = class << actor; self; end actor_singleton = class << actor; self; end
unless actor_singleton.ancestors.include?(Celluloid::ActorProxy) unless actor_singleton.ancestors.include?(Celluloid::ActorProxy)
raise ArgumentError, "not an actor" raise ArgumentError, "not an actor"
end end

@@registry_lock.synchronize do @@registry_lock.synchronize do
@@registry[name.to_sym] = actor @@registry[name.to_sym] = actor
end end
end end

# Retrieve an actor by name # Retrieve an actor by name
def [](name) def [](name)
@@registry_lock.synchronize do @@registry_lock.synchronize do
@@registry[name.to_sym] @@registry[name.to_sym]
end end
end end

# List all registered actors by name
def registered
@@registry_lock.synchronize { @@registry.keys }
end
end end
end end
15 changes: 10 additions & 5 deletions spec/celluloid/registry_spec.rb
Expand Up @@ -3,20 +3,25 @@
describe Celluloid::Registry do describe Celluloid::Registry do
class Marilyn class Marilyn
include Celluloid include Celluloid

def sing_for(person) def sing_for(person)
"o/~ Happy birthday, #{person}" "o/~ Happy birthday, #{person}"
end end
end end

it "registers Actors" do it "registers Actors" do
Celluloid::Actor[:marilyn] = Marilyn.spawn Celluloid::Actor[:marilyn] = Marilyn.new
Celluloid::Actor[:marilyn].sing_for("Mr. President").should == "o/~ Happy birthday, Mr. President" Celluloid::Actor[:marilyn].sing_for("Mr. President").should == "o/~ Happy birthday, Mr. President"
end end

it "refuses to register non-Actors" do it "refuses to register non-Actors" do
expect do expect do
Celluloid::Actor[:impostor] = Object.new Celluloid::Actor[:impostor] = Object.new
end.to raise_error(ArgumentError) end.to raise_error(ArgumentError)
end end
end
it "lists all registered actors" do
Celluloid::Actor[:marilyn] = Marilyn.spawn
Celluloid::Actor.registered.should include :marilyn
end
end

0 comments on commit 4b4adbb

Please sign in to comment.