Sha256: fae851742bc3421acec91a4c50822088b1083a58307443e63eebb13b7e4aee0e

Contents?: true

Size: 1.9 KB

Versions: 24

Compression:

Stored size: 1.9 KB

Contents

require 'stringio'
require 'protobuf/decoder'
require 'protobuf/encoder'

module Protobuf
  class Message
    module Serialization

      module ClassMethods
        def decode(bytes)
          self.new.decode(bytes)
        end

        # Create a new object with the given values and return the encoded bytes.
        def encode(fields = {})
          self.new(fields).encode
        end
      end

      def self.included(other)
        other.extend(ClassMethods)
      end

      ##
      # Instance Methods
      #

      # Decode the given non-stream bytes into this message.
      #
      def decode(bytes)
        decode_from(::StringIO.new(bytes))
      end

      # Decode the given stream into this message.
      #
      def decode_from(stream)
        ::Protobuf::Decoder.decode_each_field(stream) do |tag, bytes|
          set_field_bytes(tag, bytes)
        end

        self
      end

      # Encode this message
      #
      def encode
        stream = ::StringIO.new.set_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
        encode_to(stream).string
      end

      # Encode this message to the given stream.
      #
      def encode_to(stream)
        ::Protobuf::Encoder.encode(self, stream)
      end

      ##
      # Instance Aliases
      #
      alias_method :parse_from_string, :decode
      alias_method :deserialize, :decode
      alias_method :parse_from, :decode_from
      alias_method :deserialize_from, :decode_from
      alias_method :to_s, :encode
      alias_method :bytes, :encode
      alias_method :serialize, :encode
      alias_method :serialize_to_string, :encode
      alias_method :serialize_to, :encode_to

      private

      def field_must_be_serialized?(field)
        field.required? || ! @values[field.name].nil?
      end

      def set_field_bytes(tag, bytes)
        field = self.class.get_field(tag, true)
        field.set(self, bytes) if field
      end

    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
protobuffy-3.6.0 lib/protobuf/message/serialization.rb
protobuffy-3.5.1 lib/protobuf/message/serialization.rb
protobuffy-3.4.0 lib/protobuf/message/serialization.rb
protobuffy-3.3.0 lib/protobuf/message/serialization.rb
protobuffy-3.2.0 lib/protobuf/message/serialization.rb
protobuffy-3.1.0 lib/protobuf/message/serialization.rb
protobuf-3.3.6 lib/protobuf/message/serialization.rb
protobuf-3.3.5 lib/protobuf/message/serialization.rb
protobuf-3.3.4 lib/protobuf/message/serialization.rb
protobuf-3.3.3 lib/protobuf/message/serialization.rb
protobuf-3.3.2 lib/protobuf/message/serialization.rb
protobuf-3.3.1 lib/protobuf/message/serialization.rb
protobuf-3.3.0 lib/protobuf/message/serialization.rb
protobuf-3.2.1 lib/protobuf/message/serialization.rb
protobuf-3.2.0 lib/protobuf/message/serialization.rb
protobuf-3.1.0 lib/protobuf/message/serialization.rb
protobuf-3.0.5 lib/protobuf/message/serialization.rb
protobuf-3.0.4 lib/protobuf/message/serialization.rb
protobuf-3.0.3 lib/protobuf/message/serialization.rb
protobuf-3.0.2 lib/protobuf/message/serialization.rb