Class: SolidObjects::Reference

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actor_type:, actor_id:) ⇒ Reference

Returns a new instance of Reference.

RBS:

  • (actor_type: String, actor_id: String) -> void

Parameters:

  • actor_type: (String)
  • actor_id: (String)


11
12
13
14
15
16
17
# File 'lib/solid_objects/reference.rb', line 11

def initialize(actor_type:, actor_id:)
  raise InvalidActor, "actor ID cannot be empty" if actor_id.empty?

  @actor_type = actor_type
  @actor_id = actor_id
  freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, **keywords) ⇒ Object

RBS:

  • (Symbol, *untyped, **untyped) -> untyped

Parameters:

  • (Symbol)
  • (Object)
  • (Object)

Returns:

  • (Object)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/solid_objects/reference.rb', line 76

def method_missing(name, *arguments, **keywords)
  return super if arguments.any? || block_given?
  if actor_message?(name) || actor_query?(name)
    return invoke_synchronously(
      name,
      keywords,
      timeout: 5.seconds,
      idempotency_key: nil,
      authorization_context: nil
    )
  end

  super
end

Instance Attribute Details

#actor_idObject (readonly)

RBS:

  • @actor_type: String

  • @actor_id: String

Returns:

  • (Object)


8
9
10
# File 'lib/solid_objects/reference.rb', line 8

def actor_id
  @actor_id
end

#actor_typeObject (readonly)

RBS:

  • @actor_type: String

  • @actor_id: String

Returns:

  • (Object)


8
9
10
# File 'lib/solid_objects/reference.rb', line 8

def actor_type
  @actor_type
end

Instance Method Details

#actor_definitionActorDefinition

RBS:

  • () -> ActorDefinition

Returns:



128
129
130
# File 'lib/solid_objects/reference.rb', line 128

def actor_definition
  SolidObjects.registry.fetch(actor_type).definition
end

#actor_message?(name) ⇒ Boolean

RBS:

  • (Symbol | String) -> bool

Parameters:

  • (Symbol, String)

Returns:

  • (Boolean)


118
119
120
# File 'lib/solid_objects/reference.rb', line 118

def actor_message?(name)
  actor_definition.messages.key?(name.to_sym)
end

#actor_query?(name) ⇒ Boolean

RBS:

  • (Symbol | String) -> bool

Parameters:

  • (Symbol, String)

Returns:

  • (Boolean)


123
124
125
# File 'lib/solid_objects/reference.rb', line 123

def actor_query?(name)
  actor_definition.queries.key?(name.to_sym)
end

#async(available_at: nil, idempotency_key: nil, authorization_context: nil) ⇒ OperationDispatcher

RBS:

  • (?available_at: Time?, ?idempotency_key: String?, ?authorization_context: untyped) -> OperationDispatcher

Parameters:

  • available_at: (Time, nil) (defaults to: nil)
  • idempotency_key: (String, nil) (defaults to: nil)
  • authorization_context: (Object) (defaults to: nil)

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/solid_objects/reference.rb', line 20

def async(available_at: nil, idempotency_key: nil, authorization_context: nil)
  OperationDispatcher.new(
    actor_type:,
    handlers: actor_definition.messages
  ) do |operation, arguments|
    if (actor = Context.current_actor)
      actor.stage_outbound_message(
        reference: self,
        operation:,
        arguments:,
        available_at:,
        idempotency_key:
      )
      next
    end

    SolidObjects.client.async(
      reference: self,
      operation:,
      arguments:,
      available_at:,
      idempotency_key:,
      authorization_context:
    )
  end
end

#destroy(authorization_context: nil) ⇒ Boolean

RBS:

  • (?authorization_context: untyped) -> bool

Parameters:

  • authorization_context: (Object) (defaults to: nil)

Returns:

  • (Boolean)


66
67
68
# File 'lib/solid_objects/reference.rb', line 66

def destroy(authorization_context: nil)
  SolidObjects.client.destroy(self, authorization_context:)
end

#invoke_synchronously(operation, arguments, timeout:, idempotency_key:, authorization_context:) ⇒ Object

RBS:

  • (Symbol | String, Hash[Symbol, untyped], timeout: Numeric, idempotency_key: String?, authorization_context: untyped) -> untyped

Parameters:

  • (Symbol, String)
  • (Hash[Symbol, untyped])
  • timeout: (Numeric)
  • idempotency_key: (String, nil)
  • authorization_context: (Object)

Returns:

  • (Object)


104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/solid_objects/reference.rb', line 104

def invoke_synchronously(operation, arguments, timeout:, idempotency_key:, authorization_context:)
  raise ActorCallCycle, "actors cannot synchronously wait for another actor" if Context.current_actor

  SolidObjects.client.sync(
    reference: self,
    operation:,
    arguments:,
    timeout:,
    idempotency_key:,
    authorization_context:
  )
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

RBS:

  • (Symbol, bool) -> bool

Parameters:

  • (Symbol)
  • (Boolean)

Returns:

  • (Boolean)


92
93
94
# File 'lib/solid_objects/reference.rb', line 92

def respond_to_missing?(name, include_private = false)
  actor_message?(name) || actor_query?(name) || super
end

#snapshot(authorization_context: nil) ⇒ StateSnapshot

RBS:

  • (?authorization_context: untyped) -> StateSnapshot

Parameters:

  • authorization_context: (Object) (defaults to: nil)

Returns:



71
72
73
# File 'lib/solid_objects/reference.rb', line 71

def snapshot(authorization_context: nil)
  SolidObjects.client.snapshot(self, authorization_context:)
end

#sync(timeout: 5.seconds, idempotency_key: nil, authorization_context: nil) ⇒ OperationDispatcher

RBS:

  • (?timeout: Numeric, ?idempotency_key: String?, ?authorization_context: untyped) -> OperationDispatcher

Parameters:

  • timeout: (Numeric) (defaults to: 5.seconds)
  • idempotency_key: (String, nil) (defaults to: nil)
  • authorization_context: (Object) (defaults to: nil)

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/solid_objects/reference.rb', line 48

def sync(timeout: 5.seconds, idempotency_key: nil, authorization_context: nil)
  raise ActorCallCycle, "actors cannot synchronously wait for another actor" if Context.current_actor

  OperationDispatcher.new(
    actor_type:,
    handlers: actor_definition.messages.merge(actor_definition.queries)
  ) do |operation, arguments|
    invoke_synchronously(
      operation,
      arguments,
      timeout:,
      idempotency_key:,
      authorization_context:
    )
  end
end

#to_sString

RBS:

  • () -> String

Returns:

  • (String)


97
98
99
# File 'lib/solid_objects/reference.rb', line 97

def to_s
  "#{actor_type}(#{actor_id.inspect})"
end