Class: Celluloid::Links
- Inherits:
-
Object
- Object
- Celluloid::Links
- Includes:
- Enumerable
- Defined in:
- lib/celluloid/links.rb
Overview
Thread safe storage of inter-actor links
Instance Method Summary collapse
-
#<<(actor) ⇒ Object
Add an actor to the current links.
-
#delete(actor) ⇒ Object
Remove an actor from the links.
-
#each ⇒ Object
Iterate through all links.
-
#include?(actor) ⇒ Boolean
Do links include the given actor?.
-
#initialize ⇒ Links
constructor
A new instance of Links.
-
#inspect ⇒ Object
Generate a string representation.
-
#map ⇒ Object
Map across links.
-
#send_event(event) ⇒ Object
Send an event message to all actors.
Constructor Details
#initialize ⇒ Links
Returns a new instance of Links.
8 9 10 11 |
# File 'lib/celluloid/links.rb', line 8 def initialize @links = {} @lock = Mutex.new end |
Instance Method Details
#<<(actor) ⇒ Object
Add an actor to the current links
14 15 16 17 18 19 |
# File 'lib/celluloid/links.rb', line 14 def <<(actor) @lock.synchronize do @links[actor.mailbox.address] = actor end actor end |
#delete(actor) ⇒ Object
Remove an actor from the links
29 30 31 32 33 34 |
# File 'lib/celluloid/links.rb', line 29 def delete(actor) @lock.synchronize do @links.delete actor.mailbox.address end actor end |
#each ⇒ Object
Iterate through all links
37 38 39 40 41 |
# File 'lib/celluloid/links.rb', line 37 def each @lock.synchronize do @links.each { |_, actor| yield(actor) } end end |
#include?(actor) ⇒ Boolean
Do links include the given actor?
22 23 24 25 26 |
# File 'lib/celluloid/links.rb', line 22 def include?(actor) @lock.synchronize do @links.has_key? actor.mailbox.address end end |
#inspect ⇒ Object
Generate a string representation
56 57 58 59 |
# File 'lib/celluloid/links.rb', line 56 def inspect links = self.map(&:inspect).join(',') "#<#{self.class}[#{links}]>" end |
#map ⇒ Object
Map across links
44 45 46 47 48 |
# File 'lib/celluloid/links.rb', line 44 def map result = [] each { |actor| result << yield(actor) } result end |
#send_event(event) ⇒ Object
Send an event message to all actors
51 52 53 |
# File 'lib/celluloid/links.rb', line 51 def send_event(event) each { |actor| actor.mailbox.system_event event } end |