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.



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



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.



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