Class: EvilEvents::Core::Events::Serializers::MessagePack::Engines::Mpacker Private

Inherits:
Base::AbstractEngine show all
Defined in:
lib/evil_events/plugins/mpacker_engine/mpacker.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base::AbstractEngine

#restore_serialization_state

Constructor Details

#initialize(config) ⇒ Mpacker

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Mpacker.



12
13
14
15
16
# File 'lib/evil_events/plugins/mpacker_engine/mpacker.rb', line 12

def initialize(config)
  configurator = config.settings.options[:mpacker][:configurator]
  raise EvilEvents::SerializationEngineError unless configurator.is_a?(Proc)
  @factory = ::MessagePack::Factory.new.tap { |factory| configurator.call(factory) }
end

Instance Attribute Details

#factory::MessagePack::Factory (readonly, private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (::MessagePack::Factory)

Since:

  • 0.5.0



60
61
62
# File 'lib/evil_events/plugins/mpacker_engine/mpacker.rb', line 60

def factory
  @factory
end

Instance Method Details

#dump(serialization_state) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

  • (String)

Since:

  • 0.5.0



23
24
25
26
27
28
29
30
# File 'lib/evil_events/plugins/mpacker_engine/mpacker.rb', line 23

def dump(serialization_state)
  packer.pack(
    id:       serialization_state.id,
    type:     serialization_state.type,
    payload:  serialization_state.payload,
    metadata: serialization_state.
  ).to_str
end

#load(message) ⇒ EventSerializationState

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • message (String)

Returns:

  • (EventSerializationState)

Raises:

Since:

  • 0.5.0



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/evil_events/plugins/mpacker_engine/mpacker.rb', line 38

def load(message)
  begin
    event_data = unpacker.feed(message).unpack
    raise EvilEvents::SerializationEngineError unless event_data.is_a?(::Hash)
  rescue ::MessagePack::UnpackError, ArgumentError, TypeError
    raise EvilEvents::SerializationEngineError
  end

  restore_serialization_state(
    id:       event_data[:id],
    type:     event_data[:type],
    payload:  event_data[:payload],
    metadata: event_data[:metadata]
  )
end

#packer::MessagePack::Packer (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (::MessagePack::Packer)

Since:

  • 0.5.0



66
67
68
# File 'lib/evil_events/plugins/mpacker_engine/mpacker.rb', line 66

def packer
  factory.packer
end

#unpacker::MessagePack::Unpacker (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (::MessagePack::Unpacker)

Since:

  • 0.5.0



74
75
76
# File 'lib/evil_events/plugins/mpacker_engine/mpacker.rb', line 74

def unpacker
  factory.unpacker(symbolize_keys: true)
end