Module: Sequel::Plugins::Protobuf::Drivers::RubyProtocolBuffers

Defined in:
lib/sequel/plugins/protobuf/drivers/ruby-protocol-buffers-driver.rb

Overview

This driver definition provides a standard interface for protocol buffer serialization with the ruby-protocol-buffers gem.

Class Method Summary collapse

Class Method Details

.create(protobuf_model, attributes, options = {}) ⇒ ::ProtocolBuffers::Message

Creates and returns a new instance of the specified protobuf model initialized with the passed in attributes and configured by the passed in optoins.

Parameters:

  • protobuf_model (Object)

    . The protobuf model in which to render.

  • attributes (Hash)

    . A hash of attributes in which to create the new protobuf model

  • options (Hash) (defaults to: {})

    . A configuration hash in which to configure the creation of the New protocol buffer object.

Returns:

  • (::ProtocolBuffers::Message)

    . A newly created protocol buffer message.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sequel/plugins/protobuf/drivers/ruby-protocol-buffers-driver.rb', line 41

def self.create(protobuf_model, attributes, options = {})
  fields = protobuf_model.fields.inject([]) do |acc, (k, v)| 
    acc << v.name
    acc
  end
  
  attributes = attributes.inject({}) do |acc, (k, v)| 
    if fields.include?(k)
      acc[k] = v
    end

    acc
  end
  
  return protobuf_model.send(:new, attributes)

end

.parse(protobuf_model, protobuf, options = {}) ⇒ Object

Parses the passed in protobuf message into an instance of the corresponding protocol buffer model definition

Parameters:

  • protobuf_model (Object)

    . The class of the protocol buffer definition

  • protobuf (String)

    . The protocol buffer message to be parsed

  • options (Hash) (defaults to: {})

    . An options hash to help configure the parsing functionality.



18
19
20
# File 'lib/sequel/plugins/protobuf/drivers/ruby-protocol-buffers-driver.rb', line 18

def self.parse(protobuf_model, protobuf, options = {})
  return protobuf_model.send(:parse, protobuf)
end

.serialize(protobuf_model, attributes, options = {}) ⇒ ::ProtocolBuffers::Message

Serializes the passed in attributes hash into an instance of the passed in protocol buffer model definition.

Parameters:

  • protobuf_model (Object)

    . The class of the protocol buffer definition

  • attributes (Hash)

    . The attributes in which to be serialized into a protocol buffer message

  • options (Hash) (defaults to: {})

    . An options hash to help configure the serialization funcitonality.

Returns:

  • (::ProtocolBuffers::Message)

    . A newly created protocol buffer message



29
30
31
# File 'lib/sequel/plugins/protobuf/drivers/ruby-protocol-buffers-driver.rb', line 29

def self.serialize(protobuf_model, attributes, options = {})
  return self.create(protobuf_model, attributes, options).to_s
end