Module: Sequel::Plugins::Protobuf::InstanceMethods

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

Overview

When mixed in, this module provides various instance-level methods that allow sequel models to be rendered into protocol buffers or rendered into a driver-specific model

Instance Method Summary collapse

Instance Method Details

#as_protobuf(options = {}) ⇒ Object

Renders the current instance of the model to an instance of ProtocolBuffers::Message.

Parameters:

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

    . An options hash that is used to configure how The rendering is performed.

Returns:

  • (Object)

    . A representation of the model as an instance of the protocol_buffer model class configured at the instance level.



104
105
106
# File 'lib/sequel/plugins/protobuf.rb', line 104

def as_protobuf(options = {})
  return self.render(self, options)
end

#render(current, options = {}) ⇒ Object

Renders the passed in key ans the corresponding protocol buffer model. The passed in options specifies how to do this with the following rules:

- `:include`  This key specifies that the current protocol buffer model has a nested
              Protocol buffer model to be rendered inside of it.
- `:coerce`   This key specifies that the current model can override a specific data
              value of a particular database column.  This is useful fo re-formatting
              data from your db schema so that they adhere to your protocol 
              buffer definitions.
- `:as`       This key specifies a different protocol buffer model to render the
              current object.

Parameters:

  • current (Object)

    . The current seciton of the schema to render.

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

    . An options hash to configure how to render the current object.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/sequel/plugins/protobuf.rb', line 121

def render(current, options={})

  # If the current element in the schema is an array, 
  # then we need to render them in sequence and return the result.
  if current.is_a?(Array)
    collection = current.inject([]) do |acc, element|
      acc << render(element, options) 
      acc
    end

    return collection

  # Otherwise, if the record isn't nil, 
  # we get the current values of the object and process them accordingly.
  elsif !current.nil?
    values = current.values.dup
    
    # If the options do not specifiy a protobuf model, assume the one configured earlier
    if options.has_key?(:as) 
      model = options[:as]
    else
      model = current.class.protobuf_model
    end
    
    options.each do |k, v|
      
      # If the current key is "include", then recursively render the model specified
      if k == :include
        v.each do |model, opts|
          values.merge!({model => render(current.send(model.to_sym), opts)})
        end

      # If the current key is "coerce", then call the corresponding proc
      # for the desired attribute
      elsif k == :coerce
        v.each do |value, proc|
          values[value] = proc.call(values[value])
        end
      end
    end
    
    # Finally, return the result of creating a new protobuf model
    return current.class.protobuf_driver.create(model, values)
  end
end

#to_protobuf(options = {}) ⇒ String

Renders the current instance of the model to a protocol buffer message as it is defined in the configured protocol buffer model definition.

Parameters:

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

    . An options hash that is used to configure how the rendering is performed.

Returns:

  • (String)

    . A protocol buffer representation of the current Model instance.



94
95
96
# File 'lib/sequel/plugins/protobuf.rb', line 94

def to_protobuf(options = {})
  return self.render(self, options).to_s
end