Module: Protoable::Serialization

Defined in:
lib/protobuf/activerecord/protoable/serialization.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/protobuf/activerecord/protoable/serialization.rb', line 5

def self.included(klass)
  klass.extend Protoable::Serialization::ClassMethods
  klass.__send__(:include, ::Heredity::InheritableClassInstanceVariables)

  klass.class_eval do
    class << self
      attr_accessor :_protobuf_attribute_converters,
        :_protobuf_field_transformers, :protobuf_fields
    end

    @_protobuf_attribute_converters = {}
    @_protobuf_field_transformers = {}
    @protobuf_fields = []

    inheritable_attributes :_protobuf_attribute_converters,
      :_protobuf_field_transformers, :protobuf_fields, :protobuf_message
  end
end

Instance Method Details

#protoable_attributesObject

Extracts attributes that correspond to fields on the specified protobuf message, performing any necessary column conversions on them.



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/protobuf/activerecord/protoable/serialization.rb', line 130

def protoable_attributes
  protoable_attributes = protobuf_fields.inject({}) do |hash, field|
    if _protobuf_field_transformers.has_key?(field)
      hash[field] = _protobuf_field_transformers[field].call(self)
    else
      value = respond_to?(field) ? __send__(field) : nil
      hash[field] = _protobuf_convert_attributes_to_fields(field, value)
    end
    hash
  end

  protoable_attributes
end