class Stenotype::EventSerializer
A class used to serialize a {Stenotype::Event} upon publishing it to targets
Attributes
event[R]
uuid_generator[R]
Public Class Methods
new(event, uuid_generator: Stenotype.config.uuid_generator)
click to toggle source
@example Serializing an event with default UUID generator
event = Stenotype::Event.new(data, attributes, eval_context) serializer = Stenotype::EventSerializer.new(event)
@example Serializing an event with custom UUID generator
event = Stenotype::Event.new(data, attributes, eval_context) serializer = Stenotype::EventSerializer.new(event, uuid_generator: CustomUUIDGen)
@param event {Stenotype::Event} @param uuid_generator
{#uuid} an object responding to [#uuid]
# File lib/stenotype/event_serializer.rb, line 23 def initialize(event, uuid_generator: Stenotype.config.uuid_generator) @event = event @uuid_generator = uuid_generator end
Public Instance Methods
serialize()
click to toggle source
@example Serializing an event with default uuid generator (SecureRandom)
event = Stenotype::Event.new(data, attributes, eval_context) serializer = Stenotype::EventSerializer.new(event) serializer.serialize #=> A hash with event.data, event.options, # default_options and eval_context_options
@example Serializing an event with custom uuid generator
event = Stenotype::Event.new(data, attributes, eval_context) serializer = Stenotype::EventSerializer.new(event, uuid_generator: CustomUUIDGen) serializer.serialize #=> A hash with event.data, event.options, # default_options and eval_context_options
@return {Hash} A hash representation of the event and its context
# File lib/stenotype/event_serializer.rb, line 43 def serialize { name: event_name, **event_attributes, **default_options, **eval_context_options, } end
Private Instance Methods
default_options()
click to toggle source
# File lib/stenotype/event_serializer.rb, line 74 def default_options { timestamp: Time.now.utc, uuid: uuid_generator.uuid, } end
eval_context()
click to toggle source
# File lib/stenotype/event_serializer.rb, line 62 def eval_context event.eval_context end
eval_context_options()
click to toggle source
# File lib/stenotype/event_serializer.rb, line 66 def eval_context_options context_attributes = eval_context.map do |context_name, context| handler = Stenotype::ContextHandlers.known.choose(handler_name: context_name) handler.new(context).as_json end context_attributes.reduce(:merge!) || {} end
event_attributes()
click to toggle source
# File lib/stenotype/event_serializer.rb, line 58 def event_attributes event.attributes end
event_name()
click to toggle source
# File lib/stenotype/event_serializer.rb, line 54 def event_name event.name end