Module: Usps::Support::Lib::ProtoSerialize

Defined in:
lib/usps/support/lib/proto_serialize.rb

Overview

Extension for protobuf message classes to support serializing models data

Usage in model:

serialize :proto, Proto::Message::Name

Usage in extension config / initializer:

Proto::Message::Name.extend(ProtoSerialize)

Instance Method Summary collapse

Instance Method Details

#dump(data) ⇒ Object

Encode



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/usps/support/lib/proto_serialize.rb', line 19

def dump(data)
  case data
  when self
    data.to_proto
  when Hash
    new(data).to_proto
  when String
    decode(data).to_proto # Decode/encode to validate
  else
    raise 'Unexpected data format'
  end
end

#load(data) ⇒ Object

Decode



14
15
16
# File 'lib/usps/support/lib/proto_serialize.rb', line 14

def load(data)
  data ? decode(data) : new
end