Module: Sequel::Plugins::Protobuf::ClassMethods

Defined in:
lib/sequel/plugins/protobuf.rb

Overview

When mixed in, this module provides the ability for objects to be serialized from protocol buffer strings

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#protobuf_driverObject (readonly)

Returns the value of attribute protobuf_driver.



61
62
63
# File 'lib/sequel/plugins/protobuf.rb', line 61

def protobuf_driver
  @protobuf_driver
end

#protobuf_modelObject (readonly)

Returns the value of attribute protobuf_model.



61
62
63
# File 'lib/sequel/plugins/protobuf.rb', line 61

def protobuf_model
  @protobuf_model
end

Instance Method Details

#from_protobuf(protobuf, options = {}) ⇒ Object

Creates and returns a new instance of the current class from the passed in protobuf message

Parameters:

  • protobuf (String)

    . The protobuf message to parse.

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

    . An options hash that will configure the parsing.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sequel/plugins/protobuf.rb', line 68

def from_protobuf(protobuf, options = {})
  pb_model = @protobuf_driver.parse(@protobuf_model, protobuf, options)

  # Coerce data from protobuf array to a new model
  model = self.send(:new)
  model.columns.each do |v|
    if pb_model.respond_to?("#{v}=".to_sym)
      model.send("#{v}=", pb_model.send(v.to_sym))
    end
  end
  
  return model
end