Class: Celluloid::Internals::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/celluloid/internals/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.


5
6
7
8
9
10
11
# File 'lib/celluloid/internals/registry.rb', line 5

def initialize
  @root = nil     # keep root out of the standard list of registered names
  @actors = {}    # hash of name => actor
  @index = {}     # hash of name => branch
  @branches = {}  # hash of branch => [ actors ]
  @registry = Mutex.new
end

Instance Method Details

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

Retrieve an actor by name


53
54
55
56
57
58
# File 'lib/celluloid/internals/registry.rb', line 53

def [](name)
  return @root if name == :root
  @registry.synchronize do
    @actors[name.to_sym]
  end
end

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

Register an Actor


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/celluloid/internals/registry.rb', line 14

def []=(name, actor)
  if name == :root
    @registry.synchronize do
      @root = actor
    end
  else
    actor_singleton = class << actor; self; end
    raise TypeError, "not an actor" unless actor_singleton.ancestors.include? Proxy::Abstract

    #           if actor.class.ancestors.include? Supervision::Container
    #             puts "Supervisor: #{actor.links.inspect}"
    #           end
    @registry.synchronize do
      @actors[name.to_sym] = actor
    end
    actor.mailbox << NamingRequest.new(name.to_sym)
  end
end

#add(name, actor, branch = :services) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/celluloid/internals/registry.rb', line 33

def add(name, actor, branch = :services)
  set(name, actor)
  @registry.synchronize do
    unless @branches.key? branch
      @branches[branch] = []
      self.class.instance_eval do
        begin
          remove_method(branch)
        rescue
          nil
        end
        define_method(branch) { @branches[branch] }
      end
      @branches[branch] << name
    end
    @index[name.to_sym] = branch
  end
end

#branch(name) ⇒ Object


60
61
62
63
64
# File 'lib/celluloid/internals/registry.rb', line 60

def branch(name)
  @registry.synchronize do
    @index.select { |_a, b| b == name }
  end
end

#clearObject

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


91
92
93
94
95
96
97
98
99
# File 'lib/celluloid/internals/registry.rb', line 91

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

#delete(name) ⇒ Object


69
70
71
72
73
74
# File 'lib/celluloid/internals/registry.rb', line 69

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

#include?(name) ⇒ Boolean

Returns:

  • (Boolean)

76
77
78
# File 'lib/celluloid/internals/registry.rb', line 76

def include?(name)
  names.include? name
end

#indexObject


85
86
87
# File 'lib/celluloid/internals/registry.rb', line 85

def index
  @registry.synchronize { @index }
end

#namesObject

List all registered actors by name


81
82
83
# File 'lib/celluloid/internals/registry.rb', line 81

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