Module: Protobuf::ActiveRecord::Serialization
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/protobuf/active_record/serialization.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#_filter_field_attributes(options = {}) ⇒ Object
:nodoc:.
-
#_filtered_fields(options = {}) ⇒ Object
:nodoc:.
-
#_is_collection_association?(field) ⇒ Boolean
:nodoc:.
-
#_normalize_options(options) ⇒ Object
:nodoc:.
-
#_protobuf_field_objects(field) ⇒ Object
:nodoc:.
-
#_protobuf_field_symbol_transformers ⇒ Object
:nodoc:.
-
#_protobuf_field_transformers ⇒ Object
:nodoc:.
-
#_protobuf_message ⇒ Object
:nodoc:.
-
#fields_from_record(options = {}) ⇒ Object
Extracts attributes that correspond to fields on the specified protobuf message, performing any necessary column conversions on them.
-
#to_proto(options = {}) ⇒ Object
:nodoc:.
Instance Method Details
#_filter_field_attributes(options = {}) ⇒ Object
:nodoc:
258 259 260 261 262 263 264 265 266 |
# File 'lib/protobuf/active_record/serialization.rb', line 258 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
:nodoc:
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/protobuf/active_record/serialization.rb', line 269 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
:nodoc:
283 284 285 286 287 288 |
# File 'lib/protobuf/active_record/serialization.rb', line 283 def _is_collection_association?(field) reflection = self.class.reflect_on_association(field) return false unless reflection reflection.macro == :has_many end |
#_normalize_options(options) ⇒ Object
:nodoc:
291 292 293 294 295 296 297 |
# File 'lib/protobuf/active_record/serialization.rb', line 291 def () ||= {} [:only] ||= [] if .fetch(:except, false) [:except] ||= [] if .fetch(:only, false) self.class..merge() end |
#_protobuf_field_objects(field) ⇒ Object
:nodoc:
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/protobuf/active_record/serialization.rb', line 336 def _protobuf_field_objects(field) self.class._protobuf_field_objects[field] ||= begin case when _protobuf_field_symbol_transformers.has_key?(field) then self.class._protobuf_symbol_transformer_object(field) when _protobuf_field_transformers.has_key?(field) then self.class._protobuf_field_transformer_object(field) when respond_to?(field) then 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 end |
#_protobuf_field_symbol_transformers ⇒ Object
:nodoc:
356 357 358 |
# File 'lib/protobuf/active_record/serialization.rb', line 356 def _protobuf_field_symbol_transformers self.class._protobuf_field_symbol_transformers end |
#_protobuf_field_transformers ⇒ Object
:nodoc:
361 362 363 |
# File 'lib/protobuf/active_record/serialization.rb', line 361 def _protobuf_field_transformers self.class._protobuf_field_transformers end |
#_protobuf_message ⇒ Object
:nodoc:
366 367 368 |
# File 'lib/protobuf/active_record/serialization.rb', line 366 def self.class. 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)
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/protobuf/active_record/serialization.rb', line 309 def fields_from_record( = {}) hash = {} field_attributes = _filter_field_attributes() # Already flattened / compacted / uniqued ... unless we must include if [:include].present? field_attributes.concat([ [:include] ].flatten) field_attributes.compact! field_attributes.uniq! end attribute_number = 0 limit = field_attributes.size # One of the very few places the diff between each/while can make a difference # in terms of optimization (`while` is slightly faster as no block carried through) while attribute_number < limit field = field_attributes[attribute_number] field_object = _protobuf_field_objects(field) hash[field] = field_object.call(self) attribute_number += 1 end hash end |
#to_proto(options = {}) ⇒ Object
:nodoc:
371 372 373 374 375 376 |
# File 'lib/protobuf/active_record/serialization.rb', line 371 def to_proto( = {}) raise MessageNotDefined.new(self.class) if .nil? fields = self.fields_from_record() .new(fields) end |