Class: Celluloid::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/celluloid/registry.rb

Overview

The Registry allows us to refer to specific actors by human-meaningful names

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.


6
7
8
9
# File 'lib/celluloid/registry.rb', line 6

def initialize
  @registry = {}
  @registry_lock = Mutex.new
end

Instance Method Details

#[](name) ⇒ Object Also known as: get

Retrieve an actor by name


26
27
28
29
30
# File 'lib/celluloid/registry.rb', line 26

def [](name)
  @registry_lock.synchronize do
    @registry[name.to_sym]
  end
end

#[]=(name, actor) ⇒ Object Also known as: set

Register an Actor


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/celluloid/registry.rb', line 12

def []=(name, actor)
  actor_singleton = class << actor; self; end
  unless actor_singleton.ancestors.include? AbstractProxy
    raise TypeError, "not an actor"
  end

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

  actor.mailbox << NamingRequest.new(name.to_sym)
end

#clearObject

removes and returns all registered actors as a hash of `name => actor` can be used in testing to clear the registry


48
49
50
51
52
53
54
55
# File 'lib/celluloid/registry.rb', line 48

def clear
  hash = nil
  @registry_lock.synchronize do
    hash = @registry.dup
    @registry.clear
  end
  hash
end

#delete(name) ⇒ Object


35
36
37
38
39
# File 'lib/celluloid/registry.rb', line 35

def delete(name)
  @registry_lock.synchronize do
    @registry.delete name.to_sym
  end
end

#namesObject

List all registered actors by name


42
43
44
# File 'lib/celluloid/registry.rb', line 42

def names
  @registry_lock.synchronize { @registry.keys }
end