Module: Protoable::Serialization::ClassMethods
- Defined in:
- lib/protobuf/activerecord/protoable/serialization.rb
Instance Method Summary collapse
-
#_protobuf_convert_attributes_to_fields(key, value) ⇒ Object
:nodoc:.
-
#field_from_record(field, transformer = nil, &block) ⇒ Object
Define a field transformation from a record.
-
#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_convert_attributes_to_fields(key, value) ⇒ Object
:nodoc:
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/protobuf/activerecord/protoable/serialization.rb', line 24 def _protobuf_convert_attributes_to_fields(key, value) return value if value.nil? value = case when _protobuf_date_column?(key) then value.to_time.to_i when _protobuf_datetime_column?(key) then value.to_i when _protobuf_time_column?(key) then value.to_i when (key) then value.to_i else value end return value 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/protobuf/activerecord/protoable/serialization.rb', line 60 def field_from_record(field, transformer = nil, &block) transformer ||= block if transformer.is_a?(Symbol) callable = lambda { |value| self.__send__(transformer, value) } else callable = transformer end unless callable.respond_to?(:call) raise FieldTransformerError, 'Attribute transformers need a callable or block!' end _protobuf_field_transformers[field.to_sym] = callable 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
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/protobuf/activerecord/protoable/serialization.rb', line 99 def ( = nil, = {}) unless .nil? @protobuf_message = .to_s.classify.constantize self. = define_method(:to_proto) do | = {}| self.class..new(self.fields_from_record()) end end @protobuf_message end |