Module: Protobuf::ActiveRecord::Serialization::ClassMethods
- Defined in:
- lib/protobuf/active_record/serialization.rb
Defined Under Namespace
Classes: CollectionAssociationCaller, DateCaller, DateTimeCaller, FieldSymbolTransformerCaller, NilMethodCaller, NoConversionCaller
Instance Method Summary collapse
- #_protobuf_collection_association_object(field) ⇒ Object
- #_protobuf_convert_to_fields_object(field) ⇒ Object
- #_protobuf_field_objects ⇒ Object
- #_protobuf_field_options ⇒ Object
- #_protobuf_field_symbol_transformers ⇒ Object
- #_protobuf_field_transformer_object(field) ⇒ Object
- #_protobuf_field_transformers ⇒ Object
- #_protobuf_message_deprecated_fields ⇒ Object
- #_protobuf_message_non_deprecated_fields ⇒ Object
- #_protobuf_nil_object(field) ⇒ Object
- #_protobuf_symbol_transformer_object(field) ⇒ Object
-
#field_from_record(field, transformer = nil, &block) ⇒ Object
Define a field transformation from a record.
-
#protobuf_fields(*fields) ⇒ Object
Define the protobuf fields that will be automatically serialized (by default, all fields will be serialized).
-
#protobuf_message(message = nil, options = {}) ⇒ Object
Define the protobuf message class that should be used to serialize the object to protobuf.
Instance Method Details
#_protobuf_collection_association_object(field) ⇒ Object
158 159 160 |
# File 'lib/protobuf/active_record/serialization.rb', line 158 def _protobuf_collection_association_object(field) CollectionAssociationCaller.new(field) end |
#_protobuf_convert_to_fields_object(field) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/protobuf/active_record/serialization.rb', line 210 def _protobuf_convert_to_fields_object(field) = (field) is_date_column = _protobuf_date_column?(field) if if is_date_column DateCaller.new(field) else DateTimeCaller.new(field) end else NoConversionCaller.new(field) end end |
#_protobuf_field_objects ⇒ Object
23 24 25 |
# File 'lib/protobuf/active_record/serialization.rb', line 23 def _protobuf_field_objects @_protobuf_field_objects ||= {} end |
#_protobuf_field_options ⇒ Object
27 28 29 |
# File 'lib/protobuf/active_record/serialization.rb', line 27 def ||= {} end |
#_protobuf_field_symbol_transformers ⇒ Object
31 32 33 |
# File 'lib/protobuf/active_record/serialization.rb', line 31 def _protobuf_field_symbol_transformers @_protobuf_field_symbol_transformers ||= {} end |
#_protobuf_field_transformer_object(field) ⇒ Object
225 226 227 |
# File 'lib/protobuf/active_record/serialization.rb', line 225 def _protobuf_field_transformer_object(field) _protobuf_field_transformers[field] end |
#_protobuf_field_transformers ⇒ Object
35 36 37 |
# File 'lib/protobuf/active_record/serialization.rb', line 35 def _protobuf_field_transformers @_protobuf_field_transformers ||= {} end |
#_protobuf_message_deprecated_fields ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/protobuf/active_record/serialization.rb', line 39 def ||= begin self..all_fields.map do |field| next if field.nil? next unless field.deprecated? field.name.to_sym end end end |
#_protobuf_message_non_deprecated_fields ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/protobuf/active_record/serialization.rb', line 50 def ||= begin self..all_fields.map do |field| next if field.nil? next if field.deprecated? field.name.to_sym end end end |
#_protobuf_nil_object(field) ⇒ Object
237 238 239 |
# File 'lib/protobuf/active_record/serialization.rb', line 237 def _protobuf_nil_object(field) NilMethodCaller.new end |
#_protobuf_symbol_transformer_object(field) ⇒ Object
252 253 254 |
# File 'lib/protobuf/active_record/serialization.rb', line 252 def _protobuf_symbol_transformer_object(field) FieldSymbolTransformerCaller.new(self, _protobuf_symbol_transformer_object[field]) end |
#field_from_record(field, transformer = nil, &block) ⇒ Object
Define a field transformation from a record. Accepts a Symbol, callable, or block that is called with the record being serialized.
When given a callable or block, it is directly used to convert the field.
When a symbol is given, it extracts the method with the same name.
The callable or method must accept a single parameter, which is the proto message.
Examples:
field_from_record :public_key, :convert_public_key_to_proto
field_from_record :status, lambda { |record| # Do some stuff... }
field_from_record :status do |record|
# Do some blocky stuff...
end
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/protobuf/active_record/serialization.rb', line 78 def field_from_record(field, transformer = nil, &block) if transformer.is_a?(Symbol) _protobuf_field_symbol_transformers[field] = transformer return end transformer ||= block callable = transformer unless callable.respond_to?(:call) raise FieldTransformerError end _protobuf_field_transformers[field.to_sym] = callable end |
#protobuf_fields(*fields) ⇒ Object
Define the protobuf fields that will be automatically serialized (by default, all fields will be serialized). Accepts any number of field names and is equivalent to passing the :only option to protobuf_message.
If :except is specified, all fields except the specified fields will be serialized.
By default, deprecated fields will be serialized. To exclude deprecated fields, pass :deprecated => false in the options hash.
Examples:
protobuf_fields :guid, :name
protobuf_fields :except => :email_domain
protobuf_fields :except => :email_domain, :deprecated => false
107 108 109 110 111 112 |
# File 'lib/protobuf/active_record/serialization.rb', line 107 def protobuf_fields(*fields) = fields. [:only] = fields if fields.present? self. = end |
#protobuf_message(message = nil, options = {}) ⇒ Object
Define the protobuf message class that should be used to serialize the object to protobuf. Accepts a string or symbol and an options hash.
When protobuf_message is declared, Protoable automatically extracts the fields from the message and automatically adds a to_proto method that serializes the object to protobuf.
The fields that will be automatically serialized can be configured by passing :only or :except in the options hash. If :only is specified, only the specified fields will be serialized. If :except is specified, all field except the specified fields will be serialized.
By default, deprecated fields will be serialized. To exclude deprecated fields, pass :deprecated => false in the options hash.
Examples:
:user_message
"UserMessage"
"Namespaced::UserMessage"
:user_message, :only => [ :guid, :name ]
:user_message, :except => :email_domain
:user_message, :except => :email_domain, :deprecated => false
137 138 139 140 141 142 143 144 |
# File 'lib/protobuf/active_record/serialization.rb', line 137 def ( = nil, = {}) unless .nil? = .to_s.classify.constantize self. = end end |