Class: SolidObjects::ActorRegistry
- Inherits:
-
Object
- Object
- SolidObjects::ActorRegistry
- Defined in:
- lib/solid_objects/actor_registry.rb,
sig/generated/lib/solid_objects/actor_registry.rbs
Instance Attribute Summary collapse
-
#actors ⇒ Object
readonly
Returns the value of attribute actors.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
Instance Method Summary collapse
- #fetch(type) ⇒ Class
-
#initialize ⇒ ActorRegistry
constructor
A new instance of ActorRegistry.
- #normalize(type) ⇒ String
- #register(type, actor_class) ⇒ Class
- #registered?(type) ⇒ Boolean
- #reload_of?(existing, candidate) ⇒ Boolean
- #to_h ⇒ Hash[String, Class]
- #validate_actor_class!(actor_class) ⇒ void
Constructor Details
#initialize ⇒ ActorRegistry
Returns a new instance of ActorRegistry.
9 10 11 12 |
# File 'lib/solid_objects/actor_registry.rb', line 9 def initialize @actors = {} @mutex = Mutex.new end |
Instance Attribute Details
#actors ⇒ Object (readonly)
Returns the value of attribute actors.
49 50 51 |
# File 'lib/solid_objects/actor_registry.rb', line 49 def actors @actors end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
49 50 51 |
# File 'lib/solid_objects/actor_registry.rb', line 49 def mutex @mutex end |
Instance Method Details
#fetch(type) ⇒ Class
32 33 34 35 |
# File 'lib/solid_objects/actor_registry.rb', line 32 def fetch(type) actor_type = normalize(type) actors.fetch(actor_type) { raise UnknownActorType, "unknown actor type #{actor_type.inspect}" } end |
#normalize(type) ⇒ String
52 53 54 55 56 |
# File 'lib/solid_objects/actor_registry.rb', line 52 def normalize(type) type.to_s.tap do |value| raise InvalidActor, "actor type cannot be empty" if value.empty? end end |
#register(type, actor_class) ⇒ Class
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/solid_objects/actor_registry.rb', line 15 def register(type, actor_class) actor_type = normalize(type) validate_actor_class!(actor_class) mutex.synchronize do existing = actors[actor_type] if existing && existing != actor_class && !reload_of?(existing, actor_class) raise InvalidActor, "#{actor_type.inspect} is already registered by #{existing.name}" end actors[actor_type] = actor_class end actor_class end |
#registered?(type) ⇒ Boolean
38 39 40 |
# File 'lib/solid_objects/actor_registry.rb', line 38 def registered?(type) actors.key?(normalize(type)) end |
#reload_of?(existing, candidate) ⇒ Boolean
66 67 68 |
# File 'lib/solid_objects/actor_registry.rb', line 66 def reload_of?(existing, candidate) !existing.name.nil? && existing.name == candidate.name end |
#to_h ⇒ Hash[String, Class]
43 44 45 |
# File 'lib/solid_objects/actor_registry.rb', line 43 def to_h mutex.synchronize { actors.dup.freeze } end |
#validate_actor_class!(actor_class) ⇒ void
This method returns an undefined value.
59 60 61 62 63 |
# File 'lib/solid_objects/actor_registry.rb', line 59 def validate_actor_class!(actor_class) return if actor_class.is_a?(Class) && actor_class < Actor raise InvalidActor, "registered actor must inherit from SolidObjects::Actor" end |