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
# 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_field_transformers, :_protobuf_field_options
    end

    @_protobuf_field_transformers = {}
    @_protobuf_field_options = {}

    inheritable_attributes :_protobuf_field_transformers, :_protobuf_field_options,
      :protobuf_message
  end
end

Instance Method Details

#_filter_field_attributes(options = {}) ⇒ Object

:nodoc:



115
116
117
118
119
120
121
122
123
# File 'lib/protobuf/activerecord/protoable/serialization.rb', line 115

def _filter_field_attributes(options = {})
  options = _normalize_options(options)

  fields = _filtered_fields(options)
  fields &= [ options[:only] ].flatten if options[:only].present?
  fields -= [ options[:except] ].flatten if options[:except].present?

  fields
end

#_filtered_fields(options = {}) ⇒ Object

:nodoc:



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/protobuf/activerecord/protoable/serialization.rb', line 126

def _filtered_fields(options = {})
  exclude_deprecated = ! options.fetch(:deprecated, true)

  fields = self.class.protobuf_message.fields.map do |field|
    next if field.nil?
    next if exclude_deprecated && field.deprecated?
    field.name.to_sym
  end
  fields.compact!

  fields
end

#_normalize_options(options) ⇒ Object

:nodoc:



140
141
142
143
144
145
146
# File 'lib/protobuf/activerecord/protoable/serialization.rb', line 140

def _normalize_options(options)
  options ||= {}
  options[:only] ||= [] if options.fetch(:except, false)
  options[:except] ||= [] if options.fetch(:only, false)

  self.class._protobuf_field_options.merge(options)
end

#fields_from_record(options = {}) ⇒ Object

Extracts attributes that correspond to fields on the specified protobuf message, performing any necessary column conversions on them. Accepts a hash of options for specifying which fields should be serialized.

Examples:

fields_from_record(:only => [ :guid, :name ])
fields_from_record(:except => :email_domain)
fields_from_record(:include => :email_domain)
fields_from_record(:except => :email_domain, :deprecated => false)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/protobuf/activerecord/protoable/serialization.rb', line 158

def fields_from_record(options = {})
  field_attributes = _filter_field_attributes(options)
  field_attributes += [ options.fetch(:include, []) ]
  field_attributes.flatten!
  field_attributes.compact!
  field_attributes.uniq!

  field_attributes = field_attributes.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

  field_attributes
end