Module: Protobuf::Mongoid::Serialization

Extended by:
ActiveSupport::Concern
Defined in:
lib/protobuf/mongoid/serialization.rb

Overview

Serialization methods

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

OK =
200
BAD_REQUEST =
400

Instance Method Summary collapse

Instance Method Details

#_filter_field_attributes(options = {}) ⇒ Object



194
195
196
197
198
199
200
201
202
# File 'lib/protobuf/mongoid/serialization.rb', line 194

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



204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/protobuf/mongoid/serialization.rb', line 204

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

  fields = []
  fields.concat(self.class._protobuf_message_non_deprecated_fields)
  fields.concat(self.class._protobuf_message_deprecated_fields) if include_deprecated
  fields.concat([options[:include]].flatten) if options[:include].present?
  fields.compact!
  fields.uniq!

  fields
end

#_is_collection_association?(field) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
220
221
222
# File 'lib/protobuf/mongoid/serialization.rb', line 217

def _is_collection_association?(field)
  reflection = self.class.relations[field.to_s]
  return false unless reflection

  reflection.class.name.split('::').last.to_sym == :has_many
end

#_normalize_options(options) ⇒ Object



224
225
226
227
228
229
230
# File 'lib/protobuf/mongoid/serialization.rb', line 224

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

#_protobuf_field_objects(field) ⇒ Object

TODO: Assignment Branch Condition size for _protobuf_field_objects is too high. [<1, 19, 7> 20.27/17]



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/protobuf/mongoid/serialization.rb', line 284

def _protobuf_field_objects(field)
  self.class._protobuf_field_objects[field] ||=
    if _protobuf_field_symbol_transformers.key?(field)
      self.class._protobuf_symbol_transformer_object(field)
    elsif _protobuf_field_transformers.key?(field)
      self.class._protobuf_field_transformer_object(field)
    elsif respond_to?(field)
      if _is_collection_association?(field)
        self.class._protobuf_collection_association_object(field)
      else
        self.class._protobuf_convert_to_fields_object(field)
      end
    else
      self.class._protobuf_nil_object(field)
    end
end

#_protobuf_field_symbol_transformersObject



301
302
303
# File 'lib/protobuf/mongoid/serialization.rb', line 301

def _protobuf_field_symbol_transformers
  self.class._protobuf_field_symbol_transformers
end

#_protobuf_field_transformersObject



305
306
307
# File 'lib/protobuf/mongoid/serialization.rb', line 305

def _protobuf_field_transformers
  self.class._protobuf_field_transformers
end

#_protobuf_messageObject



309
310
311
# File 'lib/protobuf/mongoid/serialization.rb', line 309

def _protobuf_message
  self.class.protobuf_message
end

#changed_fields_from_documentObject



262
263
264
265
266
# File 'lib/protobuf/mongoid/serialization.rb', line 262

def changed_fields_from_document
  changed_attribute_names = previous_changes.keys

  changed_attribute_names
end

#errors_for_protobufObject



272
273
274
275
276
277
278
279
280
281
# File 'lib/protobuf/mongoid/serialization.rb', line 272

def errors_for_protobuf
  return [] if errors.empty?

  errors.messages.map do |field, error_messages|
    {
      :field => field.to_s,
      :messages => error_messages.map(&:to_s) # cast the messages to a string, because in Rails 6.1+ this is a ActiveModel::DeprecationHandlingMessageArray
    }
  end
end

#fields_from_document(options = {}) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/protobuf/mongoid/serialization.rb', line 232

def fields_from_document(options = {})
  hash = {}
  field_attributes = _filter_field_attributes(options)

  if options[:include].present?
    field_attributes.concat([options[:include]].flatten)
    field_attributes.compact!
    field_attributes.uniq!
  end

  field_attributes.each do |field|
    field_object = _protobuf_field_objects(field)
    hash[field] = field_object.call(self)
  end

  if hash.include?(:changed_attribute_names)
    hash[:changed_attribute_names] = changed_fields_from_document
  end

  if hash.include?(:errors)
    hash[:errors] = errors_for_protobuf
  end

  if hash.include?(:status_code)
    hash[:status_code] = status_code_for_protobuf
  end

  hash
end

#status_code_for_protobufObject



268
269
270
# File 'lib/protobuf/mongoid/serialization.rb', line 268

def status_code_for_protobuf
  errors.empty? ? OK : BAD_REQUEST
end

#to_proto(options = {}) ⇒ Object

Raises:



313
314
315
316
317
318
# File 'lib/protobuf/mongoid/serialization.rb', line 313

def to_proto(options = {})
  raise MessageNotDefined, self.class if _protobuf_message.nil?

  fields = fields_from_document(options)
  _protobuf_message.new(fields)
end