Class: ActiveRecord::Encryption::MessagePackMessageSerializer
Relationships & Source Files | |
Inherits: | Object |
Defined in: | activerecord/lib/active_record/encryption/message_pack_message_serializer.rb |
Overview
A message serializer that serializes Messages
with ::ActiveRecord::MessagePack
.
The message is converted to a hash with this structure:
{
p: <payload>,
h: {
header1: value1,
header2: value2,
#...
}
}
Then it is converted to the ::ActiveRecord::MessagePack
format.
Instance Attribute Summary
- #binary? ⇒ Boolean readonly
Instance Method Summary
Instance Attribute Details
#binary? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activerecord/lib/active_record/encryption/message_pack_message_serializer.rb', line 34
def binary? true end
Instance Method Details
#dump(message)
# File 'activerecord/lib/active_record/encryption/message_pack_message_serializer.rb', line 22
def dump( ) raise Errors::ForbiddenClass unless .is_a?(Message) ActiveSupport::MessagePack.dump( ( )) end
#hash_to_message(data, level) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_pack_message_serializer.rb', line 52
def (data, level) (data, level) Message.new(payload: data["p"], headers: parse_properties(data["h"], level)) end
#headers_to_hash(headers) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_pack_message_serializer.rb', line 46
def headers_to_hash(headers) headers.transform_values do |value| value.is_a?(Message) ? (value) : value end end
#load(serialized_content)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_pack_message_serializer.rb', line 27
def load(serialized_content) data = ActiveSupport::MessagePack.load(serialized_content) (data, 1) rescue RuntimeError raise Errors::Decryption end
#message_to_hash(message) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_pack_message_serializer.rb', line 39
def ( ) { "p" => .payload, "h" => headers_to_hash( .headers) } end
#parse_properties(headers, level) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_pack_message_serializer.rb', line 67
def parse_properties(headers, level) Properties.new.tap do |properties| headers&.each do |key, value| properties[key] = value.is_a?(Hash) ? (value, level + 1) : value end end end
#validate_message_data_format(data, level) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_pack_message_serializer.rb', line 57
def (data, level) if level > 2 raise Errors::Decryption, "More than one level of hash nesting in headers is not supported" end unless data.is_a?(Hash) && data.has_key?("p") raise Errors::Decryption, "Invalid data format: hash without payload" end end