Class: SolidObjects::ActorRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_objects/actor_registry.rb,
sig/generated/lib/solid_objects/actor_registry.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActorRegistry

Returns a new instance of ActorRegistry.

RBS:

  • () -> void



9
10
11
12
# File 'lib/solid_objects/actor_registry.rb', line 9

def initialize
  @actors = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#actorsObject (readonly)

Returns the value of attribute actors.

Returns:

  • (Object)


49
50
51
# File 'lib/solid_objects/actor_registry.rb', line 49

def actors
  @actors
end

#mutexObject (readonly)

Returns the value of attribute mutex.

Returns:

  • (Object)


49
50
51
# File 'lib/solid_objects/actor_registry.rb', line 49

def mutex
  @mutex
end

Instance Method Details

#fetch(type) ⇒ Class

RBS:

  • (String | Symbol) -> Class

Parameters:

  • (String, Symbol)

Returns:

  • (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

RBS:

  • (String | Symbol) -> String

Parameters:

  • (String, Symbol)

Returns:

  • (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

RBS:

  • (String | Symbol, Class) -> Class

Parameters:

  • (String, Symbol)
  • (Class)

Returns:

  • (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

RBS:

  • (String | Symbol) -> bool

Parameters:

  • (String, Symbol)

Returns:

  • (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

RBS:

  • (Class, Class) -> bool

Parameters:

  • (Class)
  • (Class)

Returns:

  • (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_hHash[String, Class]

RBS:

  • () -> Hash[String, Class]

Returns:

  • (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.

RBS:

  • (untyped) -> void

Parameters:

  • (Object)


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