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
- #_filter_field_attributes(options = {}) ⇒ Object
- #_filtered_fields(options = {}) ⇒ Object
- #_is_collection_association?(field) ⇒ Boolean
- #_normalize_options(options) ⇒ Object
-
#_protobuf_field_objects(field) ⇒ Object
TODO: Assignment Branch Condition size for _protobuf_field_objects is too high.
- #_protobuf_field_symbol_transformers ⇒ Object
- #_protobuf_field_transformers ⇒ Object
- #_protobuf_message ⇒ Object
- #changed_fields_from_document ⇒ Object
- #errors_for_protobuf ⇒ Object
- #fields_from_document(options = {}) ⇒ Object
- #status_code_for_protobuf ⇒ Object
- #to_proto(options = {}) ⇒ Object
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( = {}) = () fields = _filtered_fields() fields &= [[:only]].flatten if [:only].present? fields -= [[:except]].flatten if [: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( = {}) include_deprecated = .fetch(:deprecated, true) fields = [] fields.concat(self.class.) fields.concat(self.class.) if include_deprecated fields.concat([[:include]].flatten) if [:include].present? fields.compact! fields.uniq! fields end |
#_is_collection_association?(field) ⇒ 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 () ||= {} [:only] ||= [] if .fetch(:except, false) [:except] ||= [] if .fetch(:only, false) self.class..merge() 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_transformers ⇒ Object
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_transformers ⇒ Object
305 306 307 |
# File 'lib/protobuf/mongoid/serialization.rb', line 305 def _protobuf_field_transformers self.class._protobuf_field_transformers end |
#_protobuf_message ⇒ Object
309 310 311 |
# File 'lib/protobuf/mongoid/serialization.rb', line 309 def self.class. end |
#changed_fields_from_document ⇒ Object
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_protobuf ⇒ Object
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..map do |field, | { :field => field.to_s, :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( = {}) hash = {} field_attributes = _filter_field_attributes() if [:include].present? field_attributes.concat([[: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_protobuf ⇒ Object
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
313 314 315 316 317 318 |
# File 'lib/protobuf/mongoid/serialization.rb', line 313 def to_proto( = {}) raise MessageNotDefined, self.class if .nil? fields = fields_from_document() .new(fields) end |