Class: Stenotype::EventSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/stenotype/event_serializer.rb

Overview

A class used to serialize a Event upon publishing it to targets

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, uuid_generator: Stenotype.config.uuid_generator) ⇒ EventSerializer

Returns a new instance of EventSerializer.

Examples:

Serializing an event with default UUID generator

event = Stenotype::Event.new(data, attributes, eval_context)
serializer = Stenotype::EventSerializer.new(event)

Serializing an event with custom UUID generator

event = Stenotype::Event.new(data, attributes, eval_context)
serializer = Stenotype::EventSerializer.new(event, uuid_generator: CustomUUIDGen)

Parameters:

  • event (Stenotype::Event)
  • uuid_generator (#uuid) (defaults to: Stenotype.config.uuid_generator)

    an object responding to [#uuid]



23
24
25
26
# File 'lib/stenotype/event_serializer.rb', line 23

def initialize(event, uuid_generator: Stenotype.config.uuid_generator)
  @event = event
  @uuid_generator = uuid_generator
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



9
10
11
# File 'lib/stenotype/event_serializer.rb', line 9

def event
  @event
end

#uuid_generatorObject (readonly)

Returns the value of attribute uuid_generator.



9
10
11
# File 'lib/stenotype/event_serializer.rb', line 9

def uuid_generator
  @uuid_generator
end

Instance Method Details

#serializeHash

Returns A hash representation of the event and its context.

Examples:

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

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

Returns:

  • (Hash)

    A hash representation of the event and its context



43
44
45
46
47
48
49
50
# File 'lib/stenotype/event_serializer.rb', line 43

def serialize
  {
    name: event_name,
    **event_attributes,
    **default_options,
    **eval_context_options,
  }
end