Class: SolidObjects::PayloadBroadcast

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

Constant Summary collapse

MAXIMUM_PAYLOAD_BYTES =

Returns:

  • (::Integer)
1_048_576
REVISION_OBSERVABLE =

Returns:

  • (::String)
"solid_objects.revision"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(snapshot:, name:, authorization_context:) ⇒ PayloadBroadcast

Returns a new instance of PayloadBroadcast.

RBS:

  • (snapshot: ActorSnapshot, name: String, authorization_context: untyped) -> void

Parameters:

  • snapshot: (ActorSnapshot)
  • name: (String)
  • authorization_context: (Object)


15
16
17
18
19
# File 'lib/solid_objects/payload_broadcast.rb', line 15

def initialize(snapshot:, name:, authorization_context:)
  @snapshot = snapshot
  @name = name
  @authorization_context = authorization_context
end

Instance Attribute Details

#authorization_contextObject (readonly)

Returns the value of attribute authorization_context.

Returns:

  • (Object)


39
40
41
# File 'lib/solid_objects/payload_broadcast.rb', line 39

def authorization_context
  @authorization_context
end

#nameObject (readonly)

RBS:

  • @snapshot: ActorSnapshot

  • @name: String

  • @authorization_context: untyped

Returns:

  • (Object)


12
13
14
# File 'lib/solid_objects/payload_broadcast.rb', line 12

def name
  @name
end

#snapshotObject (readonly)

Returns the value of attribute snapshot.

Returns:

  • (Object)


39
40
41
# File 'lib/solid_objects/payload_broadcast.rb', line 39

def snapshot
  @snapshot
end

Instance Method Details

#authorize!void

This method returns an undefined value.

RBS:

  • () -> void



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/solid_objects/payload_broadcast.rb', line 82

def authorize!
  authorized = SolidObjects.configuration.authorize_query.call(
    actor_type: snapshot.reference.actor_type,
    actor_id: snapshot.reference.actor_id,
    message_name: name,
    arguments: {},
    authorization_context:
  )
  return if authorized

  raise Unauthorized, "actor payload broadcast is not authorized"
end

#callHash[String, untyped]

RBS:

  • () -> Hash[String, untyped]

Returns:

  • (Hash[String, untyped])


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/solid_objects/payload_broadcast.rb', line 22

def call
  handler = snapshot.actor_class.definition.payload_broadcasts[name.to_sym]
  raise UnknownPayloadBroadcast, "unknown payload broadcast #{name.inspect}" unless handler

  authorize!
  {
    "actor_type" => snapshot.reference.actor_type,
    "actor_id" => snapshot.reference.actor_id,
    "name" => name,
    "instance_id" => snapshot.instance_id,
    "revision" => snapshot.revision,
    "payload" => rendered_payload(handler)
  }
end

#class_level_receiver?(error) ⇒ Boolean

Distinguishes a block that relied on the old class-level receiver from an ordinary typo, so the one behaviour change reports itself instead of surfacing as an unexplained NameError.

RBS:

  • (NameError[untyped]) -> bool

Parameters:

  • (NameError[untyped])

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/solid_objects/payload_broadcast.rb', line 74

def class_level_receiver?(error)
  error.receiver.equal?(snapshot.actor) &&
    snapshot.actor_class.respond_to?(error.name)
rescue ArgumentError, NameError
  false
end

#evaluated_payload(handler) ⇒ Object

The block runs against the actor instance, like every other block in the actor DSL, and still receives the actor and the resolved authorization context as arguments, so blocks written to the documented signature are unaffected.

RBS:

  • (ActorDefinition::Handler) -> untyped

Parameters:

Returns:

  • (Object)


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/solid_objects/payload_broadcast.rb', line 58

def evaluated_payload(handler)
  actor = snapshot.actor
  actor.instance_exec(actor, authorization_context, &handler.block)
rescue NameError => error
  raise unless class_level_receiver?(error)

  raise InvalidPayloadBroadcast,
    "payload broadcast #{name.inspect} called #{error.name.inspect} on the " \
      "actor class. Payload blocks now run against the actor instance, like " \
      "every other actor block. Call it on the class explicitly."
end

#rendered_payload(handler) ⇒ Object

RBS:

  • (ActorDefinition::Handler) -> untyped

Parameters:

Returns:

  • (Object)


42
43
44
45
46
47
48
49
50
51
# File 'lib/solid_objects/payload_broadcast.rb', line 42

def rendered_payload(handler)
  payload = Serialization.dump(
    evaluated_payload(handler),
    max_bytes: MAXIMUM_PAYLOAD_BYTES
  )
  return payload if payload.is_a?(Hash) || payload.is_a?(Array)

  raise InvalidPayloadBroadcast,
    "payload broadcast #{name.inspect} must return a JSON object or array"
end