Class: SolidObjects::PayloadBroadcast
- Inherits:
-
Object
- Object
- SolidObjects::PayloadBroadcast
- Defined in:
- lib/solid_objects/payload_broadcast.rb,
sig/generated/lib/solid_objects/payload_broadcast.rbs
Constant Summary collapse
- MAXIMUM_PAYLOAD_BYTES =
1_048_576- REVISION_OBSERVABLE =
"solid_objects.revision"
Instance Attribute Summary collapse
-
#authorization_context ⇒ Object
readonly
Returns the value of attribute authorization_context.
- #name ⇒ Object readonly
-
#snapshot ⇒ Object
readonly
Returns the value of attribute snapshot.
Instance Method Summary collapse
- #authorize! ⇒ void
- #call ⇒ Hash[String, untyped]
-
#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.
-
#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.
-
#initialize(snapshot:, name:, authorization_context:) ⇒ PayloadBroadcast
constructor
A new instance of PayloadBroadcast.
- #rendered_payload(handler) ⇒ Object
Constructor Details
#initialize(snapshot:, name:, authorization_context:) ⇒ PayloadBroadcast
Returns a new instance of PayloadBroadcast.
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 = end |
Instance Attribute Details
#authorization_context ⇒ Object (readonly)
Returns the value of attribute authorization_context.
39 40 41 |
# File 'lib/solid_objects/payload_broadcast.rb', line 39 def @authorization_context end |
#name ⇒ Object (readonly)
12 13 14 |
# File 'lib/solid_objects/payload_broadcast.rb', line 12 def name @name end |
#snapshot ⇒ Object (readonly)
Returns the value of attribute snapshot.
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.
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/solid_objects/payload_broadcast.rb', line 82 def = SolidObjects.configuration..call( actor_type: snapshot.reference.actor_type, actor_id: snapshot.reference.actor_id, message_name: name, arguments: {}, authorization_context: ) return if raise Unauthorized, "actor payload broadcast is not authorized" end |
#call ⇒ 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 { "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.
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.
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, , &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
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 |